引入依赖
<!--读取Excel数据-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
主要代码:
Map<String, Object> row1 = new LinkedHashMap<>();
row1.put("下单时间", "张三");
row1.put("支付单号", 23);
row1.put("订单号", 88.32);
row1.put("支付方式", true);
Map<String, Object> row = new LinkedHashMap<>();
row.put("下单时间", "张三");
row.put("支付单号", 23);
row.put("订单号", 88.32);
row.put("支付方式", true);
ArrayList<Map<String, Object>> rows = CollUtil.newArrayList(row1,row);
// 通过工具类创建writer
ExcelWriter writer = ExcelUtil.getWriter("d:/test.xlsx");
writer.setFreezePane(2);
//设置列宽
writer.setColumnWidth(-1, 13);
writer.setDefaultRowHeight(26);
StyleSet style = writer.getStyleSet();
CellStyle headCellStyle = style.getHeadCellStyle();
//设置内容字体
Font font = writer.createFont();
//设置字体
font.setFontName("宋体");
//加粗
font.setBold(true);
//设置标题字体大小
font.setFontHeightInPoints((short) 10);
headCellStyle.setFont(font);
writer.setStyleSet(style);
// 合并单元格后的标题行,使用默认标题样式
writer.merge(row1.size() - 1, "支出明细");
// 一次性写出内容,使用默认样式,强制输出标题
writer.write(rows, true);
// 关闭writer,释放内存
writer.close();