1.所需依赖
注意:hutool、poi版本不匹配会导致异常
<parent>
<groupId></groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId></groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.4</version>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
文件创建、内容导入
@Data
public class User {
private String id;
private String name;
private Integer age;
private Date birthday;
public User() {
}
public User(String id, String name, Integer age, Date birthday) {
= id;
= name;
= age;
= birthday;
}
}
#控制列顺序,需要使用LinkedHashMap,不能使用HashMap
Map<String, String> headers = new LinkedHashMap<>();
("id","编号");
("name","姓名");
("age","年龄");
("birthday","出生日期");
ExcelWriter writer = ("d:/");
#设置表格头部别名
(headers);
(true);
List<User> userList = new ArrayList<>();
(new User("1","小明",20,new Date()));
(new User("2","小华",20,new Date()));
(userList,true);
#必须使用close或flush文件才能被创建、数据才能被导入
();
3.表格样式设置
Workbook workbook = ();
#设置全局样式
StyleSet styleSet = new StyleSet(workbook);
#设置字体样式
Font font = ();
#是否加粗
(true);
#颜色
(Font.COLOR_RED);
#字体
("微软雅黑");
#斜体
(true);
(font,true);
#设置全局行样式
CellStyle cellStyle = ();
();
();
(styleSet);
#头部样式
CellStyle headCellStyle = ();
();
();
(font);