前端:
postExcelFile(params, url) { // params是post请求需要的参数,url是请求url地址 const form = document.createElement('form'); form.style.display = 'none'; form.action = url; form.method = 'post'; document.body.appendChild(form); // 动态创建input并给value赋值 /* eslint-disable-next-line */ for (const key in params) { if (params[key]) { const input = document.createElement('input'); input.type = 'hidden'; input.name = key; input.value = params[key]; form.appendChild(input); } } form.submit(); form.remove(); }, // 导出 exportExcel() { this.postExcelFile( this.searchForm, '/eya-pos/posDeal/exportExcel', ); },
后台:
@PostMapping(value = "/exportExcel") public void exportExcel(HttpServletResponse response,String sku,Integer dealStatus){ DealSearch dealSearch = new DealSearch(); dealSearch.setSku(sku); dealSearch.setDealStatus(dealStatus); List<DealExport> exportList = iPosDealsService.findExportList(dealSearch); String template = 'template/PosSeckillActivityDealTemplate.xlsx'; AsposeUtils.registerCells(); try { InputStream templateStream = this.getClass().getClassLoader().getResourceAsStream(template); OutputStream outputStream = response.getOutputStream(); Workbook workbook = new Workbook(templateStream); WorkbookDesigner designer = new WorkbookDesigner(); designer.setWorkbook(workbook); designer.setDataSource("ExportList",exportList); designer.process(); response.setContentType("application/force-download"); String fileName = "Deal活动导出" + DateTimeUtils.getNowSimpleDateTime() + ".xlsx"; response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(fileName,"UTF-8")); workbook.save(outputStream,workbook,getFileFormat()); outputStream.flush(); outputStream.close(); } catch (IOException ix){ ix.printStackTrace(); } }