我无法选择在Jasper

时间:2015-12-23 18:26:10

标签: java jasper-reports odt

当我点击生成报告时出现问题...我希望我点击按钮生成报告时会显示一个窗口,询问我要保存文档的位置,现在的方式我是& #39; m在java代码中指定位置和文件名,并且始终将文件保存在代码中的指定位置,我不想要它,我需要让它保持打开状态,供人选择保存位置。我将使用一段代码。

  try {
        URL arquivo = getClass().getResource(/reports/term.jasper);
        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(arquivo);

        //It generates the dto that will be sent to IReport
        ArrayList<MinutoTRDto> dataList = getDataBeanList(licitacao);
        JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);

        Map<String, Object> parameters = getParametros();            
        JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);

        JROdtExporter exporterOdt = new JROdtExporter();
        exporterOdt.setExporterInput(new SimpleExporterInput(print));



        // HERE IS THE PROBLEM!
        exporterOdt.setExporterOutput(new SimpleOutputStreamExporterOutput("C://teste//sample_report.odt"));
        exporterOdt.exportReport();

    } catch (JRException jre) {
        jre.printStackTrace();
    }

2 个答案:

答案 0 :(得分:0)

您必须在代码中编写以下内容

response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition",
"attachment;filename=<your file name>");

这样,将出现“文件下载”对话框。您可以参考here获取完整代码。

答案 1 :(得分:0)

在AMDG回复之后,我将我的文件更改为以下代码,只是它启动了以下异常:

org.apache.jasper.JasperException:java.lang.IllegalStateException:已为此响应调用了getOutputStream()

我在框架中使用Wicket

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    try {
        URL arquivo = getClass().getResource(REPORT_PATH);
        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(arquivo);

        ArrayList<MinutoTRDto> dataList = getDataBeanList(licitacao);
        JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);

        Map<String, Object> parameters = getParametros();            
        print = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);

        JROdtExporter exporterOdt = new JROdtExporter();
        exporterOdt.setExporterInput(new SimpleExporterInput(print));
        exporterOdt.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
        exporterOdt.exportReport();

        byte[] bytes = outputStream.toByteArray();
        if (bytes != null && bytes.length > 0) {
            HttpServletResponse response =(HttpServletResponse)((WebResponse)comp.getResponse()).getContainerResponse();
            response.setContentType("application/odt");
            response.setHeader("Content-disposition", "inline; filename=\"file_generated.odt\"");               
            response.setContentLength(bytes.length);            

            ServletOutputStream outputStream2 = response.getOutputStream();
            outputStream2.write(bytes, 0, bytes.length);
            outputStream2.flush();
            outputStream2.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }