【EasyExcel】—— 实现excel动态表头设置、多个sheet

引入jar

 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.1.0</version>
        </dependency>

代码


    public static void main(String[] args) {
        //选择存储地址
        String fileName = "/Users/qweasdzxc/Downloads/text.xlsx";

        // 创建ExcelWriterBuilder对象
        ExcelWriter excelWriter = EasyExcelFactory.write(fileName).build();

        //设置动态表头
        List<List<String>> head1 =new ArrayList<>();
        head1.add(Collections.singletonList("姓名"));
        head1.add(Collections.singletonList("年龄"));
        head1.add(Collections.singletonList("性别"));

        //填充数据
        List<List<Object>> data1 = new ArrayList<>();
        data1.add(Arrays.asList("张三", 20, "男"));
        data1.add(Arrays.asList("李四", 25, "女"));

        //放入sheet1中
        //automaticMergeHead 控制是否合并相同标题的单元格 false==不合并
        WriteSheet writeSheet = EasyExcelFactory.writerSheet(0, "学生信息1").head(head1).automaticMergeHead(false).build();
        excelWriter.write(data1, writeSheet);


        List<List<String>> head2 =new ArrayList<>();
        head2.add(Collections.singletonList("学科"));
        head2.add(Collections.singletonList("分数"));

        List<List<Object>> data2 = new ArrayList<>();
        data2.add(Arrays.asList("数学", 90));
        data2.add(Arrays.asList("英语", 85));

        //放入sheet2中
        writeSheet = EasyExcelFactory.writerSheet(1, "学生信息2").head(head2).build();
        excelWriter.write(data2, writeSheet);
        //关流
        excelWriter.finish();

    }

效果

在这里插入图片描述

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用EasyExcel实现动态调整Excel表头格式时,可以通过以下步骤进行操作: 1. 创建ExcelWriter对象,用于写入Excel文件。 2. 创建Sheet对象,用于设置表格的样式和内容。 3. 创建HeadStyle对象,用于设置表头的样式。 4. 创建HeadFont对象,用于设置表头的字体样式。 5. 创建CellStyle对象,用于设置单元格的样式。 6. 创建ContentStyle对象,用于设置内容单元格的样式。 7. 使用addHeadRow方法添加表头行,并设置表头样式。 8. 使用addContentRow方法添加内容行,并设置内容样式。 9. 使用finish方法将数据写入Excel文件并关闭ExcelWriter对象。 下面是一个示例代码,演示了如何使用EasyExcel动态调整Excel表头格式: ```java // 导入所需的类 import com.alibaba.excel.EasyExcel; import com.alibaba.excel.write.builder.ExcelWriterBuilder; import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; // 创建ExcelWriter对象 ExcelWriterBuilder excelWriterBuilder = EasyExcel.write("output.xlsx"); // 创建Sheet对象 ExcelWriterSheetBuilder sheetBuilder = excelWriterBuilder.sheet("Sheet1"); // 创建表头样式 WriteCellStyle headStyle = new WriteCellStyle(); WriteFont headFont = new WriteFont(); headFont.setFontName("Arial"); headFont.setFontHeightInPoints((short) 12); headFont.setBold(true); headStyle.setWriteFont(headFont); // 创建内容样式 WriteCellStyle contentStyle = new WriteCellStyle(); WriteFont contentFont = new WriteFont(); contentFont.setFontName("Arial"); contentFont.setFontHeightInPoints((short) 10); contentStyle.setWriteFont(contentFont); // 设置表头样式 sheetBuilder.headStyle(headStyle); // 设置内容样式 sheetBuilder.registerWriteHandler(new HorizontalCellStyleStrategy(contentStyle)); // 添加表头行 List<String> headList = Arrays.asList("姓名", "年龄", "性别"); sheetBuilder.head(headList); // 添加内容行 List<List<Object>> dataList = new ArrayList<>(); dataList.add(Arrays.asList("张三", 20, "男")); dataList.add(Arrays.asList("李四", 25, "女")); sheetBuilder.doWrite(dataList); // 关闭ExcelWriter对象 excelWriterBuilder.finish(); ``` 这样就可以根据需要动态调整Excel表头的格式了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值