EasyExcel-Defaine-And-Export-With-Template-File
- Easyexcel(1-注解介绍与使用)
- Easyexcel(2-文件多Sheet、不同对象、多数据读取)
- Easyexcel(3-文件导出)
- Easyexcel(4-模板文件定义与导出)
- Easyexcel(5-自定义列宽)
- Easyexcel(6-单元格合并)
- Easyexcel(7-自定义样式)
- Easyexcel(8-通用工具类)
文件导出
获取 resources 目录下的文件,使用 withTemplate 获取文件流导出文件模板
@GetMapping("/download1")
public void download1(HttpServletResponse response) {
try (InputStream in = new ClassPathResource("测试.xls").getInputStream()) {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
EasyExcel.write(response.getOutputStream())
.withTemplate(in)
.sheet("sheet1")
.doWrite(Collections.emptyList());
} catch (Exception e) {
e.printStackTrace();
}
}
注意:获取 resources 目录下的文件需要在 maven 中添加以下配置,过滤对应的文件,防止编译生成后的 class 文件找不到对应的文件信息
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
对象填充导出
模板文件信息

@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
@ExcelProperty(value = "用户Id")
private Integer userId;
@ExcelProperty(value = "姓名")
private String name;
@ExcelProperty(value = "手机")
private String phone;
@ExcelProperty(value = "邮箱")
private String email;
@ExcelProperty(value = "创建时间")
private Date createTime;
}
@GetMapping("/download5")
public void download5(HttpServletResponse response) {
try (InputStream in = new ClassPathResource("测试3.xls").getInputStream()) {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("测试3", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
User user = new User(1, "张三", "12345678901", "zhangsan@qq.com", new Date());
EasyExcel.write(response.getOutputStream(), User.class)
.withTemplate(in)
.sheet("模板")
.doFill(user);
} catch (Exception e) {
e.printStackTrace();
}
}
**注意:**填充模板跟写文件使用的方法不一致,模板填充使用的方法是 doFill ,而不是 doWrite
导出文件内容

List 填充导出
对象导出
模板文件信息

@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
@ExcelProperty(value = "用户Id")
private Integer userId;
@ExcelProperty(value = "姓名")
private String name;
@ExcelProperty(value = "手机")
private String phone;
@ExcelProperty(value = "邮箱")
private String email;
@ExcelProperty(value = "创建时间")
private Date createTime;
}
@GetMapping("/download2")
public void download2(HttpServletResponse response) {
try (InputStream in = new ClassPathResource("测试.xls").getInputStream()) {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
List<User> userList = new ArrayList<>();
userList.add(new User(1, "张三", "12345678901", "zhangsan@qq.com", new Date()));
userList.add(new User(2, "李四", "12345678902", "lisi@qq.com", new Date()));
EasyExcel.write(response.getOutputStream(), User.class)
.withTemplate(in)
.sheet("模板")
.doFill(userList);
} catch (Exception e) {
e.printStackTrace();
}
}
导出文件内容

对象嵌套对象(默认不支持)
原因排查
模板文件信息

@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
@ExcelProperty(value = "用户Id")
private Integer userId;
@ExcelProperty(value = "姓名")
private String name;
@ExcelProperty(value = "手机")
private String phone;
@ExcelProperty(value = "邮箱")
private String email;
@ExcelProperty(value = "学生")
private Student stu;
@NoArgsConstructor
@AllArgsConstructor
@Data
public static class Student {
@ExcelProperty("姓名")
private String name;
@ExcelProperty("年龄")
private Integer age;
}
}
@GetMapping("/download3")
public void download3(HttpServletResponse response) {
try (InputStream in = new ClassPathResource("测试2.xls").getInputStream()) {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("测试2", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
List<User> userList = new ArrayList<>();
userList.add(new User(1, "张三", "12345678901", "zhangsan@qq.com", new User.Student("张三", 12)));
userList.add(new User(2, "李四", "12345678902", "lisi@qq.com", new User.Student("李四", 13)));
EasyExcel.write(response.getOutputStream(), User.class)
.withTemplate(in)
.sheet("模板")
.doFill(userList);
} catch (Exception e) {
e.printStackTrace();
}
}