需求:
导出列表数据进行分析,并根据不同的维度展示在不同的sheet中
由于hutool提供的sheet 方法不明确多个sheet 页面但是数据只在一个当中所以自己封装一个utils
hutool:Hutool参考文档
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.shands.mod.quality.service.v2.SheetDTO;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
public class HuExcelUtils {
public static void exportExcel(HttpServletResponse response, List sheetList,
String fileName) {
ExcelWriter bigWriter = ExcelUtil.getBigWriter();
// 重命名第一个Sheet的名称,不然会默认多出一个Sheet1的页
bigWriter.renameSheet(0, sheetList.get(0).getSheetName());
for (SheetDTO sheet : sheetList) {
// 指定要写出的 Sheet 页
bigWriter.setSheet(sheet.getSheetName());
Integer[] columnWidth = sheet.getColumnWidth();
if (columnWidth == null || columnWidth.length != sheet.getFieldAndAlias().size()) {
// 设置默认宽度
for (int i = 0; i < sheet.getFieldAndAlias().size(); i++) {
bigWriter.setColumnWidth(i, 25);
}
} else {
// 设置自定义宽度
for (int i = 0; i < columnWidth.length; i++) {
bigWriter.setColumnWidth(i, columnWidth[i]);
}
}
// 设置字段和别名
bigWriter.setHeaderAlias(sheet.getFieldAndAlias());
// 设置只导出有别名的字段
bigWriter.setOnlyAlias(true);
// 设置默认行高
bigWriter.setDefaultRowHeight(18);
// 设置冻结行
//bigWriter.setFreezePane(1);
// 一次性写出内容,使用默认样式,强制输出标题
bigWriter.write(sheet.getCollection(), true);
// 设置所有列为自动宽度,不考虑合并单元格
// bigWriter.autoSizeColumnAll();
}
ServletOutputStream out = null;
try {
//response为HttpServletResponse对象
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition",
"attachment;filename=" +
URLEncoder.encode(fileName + DateUtil.today() + ".xlsx", "UTF-8"));
out = response.getOutputStream();
bigWriter.flush(out, true);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭writer,释放内存
bigWriter.close();
}
//此处记得关闭输出Servlet流
IoUtil.close(out);
}
}
Controller
@PostMapping("/export")
@ApiOperation("导出")
public ResultVO export(HttpServletResponse response,@RequestBody Map map) {
log.info("报告)-请求参数:{}", JSONObject.toJSONString(map));
patrolReportService.exportBlocPreviewReport(response,map.get("checkId"));
return ResultVO.success();
}
sericeImpl
@Override
public void export(HttpServletResponse response, Integer checkId) {
List patrolTopicVo = qtCheckDetailsMapper.selecteachTopic(checkId);
AtomicInteger index = new AtomicInteger();
List sheetList = new ArrayList<>(2);
Integer size = 2;
for (int a = 0; a < size; a++) {
if (a == 0) {
LinkedHashMap map = new LinkedHashMap<>();
// 设置只导出有别名的字段
map.put("id", "序号");
map.put("hotelCode", "酒店编码");
map.put("hotelName", "酒店名称");
map.put("cooperationName", "归属事业部");
map.put("ownershipName", "管理方式");
map.put("taskStartTime", "任务时间");
map.put("checkPeople", "巡检员");
map.put("passRate", "检查合格率");
map.put("totalScore", "报告得分");
SheetDTO sheet = new SheetDTO();
sheet.setFieldAndAlias(map);
sheet.setCollection(patrolTopicVo);
sheet.setSheetName("整体情况");
sheetList.add(sheet);
index.getAndIncrement();
}
if (a == 1) {
LinkedHashMap map = new LinkedHashMap<>();
// 设置只导出有别名的字段
map.put("id", "序号");
map.put("departmentName", "部门");
map.put("dimensionName", "维度");
map.put("areaName", "区域");
map.put("projectName", "题目");
map.put("className", "分类");
map.put("qualifiedSize", "合格题目数量");
map.put("failedSize", "不合格题目数量");
map.put("notApplication", "不适用题目数量");
map.put("passRate", "合格率");
SheetDTO sheet = new SheetDTO();
sheet.setFieldAndAlias(map);
sheet.setCollection(patrolTopicVo);
sheet.setSheetName("题项明细");
sheetList.add(sheet);
index.getAndIncrement();
}
}
try {
HuExcelUtils.exportExcel(response, sheetList, "明细样式");
} catch (Exception e) {
e.printStackTrace();
}
}



