Unreadable content message with Jasper and .xlsx

寵の児 提交于 2019-12-06 16:47:56

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).

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.

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 {
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!