2014-09-02 52 views
0

如何將jasperReport導出到pptx?將Jasperreports導出到Servlet的pptx

我嘗試這一點,但不工作...

該servlet返回一個PPTX文件,但

 JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, parametros, new JREmptyDataSource()); 
     String fileName = "informeDemo.pptx"; 
     JRPptxExporter exporter = new JRPptxExporter(); 
     exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
     exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, fileName); 
     exporter.exportReport(); 

     File f = new File(fileName); 

     response.setContentType("application/vnd.openxmlformats-officedocument.presentationml.presentation"); //Tipo de fichero. 
     response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\""); //Configurar cabecera http 

     InputStream in = new FileInputStream(f); 
     out = response.getOutputStream(); 

     int bit = 256; 
     while ((bit) >= 0) { 
      bit = in.read(); 
      out.write(bit); 
     } 

     out.flush(); 
     out.close(); 
     in.close(); 

回答

1

這裏的做下面的方式將文件無法打開:

   JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, parametros, new JREmptyDataSource()); 
       String fileName = "informeDemo.pptx"; 
       JRPptxExporter exporter = new JRPptxExporter(); 
       exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
       exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, fileName); 
       ByteOutputStream bos=new ByteOutputStream(); // note bos will contain jasperPrint as byte data 
       exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,bos); 
       exporter.exportReport(); 

       response.setContentType("application/vnd.openxmlformats-officedocument.presentationml.presentation"); //Tipo de fichero. 
       response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\""); //Configurar cabecera http 
       response.setCharacterEncoding("UTF-8"); 
       response.getOutputStream().write(bos.getBytes()); //note bos is written to response's output stream. 
       bos.flush(); 
       bos.close(); 
       response.flushBuffer(); 

希望這可以幫助你。