1.添加依赖

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.4.7</version>
</dependency>

2.(将行列对象写出到Excel)

我们先定义一个嵌套的List,List的元素也是一个List,内层的一个List代表一行数据,每行都有4个单元格,最终list对象代表多行数据。

public class test{
    List<String> row1 = CollUtil.newArrayList("aa", "bb", "cc", "dd");
    List<String> row2 = CollUtil.newArrayList("aa1", "bb1", "cc1", "dd1");
    List<String> row3 = CollUtil.newArrayList("aa2", "bb2", "cc2", "dd2");
    List<String> row4 = CollUtil.newArrayList("aa3", "bb3", "cc3", "dd3");
    List<String> row5 = CollUtil.newArrayList("aa4", "bb4", "cc4", "dd4");
    
    public static void main(String[] args) {
        List<List<String>> rows = CollUtil.newArrayList(row1, row2, row3, row4, row5);
        writerExcelWriter writer = ExcelUtil.getWriter("d:/writeTest.xlsx");//通过构造方法创建writer//ExcelWriter writer = new ExcelWriter("d:/writeTest.xls");
//跳过当前行,既第一行,非必须,在此演示用
        writer.passCurrentRow();
//合并单元格后的标题行,使用默认标题样式
        writer.merge(row1.size() - 1, "测试标题");//一次性写出内容,强制输出标题
        writer.write(rows, true);//关闭writer,释放内存
        writer.close();
    }

}

3.(将Map写出到Excel)

public class test{
    Map<String, Object> row1 = new LinkedHashMap<>();
row1.put("姓名", "张三");
row1.put("年龄", 23);
row1.put("成绩", 88.32);
row1.put("是否合格", true);
row1.put("考试日期", DateUtil.date());
    Map<String, Object> row2 = new LinkedHashMap<>();
row2.put("姓名", "李四");
row2.put("年龄", 33);
row2.put("成绩", 59.50);
row2.put("是否合格", false);
row2.put("考试日期", DateUtil.date());
    ArrayList<Map<String, Object>> rows = CollUtil.newArrayList(row1, row2);
    Copy to clipboardErrorCopied

    public static void main(String[] args) {
        // 通过工具类创建
        writerExcelWriter writer = ExcelUtil.getWriter("d:/writeMapTest.xlsx");// 合并单元格后的标题行,使用默认标题样式
        writer.merge(rows.size() - 1, "一班成绩单");// 一次性写出内容,使用默认样式,强制输出标题
        writer.write(rows, true);// 关闭writer,释放内存
        writer.close();Copy to clipboardErrorCopied
    }

}

Q.E.D.