使用EasyExcel实现简单的Excel文件读写
EasyExcel官方网站:https://easyexcel.opensource.alibaba.com/
引入依赖
<!-- EasyExcel依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.1</version>
</dependency>
创建一个实体类操作数据,用@Data简化代码
package com.zeromemos.entity;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
@Data
public class ExcelDemoData {
//设置表头名称
@ExcelProperty("编号")
private int id;
//设置表头名称
@ExcelProperty("姓名")
private String name;
}
创建读取操作的监听器ExcelListener,可以定义一些读取时候的操作,这边直接放工具类包里。
package com.zeromemos.utils;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zeromemos.entity.ExcelDemoData;
import java.util.ArrayList;
import java.util.Map;
public class ExcelListener extends AnalysisEventListener<ExcelDemoData> {
ArrayList<ExcelDemoData> list = new ArrayList<>();
//一行一行去读取excel内容
@Override
public void invoke(ExcelDemoData data, AnalysisContext analysisContext) {
System.out.println("***" + data);
list.add(data);
}
//读取excel表头信息
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
System.out.println("表头信息" + headMap);
}
//读取完成后执行
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
System.out.println("读取完成");
}
}
直接放测试包里进行测试
run1/2是两种写出到excel的写法,run3/4是两种读excel数据的写法
package code;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.zeromemos.entity.ExcelDemoData;
import com.zeromemos.utils.ExcelListener;
import org.junit.Test;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class ExcelTest {
//创建10个测试数据
private static List<ExcelDemoData> data(){
List<ExcelDemoData> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
ExcelDemoData data = new ExcelDemoData();
data.setId(i);
data.setName("小" + i);
list.add(data);
}
return list;
}
@Test
public void run1(){
//指定文件保存目录
String fileName = "C:\\Users\\admin\\Desktop\\demo.xlsx";
//这里需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName, ExcelDemoData.class)
.sheet("写入第一个sheet测试")
.doWrite(data());
}
@Test
public void run2(){
//指定文件保存目录
String fileName = "C:\\Users\\admin\\Desktop\\demo.xlsx";
//这里需要指定写用哪个class去写
ExcelWriter excelWriter = EasyExcel.write(fileName, ExcelDemoData.class).build();
//然后写到第一个sheet,名字为模板
WriteSheet writeSheet = EasyExcel.writerSheet("写入第一个sheet测试").build();
//执行写操作
excelWriter.write(data(), writeSheet);
//千万别忘记finish会帮忙关闭流
excelWriter.finish();
}
@Test
public void run3(){
//指定读取文件
String fileName = "C:\\Users\\admin\\Desktop\\demo.xlsx";
//指定读用哪个class去读,然后读取第一个sheet文件流会自动关闭
EasyExcel.read(fileName, ExcelDemoData.class, new ExcelListener())
.sheet().doRead();
}
@Test
public void run4(){
//指定读取文件
String fileName = "C:\\Users\\admin\\Desktop\\demo.xlsx";
try {
//创建输入流
InputStream in = new BufferedInputStream(new FileInputStream(fileName));
//指定用哪个class去读,和监听器
ExcelReader excelReader = EasyExcel.read(in, ExcelDemoData.class, new ExcelListener()).build();
//指定第一个sheet文件
ReadSheet readSheet = EasyExcel.readSheet(0).build();
//执行读
excelReader.read(readSheet);
//关闭流
excelReader.finish();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
下面生成的excel文件和读取是打印的效果,打印代码是在监听器ExcelListener里写的