1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| package com.ruben;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.poi.excel.ExcelUtil; import cn.hutool.poi.excel.ExcelWriter;
import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.Map;
public class ExcelDemo { public static void main(String[] args) {
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);
ArrayList<Map<String, Object>> rows2 = CollUtil.newArrayList(row2);
ExcelWriter writer = ExcelUtil.getWriter("d:/tmp/writeMapTest.xlsx");
writer.setSheet(0).renameSheet("例子sheet1").merge(row1.size() - 1, "一班成绩单").write(rows, true); writer.setSheet("例子sheet2").merge(row2.size() - 1, "二班成绩单").write(rows2, true);
writer.close(); } }
|