话不多说,直接上代码。依赖使用easypoi
try(OutputStream outputStream = response.getOutputStream()) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
BuserMyPageDTO data1 = this.buserSearch(filterDTO).getData();
String templateFileName = DuserConstants.BATCH_EXPORT_TEMP;
// 查询列表
List<BuserReqDTO> busiList = buserService.searchListForExport(“传参”).getData();
// 获取模板地址
String templateUrl = ExcelUtil.getTemplate(templateFileName);
logger.info("导出excel模板地址:{}", templateUrl);
String fileNmae = DuserConstants.FILE_NAME + ".xls";
// 模板导出参数封装
TemplateExportParams templateExportParams = new TemplateExportParams();
templateExportParams.setTemplateUrl(templateUrl);
Map<String, Object> map = new HashMap<>();
Map<String, String> otherObjMap = new HashMap<String, String>();
otherObjMap.put("inventoryBatchId", data1.getInventoryBatchId());
otherObjMap.put("count", data1.getCount() + "");
// otherObjMap.put("regDate", format.format(data1.getRegDate()));
// if (StringUtils.isNotBlank(data1.getApprovalOperName())) {
// otherObjMap.put("approvalDate", data1.getApprovalDateName());
// } else {
// otherObjMap.put("approvalDate", "");
// }
// otherObjMap.put("date", data1.getApprovalDateName());
map.put("otherObjMap", otherObjMap);
map.put("busiList", busiList);
logger.info("导出excel参数集合:{}", map);
Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, map);
// new String(fileNmae.getBytes("GBK"), StandardCharsets.ISO_8859_1)
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileNmae,"UTF-8"));
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// response.setContentType("application/octet-stream");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
workbook.write(byteArrayOutputStream);
response.setHeader("Content-Length", "" + byteArrayOutputStream.size());
outputStream.write(byteArrayOutputStream.toByteArray());
byteArrayOutputStream.close();