hutool ExcelUtil导出excel二级表头

简介:Hutool是一款十分好用的开发工具集,里面包含了大部分日常开发常用的工具,使用简单方便,可以大大提升日常开发效率,十分推荐大家使用。这里简单总结一下基于Hutool的Excel使用。

一、Hutool依赖

java 复制代码
    <!-- 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>
java 复制代码
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();
        }
    }
}
相关推荐
正在走向自律1 小时前
阿里云ESC服务器一次性全部迁移到另一个ESC
服务器·阿里云·云计算
gywl2 小时前
openEuler VM虚拟机操作(期末考试)
linux·服务器·网络·windows·http·centos
WTT00113 小时前
2024楚慧杯WP
大数据·运维·网络·安全·web安全·ctf
了一li3 小时前
Qt中的QProcess与Boost.Interprocess:实现多进程编程
服务器·数据库·qt
杨德杰3 小时前
QT网络(一):主机信息查询
网络·qt
日记跟新中3 小时前
Ubuntu20.04 修改root密码
linux·运维·服务器
唐小旭3 小时前
服务器建立-错误:pyenv环境建立后python版本不对
运维·服务器·python
明 庭3 小时前
Ubuntu下通过Docker部署NGINX服务器
服务器·ubuntu·docker
BUG 4044 小时前
Linux——Shell
linux·运维·服务器