GitHub - alibaba/easyexcel: 快速、简洁、解决大文件内存溢出的java处理Excel工具
EasyExcel官方文档 - 基于Java的Excel处理工具 | Easy Excel
EasyExcel是一个基于Java的、快速、简洁、解决大文件内存溢出的Excel处理工具。 他能让你在不用考虑性能、内存的等因素的情况下,快速完成Excel的读、写等功能。
Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。 easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析依然需要100M左右内存,改用easyexcel可以降低到几M,并且再大的excel也不会出现内存溢出;03版依赖POI的sax模式,在上层做了模型转换的封装,让使用者更加简单方便
EasyExcel
com.alibaba
easyexcel
3.2.1
ch.qos.logback
logback-classic
1.2.3
这个错误通常是由于您的项目使用了 SLF4J 日志框架(Simple Logging Facade for Java),但无法找到对应的日志实现。
SLF4J 只是一个日志框架,它并不提供具体的日志实现。
要解决这个问题,您需要添加一个 SLF4J 的日志实现,例如 Logback 或 Log4j。
这些实现可以将 SLF4J 的 API 转换为特定的日志系统的 API,并提供日志记录功能。
如果使用的是 Maven,则可以通过以上方式添加 Logback 日志实现:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
@ExcelProperty("序号")
private Integer id;
@ExcelProperty("姓名")
private String name;
@ExcelProperty("年龄")
private Integer age;
@ExcelProperty("生日")
private Date birthday;
}
@Test
void test1() {
List userList = new ArrayList<>();
// 添加用户信息
userList.add(new User(1, "张三", 23, new Date()));
userList.add(new User(2, "李四", 24, new Date()));
userList.add(new User(3, "王五", 25, new Date()));
userList.add(new User(4, "赵六", 26, new Date()));
// 输出目录
String fileName = "/Users/whitecamellia/Desktop/test/用户信息表1.xlsx";
EasyExcel.write(fileName,User.class).sheet("用户信息1").doWrite(userList);
}
@Test
void test2() {
List userList = new ArrayList<>();
// 添加用户信息
userList.add(new User(1, "张三", 23, new Date()));
userList.add(new User(2, "李四", 24, new Date()));
userList.add(new User(3, "王五", 25, new Date()));
userList.add(new User(4, "赵六", 26, new Date()));
// 输出目录
String fileName = "/Users/whitecamellia/Desktop/test/用户信息表2.xlsx";
// 创建excelWriter对象
ExcelWriter excelWriter = EasyExcel.write(fileName, User.class).build();
// 创建writeSheet对象
WriteSheet writeSheet = EasyExcel.writerSheet("用户信息2").build();
// 写入数据
excelWriter.write(userList,writeSheet);
// 关闭流操作
excelWriter.finish();
}
@Test
void test3() {
List userList = new ArrayList<>();
// 添加用户信息
userList.add(new User(1, "张三", 23, new Date()));
userList.add(new User(2, "李四", 24, new Date()));
userList.add(new User(3, "王五", 25, new Date()));
userList.add(new User(4, "赵六", 26, new Date()));
// 输出目录
String fileName = "/Users/whitecamellia/Desktop/test/用户信息表3.xlsx";
Set set = new HashSet<>();
set.add("age");
set.add("birthday");
EasyExcel.write(fileName,User.class)
// 排除列
.excludeColumnFieldNames(set)
.sheet("用户信息3")
.doWrite(userList);
}
@Test
void test4() {
List userList = new ArrayList<>();
// 添加用户信息
userList.add(new User(1, "张三", 23, new Date()));
userList.add(new User(2, "李四", 24, new Date()));
userList.add(new User(3, "王五", 25, new Date()));
userList.add(new User(4, "赵六", 26, new Date()));
// 输出目录
String fileName = "/Users/whitecamellia/Desktop/test/用户信息表4.xlsx";
Set set = new HashSet<>();
set.add("age");
set.add("birthday");
EasyExcel.write(fileName,User.class)
// 指定列
.includeColumnFieldNames(set)
.sheet("用户信息4")
.doWrite(userList);
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
@ExcelProperty(value = "序号",index = 0)
private Integer id;
@ExcelProperty(value = "姓名",index = 1)
private String name;
@ExcelProperty(value = "年龄",index = 3)
private Integer age;
@ExcelProperty(value = "生日",index = 2)
private Date birthday;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
@ExcelProperty({"基本信息","编号"})
private Integer id;
@ExcelProperty({"基本信息","姓名"})
private String name;
@ExcelProperty({"基本信息","年龄"})
private Integer age;
@ExcelProperty({"日期","入职"})
private Date entry;
@ExcelProperty({"日期","离职"})
private Date leave;
}
@Test
void test5() {
List empList = new ArrayList<>();
// 添加用户信息
empList.add(new Emp(1, "张三", 23, new Date(),new Date()));
empList.add(new Emp(2, "李四", 24, new Date(),new Date()));
empList.add(new Emp(3, "王五", 25, new Date(),new Date()));
empList.add(new Emp(4, "赵六", 26, new Date(),new Date()));
// 输出目录
String fileName = "/Users/whitecamellia/Desktop/test/员工信息表1.xlsx";
EasyExcel.write(fileName,Emp.class).sheet("员工信息表1").doWrite(empList);
}
@Test
void test6 () {
List userList = new ArrayList<>();
// 添加用户信息
userList.add(new User(1, "张三", 23, new Date()));
userList.add(new User(2, "李四", 24, new Date()));
userList.add(new User(3, "王五", 25, new Date()));
userList.add(new User(4, "赵六", 26, new Date()));
// 输出目录
String fileName = "/Users/whitecamellia/Desktop/test/用户信息表5.xlsx";
// 创建excelWriter对象
ExcelWriter excelWriter = EasyExcel.write(fileName, User.class).build();
// 创建writeSheet对象
WriteSheet writeSheet = EasyExcel.writerSheet("用户信息5").build();
// 写10次
for (int i = 0; i < 10; i++) {
excelWriter.write(userList,writeSheet);
}
// 关闭流
excelWriter.finish();
}
@Test
void test7 () {
List userList = new ArrayList<>();
// 添加用户信息
userList.add(new User(1, "张三", 23, new Date()));
userList.add(new User(2, "李四", 24, new Date()));
userList.add(new User(3, "王五", 25, new Date()));
userList.add(new User(4, "赵六", 26, new Date()));
// 输出目录
String fileName = "/Users/whitecamellia/Desktop/test/用户信息表6.xlsx";
// 创建excelWriter对象
ExcelWriter excelWriter = EasyExcel.write(fileName, User.class).build();
// 写10次
for (int i = 1; i <= 10; i++) {
// 创建writeSheet对象
WriteSheet writeSheet = EasyExcel.writerSheet("用户信息"+i).build();
excelWriter.write(userList,writeSheet);
}
// 关闭流
excelWriter.finish();
}
@NumberFormat("#.##")
@ExcelProperty("薪资")
日期格式化
@DateTimeFormat("yyyy年MM月dd日")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
@ExcelProperty({"基本信息","编号"})
private Integer id;
@ExcelProperty({"基本信息","姓名"})
private String name;
@ExcelProperty({"基本信息","年龄"})
private Integer age;
@DateTimeFormat("yyyy年MM月dd日")
@ExcelProperty({"日期","入职"})
private Date entry;
@DateTimeFormat("yyyy年MM月dd日")
@ExcelProperty({"日期","离职"})
private Date leave;
@NumberFormat("#.##")
@ExcelProperty({"薪资"})
private Double salary;
}
@Test
void test8() {
List empList = new ArrayList<>();
// 添加用户信息
empList.add(new Emp(1, "张三", 23, new Date(),new Date(),8234.333));
empList.add(new Emp(2, "李四", 24, new Date(),new Date(),8234.333));
empList.add(new Emp(3, "王五", 25, new Date(),new Date(),8234.333));
empList.add(new Emp(4, "赵六", 26, new Date(),new Date(),8234.333));
// 输出目录
String fileName = "/Users/whitecamellia/Desktop/test/员工信息表2.xlsx";
EasyExcel.write(fileName,Emp.class).sheet("员工信息表2").doWrite(empList);
}
ImageData
@Data
public class ImageData {
// 抽象文件表示图片
@ExcelProperty(value = "图1")
private File file;
// 输入流表示一个图片
@ExcelProperty(value = "图2")
private InputStream inputStream;
/** 如果string类型保存一个图片,必须使用StringImageConverter转换器*/
@ExcelProperty(converter = StringImageConverter.class,value = "图3")
private String string;
// 二进制数组表示图片
@ExcelProperty(value = "图4")
private byte[] byteArray;
// 网络链接表示图片 */
@ExcelProperty(value = "图5")
private URL url;
}
@Test
void test9 () throws Exception {
String fileName = "/Users/whitecamellia/Desktop/test/xxx.xlsx";
String imageLocalUrl = "/Users/whitecamellia/资料/xx/xxx/xxxx .jpg";
String imageUrl = "https://www.mianfeiwendang.com/pic/1da74e363276b769ac770539/4-810-jpg_6-1080-0-0-1080.jpg";
List list = new ArrayList<>();
ImageData imageData = new ImageData();
imageData.setFile(new File(imageLocalUrl));
imageData.setInputStream(new FileInputStream(imageLocalUrl));
imageData.setString(imageLocalUrl);
byte[] b = new byte[(int) new File(imageLocalUrl).length()];
FileInputStream fileInputStream = new FileInputStream(imageLocalUrl);
fileInputStream.read(b);
imageData.setByteArray(b);
imageData.setUrl(new URL(imageUrl));
list.add(imageData);
EasyExcel.write(fileName, ImageData.class).sheet("图片").doWrite(list);
}
@ContentRowHeight(2000)//设置内容高度
@HeadRowHeight(50)//设置标题高度
@ColumnWidth(45)//设置列宽
@Data
public class ImageData {
// 抽象文件表示图片
@ColumnWidth(255)//设置列宽
@ExcelProperty(value = "图1")
private File file;
....
}
@Test
void test9 () {
.....
}
StyleDate
@HeadRowHeight(50) //设置标题高度
// 头背景设置为红色(10) enum IndexedColors
@HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND,fillForegroundColor = 10)
// 头字体 设置为40
@HeadFontStyle(fontHeightInPoints = 40)
// 内容背景设置为红色(10)
@ContentStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND,fillForegroundColor = 14)
// 内容字体 设置为30
@ContentFontStyle(fontHeightInPoints = 30)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
@ExcelProperty({"基本信息","编号"})
private Integer id;
@ExcelProperty({"基本信息","姓名"})
private String name;
@ExcelProperty({"基本信息","年龄"})
private Integer age;
@DateTimeFormat("yyyy年MM月dd日")
@ExcelProperty({"日期","入职"})
private Date entry;
@DateTimeFormat("yyyy年MM月dd日")
@ExcelProperty({"日期","离职"})
private Date leave;
@NumberFormat("#.##")
@ExcelProperty({"薪资"})
private Double salary;
}
@Test
void test8 () {
.....
}
@OnceAbsoluteMerge(firstRowIndex = 2, lastRowIndex = 3, firstColumnIndex = 2, lastColumnIndex = 3)
public class Emp {
@ExcelProperty({"基本信息","编号"})
private Integer id;
@ExcelProperty({"基本信息","姓名"})
.....
}
@Test
void test8 () {
.....
}
@Test
void test1 () {
String fileName = "/Users/whitecamellia/Desktop/test/用户信息表1.xlsx";
EasyExcel.read(fileName, User.class, new AnalysisEventListener() {
@Override
public void invoke(User user, AnalysisContext analysisContext) {
// 每读一行 执行一次 ,可以在这里执行db操作,这里读取时尽量不要在User上加index
System.out.println("读取到的数据是:" + user);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
// 读完数据 最后做一次
System.out.println("全部读取完毕!");
}
}).sheet().doRead();
}
DemoData
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Integer id;
private String name;
private Integer age;
private Date birthday;
}
@Test
void test2 () {
String fileName = "/Users/whitecamellia/Desktop/test/用户信息表1.xlsx";
ExcelReader excelReader = EasyExcel.read(fileName, User.class, new AnalysisEventListener() {
@Override
public void invoke(User user, AnalysisContext analysisContext) {
// 每读一行 执行一次 ,可以在这里执行db操作,这里读取时尽量不要在User上加index
System.out.println("读取到的数据是:" + user);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
// 读完数据 最后做一次
System.out.println("全部读取完毕!");
}
}).build();
ReadSheet readSheet = EasyExcel.readSheet(0).build();
excelReader.read(readSheet);
//关闭流资源,在读取文件时,会创建临时文件,如果不关闭,磁盘会挂掉。
excelReader.finish();
}
通过名称或者下标读取列
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
@ExcelProperty(value = "年龄")
private Integer age;
@ExcelProperty(value = "生日")
private Date birthday;
@ExcelProperty(value = "序号")
private Integer id;
@ExcelProperty(value = "姓名")
private String name;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
@ExcelProperty(index = 2)
private Integer age;
@ExcelProperty(index = 3)
private Date birthday;
@ExcelProperty(index = 0)
private Integer id;
@ExcelProperty(index = 1)
private String name;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
@ExcelProperty(index = 2)
private Integer age;
@ExcelProperty(index = 3)
@DateTimeFormat("yyyy年MM月dd日 HH:mm:ss")
private Date birthday;
@ExcelProperty(index = 0)
private Integer id;
@ExcelProperty(index = 1)
private String name;
@ExcelProperty(index = 4)
@NumberFormat("#.##") //小数点后保留2位,注意,必须用String类型,不可以用Double类型
private String salary;
}
doReadAll();
sheet(0).doRead();
@Test
void test4 () {
String fileName = "/Users/whitecamellia/Desktop/test/用户信息表1.xlsx";
EasyExcel.read(fileName, User.class, new AnalysisEventListener() {
@Override
public void invoke(User user, AnalysisContext analysisContext) {
// 每读一行 执行一次 ,可以在这里执行db操作,这里读取时尽量不要在User上加index
System.out.println("读取到的数据是:" + user);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
// 读完数据 最后做一次
System.out.println("全部读取完毕!");
}
}).doReadAll();
}
@Test
void test5 () {
String fileName = "/Users/whitecamellia/Desktop/test/用户信息表1.xlsx";
// 读取文件
ExcelReader excelReader = EasyExcel.read(fileName).build();
// 构建sheet0
ReadSheet sheet0 = EasyExcel.readSheet(0).head(User.class).registerReadListener(new AnalysisEventListener() {
@Override
public void invoke(User user, AnalysisContext analysisContext) {
System.out.println("sheet0读取到的数据是:" + user);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
System.out.println("sheet0全部读取完毕!");
}
}).build();
// 构建sheet1
ReadSheet sheet1 = EasyExcel.readSheet(1).head(User.class).registerReadListener(new AnalysisEventListener() {
@Override
public void invoke(User user, AnalysisContext analysisContext) {
System.out.println("sheet1读取到的数据是:" + user);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
System.out.println("sheet1全部读取完毕!");
}
}).build();
// 读取sheet0,sheet1
excelReader.read(sheet0,sheet1);
//关闭流资源,在读取文件时,会创建临时文件,如果不关闭,磁盘会挂掉。
excelReader.finish();
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class FillData {
private String name;
private Integer age;
private String clazz;
private double score;
private String desc;
}
@Test
void test6 () {
// 1.根据哪个模版进行填充
String templateName = "/Users/whitecamellia/Desktop/test/template.xlsx";
// 2.填充完成之后的Excel
String fullFileName = "/Users/whitecamellia/Desktop/test/fullFileName.xlsx";
// 3.构建填充数据
FillData fillData = new FillData("小明",20,"3年2班",85.5,"这次没考好!");
// 4.进行填充
EasyExcel.write(fullFileName).withTemplate(templateName).sheet().doFill(fillData);
}
@Test
void test7 () {
// 1.根据哪个模版进行填充
String templateName = "/Users/whitecamellia/Desktop/test/template.xlsx";
// 2.填充完成之后的Excel
String fullFileName = "/Users/whitecamellia/Desktop/test/fullFileName.xlsx";
// 3.构建填充数据
FillData fillData1 = new FillData("小明",20,"3年2班",85.5,"这次没考好!");
FillData fillData2 = new FillData("小红",21,"3年3班",99.5,"这次还不错!");
List list = new ArrayList<>();
list.add(fillData1);
list.add(fillData2);
// 4.进行填充
EasyExcel.write(fullFileName).withTemplate(templateName).sheet(0).doFill(list);
}