@Override
public void exportPostOverview(HttpServletResponse response) throws IOException {
// 获取所有数据
List headcountViewList =
headcountViewMapper.getAllHeadcountView();
List buCodeList = getBuCodeExportList();
List regionCodeList =
getRegionCodeExportList(headcountViewList);
List areaCodeList = getAreaCodeExportList(headcountViewList);
// 设置输入流,设置响应域
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("编制总览.xlsx", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);
// 需要合并的列
List ignoreColumn = new ArrayList<>();
ignoreColumn.add(6);
ignoreColumn.add(7);
ignoreColumn.add(8);
ignoreColumn.add(10);
ignoreColumn.add(11);
// 需要从第一行开始,列头第一行,到第12列
int mergeRowIndex = 1;
int mergeColumnRegion = 11;
// 将数据映射到DownloadDTO实体类并响应到浏览器
OutputStream os = response.getOutputStream();
ExcelWriter excelWriter = EasyExcelFactory.write(os).autoCloseStream(Boolean.TRUE).build();
WriteSheet buSheet =
EasyExcelFactory.writerSheet(0, "BU")
.head(HeadcountOverviewBuCodeDTO.class)
.registerWriteHandler(new ExcelFillCellMergeStrategy(mergeRowIndex, mergeColumnRegion, ignoreColumn))
.build();
WriteSheet regionSheet =
EasyExcelFactory.writerSheet(1, "Region")
.head(HeadcountOverviewRegionCodeDTO.class)
.registerWriteHandler(new ExcelFillCellMergeStrategy(mergeRowIndex, mergeColumnRegion, ignoreColumn))
.build();
WriteSheet areaSheet =
EasyExcelFactory.writerSheet(2, "Area")
.head(HeadcountOverviewAreaCodeDTO.class)
.registerWriteHandler(new ExcelFillCellMergeStrategy(mergeRowIndex, mergeColumnRegion, ignoreColumn))
.build();
excelWriter
.write(buCodeList, buSheet)
.write(regionCodeList, regionSheet)
.write(areaCodeList, areaSheet);
}
这里是导出包含3个sheet的excel文件。