从Jaslet导出Jasperreports到pptx

时间:2014-09-02 15:12:12

标签: java servlets jasper-reports

如何将jasperReport导出到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 个答案:

答案 0 :(得分:2)

以下是这样做的:

                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();

希望这会对你有所帮助。