pom引入依赖:
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.1.0</version>
</dependency>
代码示例:
@ApiOperation("导出分类内容")
@PostMapping(value = UrlRuleConstants.CLASS_EXPORT, produces = UrlRuleConstants.PRODUCES)
public void exportMoreSheet(HttpServletResponse response, @RequestBody IdsHandoverLogVo vo) {
UserInfo userInfo = CommonUtil.filterCpmoCopForString();
List<HandoverLogExcelVo> list = new ArrayList<>();
List<HandoverExcelVo> exportContentList = getExportContentList(vo, userInfo);
if (!HandoverTypeEnum.SUPPLY_GAS_EXPLAIN.getCode().equals(vo.getType())) {
for (HandoverExcelVo excelVo : exportContentList) {
HandoverLogExcelVo handoverLogExcelVo = new HandoverLogExcelVo();
handoverLogExcelVo.setClassType(excelVo.getClassType());
handoverLogExcelVo.setHandoverPerson(excelVo.getHandoverPerson());
handoverLogExcelVo.setHandoverTime(excelVo.getHandoverTime());
handoverLogExcelVo.setValue(excelVo.getValue());
list.add(handoverLogExcelVo);
}
}
//1.分别构建客户与场站信息导出参数
ExportParams exportParam = new ExportParams();
//标题
exportParam.setTitle(HandoverTypeEnum.getDescBycode(vo.getType()));
//sheet标题
exportParam.setSheetName("分类内容");
exportParam.setHeight((short) 8);
exportParam.setStyle(ExcelStyleUtil.class);
//2.构建分别填充数据:data(数据)、title(标题)、entity(导出属性)
Map<String, Object> exportCustomerMap = new HashMap<String, Object>();
if (vo.getType() == 1) {
exportCustomerMap.put("data", exportContentList);
exportCustomerMap.put("entity", HandoverExcelVo.class);
} else {
exportCustomerMap.put("data", list);
exportCustomerMap.put("entity", HandoverLogExcelVo.class);
}
exportCustomerMap.put("title", exportParam);
//3.组装参数
List<Map<String, Object>> sheetsList = new ArrayList<Map<String, Object>>();
sheetsList.add(exportCustomerMap);
try {
//4.调用导出接口
Workbook workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
String fileName = "分类内容";
fileName = URLEncoder.encode(fileName, "UTF8");
response.setContentType("application/vnd.ms-excel;chartset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
ServletOutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}