java - 使用spring的Jasper报告:文件格式和扩展名不匹配xls
问题描述
我在 Spring mvc 上有测试应用程序,用于使用 JasperReports 进行报告,我的代码是这样的:
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();
}
并且报告生成但由于错误而无法打开:文件格式和扩展名不匹配 testExcel.xls 出了什么问题我不明白。dataSource 很好,因为我使用它成功生成了 pdf。
解决方案
推荐阅读
- rxjs - 热的未完成的数据库可观察对象是 Rx 用例吗?副作用写作问题
- ios - Firebase iOS 卡在“运行您的应用程序以验证安装”
- android - 从带有 KSoap2 的 Android 到 .net WebServer 的 SOAP 调用
- javascript - 如何只允许输入 textarea 数字、许可证、单词白名单中的内容
- c# - MVCGrid.NET - 如何重新加载网格?
- javascript - 为什么 webpack 代理不起作用(Access-Control-Allow-Origin 错误)?
- c++ - OpenCL 和 C++:事件无法正常工作
- r - 如何从 R markdown 文档中的文件中读取 markdown 代码
- javascript - 如何从 CONNECT 平台上的包中的另一个节点进行节点调用?
- javascript - 抓取 json 请求 `ak` 参数值