hutool ExcelUtil导出excel二级表头

简介: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();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值