使⽤hutool导出Excel循环表头和数据、⾃动合并单元格、样式
修改
引⼊hutool⼯具包
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
部分代码段
public void export(ParamDto data, HttpServletResponse response) {
try (OutputStream out = OutputStream()) {
List<String> taskList = null;
List<Map<String, Object>> list = new ArrayList<>();
List<Map<String, Object>> titleList = new ArrayList<>();
if (StringUtils.TaskName())) {
taskList = Department());
}
ExcelWriter writer = Writer();
writer.setColumnWidth(-1, 18);
// 设置样式
StyleSet style = StyleSet();
CellStyle cellStyle = CellStyle();
cellStyle.setWrapText(true);
CellStyle headCellStyle = HeadCellStyle();
//⽔平居中
headCellStyle.setAlignment(HorizontalAlignment.LEFT);
headCellStyle.setWrapText(true);
//设置内容字体
Font font = ateFont();
/
/加粗
font.setBold(true);
//设置标题字体⼤⼩
font.setFontHeightInPoints((short) 16);
headCellStyle.setFont(font);
writer.setStyleSet(style);
if (CollectionUtils.isEmpty(taskList)) {
list = ActivityList(data, setList());
titleList = ActivityList(data, setTitleList());
if(!CollectionUtils.isEmpty(list)) {
/
/创建标题和表头信息
if (!CollectionUtils.isEmpty(titleList)) {
if (!CollectionUtils.isEmpty(titleList)) {
<(15, (0).get("bumenchejian") + "评价记录", true);
<(3, StringUtils.join("单位:", (0).get("bumenchejian")), true);                        writer.writeRow(new ArrayList<>());
layout(writer, titleList, 0);
writer.setRowHeight(2, 40);
}
setHeader(writer);
writer.write(list, true);
}
} else {
Integer num = 0;
for (int i = 0; i < taskList.size(); i++) {
list = ActivityList(data.(i)), setList());
titleList = ActivityList(data.(i)), setTitleList());
if(!CollectionUtils.isEmpty(list)) {
//创建标题和表头信息
if (!CollectionUtils.isEmpty(titleList)) {
if(i==0) {
<(15, (0).get("bumenchejian") + "评价记录", true);
}else {
writer.writeRow(new ArrayList<>());
}
<(3, StringUtils.join("单位:", (0).get("bumenchejian")), true);                            writer.writeRow(new ArrayList<>());
writer.setRowHeight(num + 2, 40);
layout(writer, titleList, num);
}else {
<(15, "评价记录", true);
<(3, StringUtils.join("单位:"), true);
writer.writeRow(new ArrayList<>());
writer.writeRow(new ArrayList<>());
layout(writer, num);
}
num += list.size() + 4;
setHeader(writer);
writer.write(list, true);
}
}
}
response.setContentType("application/vnd.ms-excel;charset=utf-8");
String name = new String("评价记录".getBytes("utf-8"), "iso-8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + name + ".xls");
response.setHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "fileName");            writer.flush(out, true);
// 关闭writer,释放内存
writer.close();
IoUtil.close(out);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void setHeader(ExcelWriter writer) {
Map<String, String> objectMap = new LinkedHashMap<>();
objectMap.put("xuhao", "序号");
objectMap.put("zuoyebuzhou", "作业步骤");
objectMap.put("weixianyuanhuoqianza", "危险源");
objectMap.put("zhuyaohouguo", "主要后果");
objectMap.put("gongchengcuoshi", "⼯程控制");
objectMap.put("guanlicuoshi", "管理措施");
objectMap.put("peixunjiaoyu", "培训教育");
objectMap.put("getifanghu", "个体防护");
objectMap.put("yingjichuzhi", "应急处置");
objectMap.put("lquzhi", "L");
objectMap.put("equzhi", "E");
objectMap.put("cquzhi", "C");
objectMap.put("dzhi7", "D");
objectMap.put("pingjiajibie", "F");
objectMap.put("guankongjibie", "FF");
objectMap.put("jianyixinzenggaijinc", "措施");
writer.setHeaderAlias(objectMap);
}
public static void layout(ExcelWriter writer, List<Map<String, Object>> list, Integer i) {        (1 + i, 1 + i, 4, 10,
StringUtils.join("风险点岗位:", (0).get("fengxiandiangangwei"))
.replace("[", "").replace("]", ""), true);
<(1 + i, 1 + i, 11, 15,
StringUtils.join("⼯作任务:", (0).get("fengxiandianmingchen")), true);        commonLayout(writer, i);
fastjson怎么用
}
public static void layout(ExcelWriter writer, Integer i) {
<(1 + i, 1 + i, 4, 10,
StringUtils.join("风险点岗位:")
.replace("[", "").replace("]", ""), true);
<(1 + i, 1 + i, 11, 15,
StringUtils.join("⼯作任务:"), true);
commonLayout(writer, i);
}
public static void commonLayout(ExcelWriter writer, Integer i) {
<(2 + i, 3 + i, 0, 0, "序号", true);
<(2 + i, 3 + i, 1, 1, "作业步骤", true);
<(2 + i, 3 + i, 2, 2, "危险源", true);
<(2 + i, 3 + i, 3, 3, "主要后果", true);
<(2 + i, 2 + i, 4, 8, "A", true);
<(2 + i, 3 + i, 9, 9, "L", true);
<(2 + i, 3 + i, 10, 10, "E", true);
<(2 + i, 3 + i, 11, 11, "C", true);
<(2 + i, 3 + i, 12, 12, "D", true);
<(2 + i, 3 + i, 13, 13, "F", true);
<(2 + i, 3 + i, 14, 14, "FF", true);
<(2 + i, 3 + i, 15, 15, "措施", true);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。