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
I encountered the same problem with the XLSX (but not xls) exporter when
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).
Try this
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.