I have testing application on Spring mvc, for reporting using JasperReports, my code is this:
response.addHeader("Content-disposition", "attachment; filename=" + "testExcel.xls");
response.setContentType("application/vnd.ms-excel");
ServletOutputStream outputStream=response.getOutputStream();
ServletContext context = request.getServletContext();
String filename = "/WEB-INF/resources/jasperReports/testExcel.jasper";
Map<String,Object> params = new HashMap<>();
List<Customer> listCustomer = service.listDataSource();
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(listCustomer);
try {
InputStream is = context.getResourceAsStream(filename);
if (is != null) {
JasperReport report = JasperCompileManager.compileReport(is);
JasperPrint jasperPrint = JasperFillManager.fillReport(report, params, dataSource);
JRXlsExporter exporterXLSX = new JRXlsExporter();
exporterXLSX.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporterXLSX.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
exporterXLSX.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporterXLSX.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporterXLSX.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, outputStream);
exporterXLSX.exportReport();
is.close();
outputStream.flush();
outputStream.close();
}
} catch (JRException exp) {
exp.printStackTrace();
}
and reports generates but can not open because of error : file format and extension don't match testExcel.xls what's wrong i don't get. dataSource is good because i successfully generated pdf using it.