【问题标题】:Unreadable content message with Jasper and .xlsx带有 Jasper 和 .xlsx 的不可读内容消息
【发布时间】:2015-10-17 10:37:25
【问题描述】:

我有以下将 xlsx 导出到流的代码。它有效,我得到了 xlsx 文件,但它说 excel 在“...”中找到了不可读的内容,并询问我是否要恢复工作簿的内容。当我这样做时,我会根据需要在正确的位置获得包含正确数据的 xlsx 文件。如何避免或抑制此错误?

try {
        ServletOutputStream servletOutputStream = resp.getOutputStream();
        JasperReport jasperReport = JasperCompileManager
                .compileReport(path
                        + "Template/Instructions_with_CHGFOX_Template/testeXlsx2.jrxml");

        JasperPrint print = JasperFillManager.fillReport(jasperReport,
                parameters, new JRBeanCollectionDataSource(list));

        JRXlsxExporter xlsxExporter = new JRXlsxExporter();
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();

        xlsxExporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
        xlsxExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,
                title + ".xlsx");

        xlsxExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
                servletOutputStream);

        xlsxExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outStream);
        xlsxExporter.exportReport();

        resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        resp.setHeader("Content-Disposition", "attachment; filename="
                + title + ".xlsx");

        servletOutputStream.write(outStream.toByteArray());

        resp.getOutputStream().write(outStream.toByteArray());
        resp.getOutputStream().flush();
        resp.getOutputStream().close();
        resp.flushBuffer();

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

    }

使用 JasperReports v4.7.1

[更新] 当没有数据类型时尝试以下选项 无页、空白页、所有部分 无详细信息和无数据部分 他们都没有工作

还尝试将内容添加到我的 web.xml

<mime-mapping>
<extension>xlsx</extension>
<mime-type>application/vnd.openxmlformats-          officedocument.spreadsheetml.sheet</mime-type>
</mime-mapping>

终于尝试关闭我的 outStream 也没有用,excel 仍然显示不可读的内容

【问题讨论】:

    标签: java stream jasper-reports xlsx


    【解决方案1】:

    我在使用 XLSX(但不是 xls)导出器时遇到了同样的问题

    1. 没有数据时报表不打印页面
    2. 数据列表为空。

    检查您的数据列表并将您的报告设置为打印“无页面”以外的内容(例如,没有数据部分或所有部分都没有详细信息)。

    【讨论】:

    • 在没有数据的情况下,除了没有页面,如何打印?
    • 如果您在 ireport 中进行设计,您可以在报告属性中设置它(右键单击报告检查器中的最顶部节点)或向根 元素添加属性,例如
    • 我正在使用 JasperSoft Studio,我的属性中有“何时执行数据类型 -> 空白页”
    • 我想您可以将其更改为其他值,例如“无数据部分”。见community.jaspersoft.com/questions/534461/how-use-no-data-band
    • 我也试过了。我尝试了以下选项无页面、空白页面、所有部分无详细信息和无数据部分
    【解决方案2】:

    这意味着该报告没有数据。要修复错误文件消息,只需将 NoData 部分添加到您的报告中,并添加一条有意义的消息。

    【讨论】:

    • 如果我没有数据,你的意思是做一个 if?
    • 您不需要指定条件。当报表行数为零时,将打印 NoData 部分而不是所有其他波段。
    • 我在没有数据类型时选择了空白页,但仍然遇到同样的问题
    • 在报告中当没有数据属性时,您应该指定无数据部分,然后启用无数据带。
    【解决方案3】:

    试试这个

    if (reportType.equals("excel")) {
            try {
    
                ServletOutputStream servletOutputStream = resp
                        .getOutputStream();
                JasperReport jasperReport = JasperCompileManager
                        .compileReport(path + "Template/" + template);
    
                JasperPrint print = JasperFillManager.fillReport(jasperReport,
                        parameters, new JRBeanCollectionDataSource(list));
    
                JRXlsxExporter exporter = new JRXlsxExporter();
                exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, os);
                exporter.exportReport();
                resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                resp.setHeader("Content-Disposition", "attachment;filename="+ title + ".xlsx");
                resp.getOutputStream().write(os.toByteArray());
                resp.flushBuffer();
    
            } catch (JRException e) {
                e.printStackTrace();
            }
    
        } else {
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-29
      • 2013-02-08
      • 2022-01-25
      • 2018-02-14
      • 2022-11-22
      • 2018-11-19
      • 1970-01-01
      • 2017-02-24
      相关资源
      最近更新 更多