后台util

fileParamList中为每个模板的参数,参数包括filePath模板文件路径(templates下面的),downloadFileName为带后缀的文件名

public static void batchDownLoadExcelForZip(List<Map<String, Object>> fileParamList, HttpServletResponse response)throws Exception {OutputStream os = null;ZipOutputStream zipOutputStream = null;try{response.setHeader("Content-disposition", "attachment; filename=" + "test.zip");response.setContentType("application/zip; charset=utf-8");os = new BufferedOutputStream(response.getOutputStream());zipOutputStream = new ZipOutputStream(os);for(Map<String, Object> paramMap:fileParamList){ByteArrayOutputStream baos = new ByteArrayOutputStream();if("word".equals(paramMap.get("fileType"))){InputStream is = TemplateExcelUtils.class.getClassLoader().getResourceAsStream("templates/"+paramMap.get("filePath"));if(null!=is){XWPFTemplate template = XWPFTemplate.compile(is).render(new HashMap<>());template.write(baos);is.close();}}else{//导出excelWorkbook workBook = getWorkBookForDownLoad(paramMap);//将workbook写入内存流workBook.write(baos);workBook.close();}ZipEntry zipEntry = new ZipEntry((String) paramMap.get("downloadFileName"));zipOutputStream.putNextEntry(zipEntry);//将内存流写入zip文件zipOutputStream.write(baos.toByteArray());}}catch (Exception e){e.printStackTrace();throw e;}finally {if(null !=zipOutputStream){zipOutputStream.closeEntry();zipOutputStream.flush();zipOutputStream.close();}if(null != os){os.close();}}}private static Workbook getWorkBookForDownLoad(Map<String, Object> fileParams) throws Exception {Workbook workbook = null;try {//读取模板InputStream is = TemplateExcelUtils.class.getClassLoader().getResourceAsStream("templates/"+fileParams.get("filePath"));XLSTransformer transformer = new XLSTransformer();//向模板中写入内容workbook = transformer.transformXLS(is, fileParams);HSSFSheet sheet = (HSSFSheet) workbook.getSheetAt(0);if(ObjectUtils.isNotEmpty(fileParams.get("mergCellNos"))){List<Map<String,Integer>> mergCellNoMaps = (List<Map<String, Integer>>) fileParams.get("mergCellNos");for(Map<String,Integer> mergMap:mergCellNoMaps){sheet.addMergedRegion(new CellRangeAddress(mergMap.get("firstRow"),mergMap.get("lastRow"),mergMap.get("firstCell"),mergMap.get("lastCell")));//起始行,截止行号,起始列,截止列}}}catch (Exception e){e.printStackTrace();throw e;}finally {if(null !=workbook){//workbook.close();}}return workbook;}

controller

@PostMapping("/exporttest")public void exportDirectorData(@RequestBody Map<String, Object> param, HttpServletResponse httpServletResponse) {List<Map<String, Object>> fileParamList = cmBaseDirectorExport.getFileParams(param);try{TemplateExcelUtils.batchDownLoadExcelForZip(fileParamList,httpServletResponse);}catch (Exception e){e.printStackTrace();}}

前台写法

 handleExportDs() {const that = this// const queryParams = JSON.parse(JSON.stringify(this.queryParams))let param = { id: '123' }this.$confirm('请确认是否导出?', '警告', {confirmButtonText: '确定',cancelButtonText: '取消',type: 'warning'}).then(function() {that.loading = truerequest({method: 'post',url: 'test/exporttest',data: param,responseType: 'blob'}).then(res => {that.loading = falseconst aLink = document.createElement('a')var fileName = '测试数据导出' + moment().format('YYYYMMDDHHmmss') + '.zip'fileName = fileName.replace(/\"/g, '')const url = window.URL.createObjectURL(res)aLink.href = urlaLink.download = fileNameaLink.click()aLink.remove()URL.revokeObjectURL(url)})}).catch(function() { })},