Unreadable content message with Jasper and .xlsx

2019-06-07 15:11发布

问题:

I have the following code that exports a xlsx to a stream. It works and I get the xlsx file but it says excel found unreadable content in "..." and asks if I want to recover the contents of the workbook. When I do so, I get the xlsx file with the correct data, in the correct places, as I wanted. How can I avoid or supress this error?

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

    }

using using JasperReports v4.7.1

[UPDATE] tried for when no data type the following options No pages, Blank Page, All Sections No Detail and No Data Section none of them worked

also tried adding thing to my web.xml

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

finally tried closing my outStream which didn't work either, excel still says unreadable content

回答1:

I encountered the same problem with the XLSX (but not xls) exporter when

  1. The report prints no pages when there's no data
  2. The data list is empty.

Inspect your data list and set your report to print something other than "no pages" (e.g. no data section or all sections without detail).



回答2:

This means the report had no data. To fix the bad file message simply add the NoData section to your report with a meaningful message.



回答3:

Try this

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 {