如何使用Spring响应下载excel文件

我试图通过使用Jasper Report 6.2.2来下载excel文件

这是我的spring控制器:

@RequestMapping(value = "/downloadExcel", method = RequestMethod.POST) @ResponseBody public void downloadMyReportExcelFile(@RequestBody ExcelFilter excelFilter, HttpServletResponse response) { try { reportExportBo.downloadReportFile(response, excelFilter); } catch (Throwable e) { LOGGER.error("Unknown error at REST Service", e); } } 

这里也是我的downloadReportFile方法代码:

  @Override public void downloadReportFile(HttpServletResponse response, ExcelFilter excelFilter) { List<myClassObject> myObjectList= objectRecordBo.myData(excelFilter); InputStream is = this.getClass().getClassLoader().getResourceAsStream("/my_reports.jrxml"); ExcelExporter exporter = new ExcelExporter(); String fileName = "my_exported_report.xls"; JasperDesign jd = JRXmlLoader.load(is); JasperReport jr = JasperCompileManager.compileReport(jd); JasperPrint jprint = JasperFillManager.fillReport(jr, null, new JRBeanCollectionDataSource(myObjectList)); response.setHeader("Content-Disposition", "attachment; filename=" + fileName); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); JRXlsExporter xlsExporter = new JRXlsExporter(); xlsExporter.setExporterInput(new SimpleExporterInput(jprint)); xlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream())); SimpleXlsReportConfiguration xlsReportConfiguration = new SimpleXlsReportConfiguration(); xlsReportConfiguration.setOnePagePerSheet(false); xlsReportConfiguration.setRemoveEmptySpaceBetweenRows(true); xlsReportConfiguration.setDetectCellType(false); xlsReportConfiguration.setWhitePageBackground(false); xlsExporter.setConfiguration(xlsReportConfiguration); xlsExporter.exportReport(); 

my_reports.jrxml适用于myObjectList,列和variables相同。

另外这里是我的JavaScriptfunction;

 function downloadService(url, paramData, fileName, $http) { return $http.post(url, paramData, {responseType:'Content-Type'}).then(function (response) { var blob = new Blob([response.data], {type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}); var objectUrl = URL.createObjectURL(blob); var a = document.createElement("a"); a.style = "display: none"; a.href = objectUrl; a.download = fileName + '.xls' ; document.body.appendChild(a); a.click(); setTimeout(function () { document.body.removeChild(a); window.URL.revokeObjectURL(objectUrl); }, 100); }, function (response) { //TODO }); } 

调用downloadService方法后,我得到了excel下载,但它不可读

在这里输入图像说明

我错了什么?

编辑:

顺便说一句,当我在HTML方面使用;

 <a style="float:right; " href="service/downloadExcel">{{ 'EXPORT_EXCEL' | translate}}</a> 

和Spring控制器是GET和没有任何@RequestBody,它工作正常。 但我需要传递参数与JSON对象,所以我不能使用它。

我解决了我的问题。

首先,我在JasperDesign之前设置了标题和内容types的响应。

 ... if (list != null && list.size() > 0) { response.setHeader("Content-Disposition", "attachment; filename=" + fileName); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); JasperDesign jd = JRXmlLoader.load(reportStream); JasperReport jr = JasperCompileManager.compileReport(jd); ... 

我也更新了我的ajax服务;

 ... return $http.post(newUrl, paramData, {responseType: 'arraybuffer'}).then(function (response) { var blob = new Blob([response.data], {type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8'}); .... 

谢谢你帮帮忙