简介:Hutool是一款十分好用的开发工具集,里面包含了大部分日常开发常用的工具,使用简单方便,可以大大提升日常开发效率,十分推荐大家使用。这里简单总结一下基于Hutool的Excel使用。
一、Hutool依赖
<!-- Excel导出、导入-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.5</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
public static void main(String[] args) {
ExcelWriter writer = null;
try {
writer = ExcelUtil.getWriter(true);
// 标题
writer.merge(0, 1, 0, 0, "序号", true);
writer.merge(0, 1, 1, 1, "合同编号", true);
writer.merge(0, 1, 2, 2, "合同名称", true);
writer.merge(0, 1, 3, 3, "承包商", true);
writer.merge(0, 0, 4, 5, "合同投资", true);
writer.merge(0, 1, 6, 6, "动态投资", true);
writer.merge(0, 1, 7, 7, "估算投资", true);
writer.merge(0, 1, 8, 8, "概算投资", true);
writer.merge(0, 0, 9, 10, "工程预算", true);
writer.merge(0, 0, 11, 12, "工程结(决)算", true);
writer.merge(0, 0, 13, 14, "工程审计金额", true);
writer.writeCellValue(4, 1, "总投资");
// 设置单元格为表头默认样式,不设置当前单元格样式会与表头不一致
writer.setStyle(StyleUtil.createHeadCellStyle(writer.getWorkbook()), 4, 1);
writer.writeCellValue(5, 1, "暂定金额");
writer.setStyle(StyleUtil.createHeadCellStyle(writer.getWorkbook()), 5, 1);
writer.writeCellValue(9, 1, "工程预算");
writer.setStyle(StyleUtil.createHeadCellStyle(writer.getWorkbook()), 9, 1);
writer.writeCellValue(10, 1, "招标控制价");
writer.setStyle(StyleUtil.createHeadCellStyle(writer.getWorkbook()), 10, 1);
writer.writeCellValue(11, 1, "结算金额");
writer.setStyle(StyleUtil.createHeadCellStyle(writer.getWorkbook()), 11, 1);
writer.writeCellValue(12, 1, "决算金额");
writer.setStyle(StyleUtil.createHeadCellStyle(writer.getWorkbook()), 12, 1);
writer.writeCellValue(13, 1, "结算审计");
writer.setStyle(StyleUtil.createHeadCellStyle(writer.getWorkbook()), 13, 1);
writer.writeCellValue(14, 1, "决算审计");
writer.setStyle(StyleUtil.createHeadCellStyle(writer.getWorkbook()), 14, 1);
writer.passCurrentRow();
writer.passCurrentRow();
List<String> row1 = CollUtil.newArrayList("一","","建安投资","承包商1","23","30","54.2","23","22","33","124","203","345","55","5555");
List<String> row2 = CollUtil.newArrayList("二","","建安投资","承包商1","23","30","54.2","23","22","33","124","203","345","55","5555");
List<List<String>> rows2 = CollUtil.newArrayList(row1,row2);
writer.write(rows2, false);
// 写出流
writer.flush(new FileOutputStream(new File("d:/复杂表头.xlsx")));
} catch (Exception e) {
} finally {
if (writer != null) {
writer.close();
}
}
}