1.EasyExcel简单介绍
EasyExcel工具是阿里的一个操作excel的开源项目,对现有的POI框架进行性能优化,解决了大数据量时内存溢出的问题。同时封装的更加简单灵活,适合初学者上手。
maven依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.2</version>
</dependency>
首先创建类,表头对应的就是属性名
@Data
@ContentRowHeight(21)
@HeadRowHeight(30)
@ColumnWidth(15)
public class EvaluationStatistics {
@ExcelIgnore
private String id;
@ExcelProperty(value = {"班级名称"},index = 0)
private String name;
@ExcelProperty(value = {"培训天数"},index = 1)
private String projectDays;
@ExcelProperty(value = {"培训开始时间"},index = 2)
private String beginDate;
@ExcelProperty(value = {"培训结束时间"},index = 3)
private String endDate;
@ExcelProperty(value = {"学员人数"},index = 4)
private String planNum;
/*@ExcelProperty("学员等级")
private String planNum;*/
}
@ContentRowHeight(21) 内容行高
@HeadRowHeight(30) 头排高度
@ColumnWidth(15) 列宽@ExcelIgnore 不需要的的字段使用
@ExcelProperty({"职位名称"}) 对应表头
通过对象写入方式
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码
String excelName = URLEncoder.encode("班次课程信息", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + excelName + ExcelTypeEnum.XLSX.getValue());
EasyExcel.write(response.getOutputStream(), ShiftCourse.class).sheet("班次课程信息").doWrite(list); //list就是存储的数据
这只是其中用到的一种,
Alibaba Easy Excel - 简单、省内存的Java解析Excel工具 | 写Excel