本文最后更新于:2023年11月9日 晚上
使用代码操作Excel文件(easyExcel)
EasyExcel文档:https://www.yuque.com/easyexcel/doc/easyexcel
1.Maven导入easyExcel包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <dependencies> <!--easyExcel--> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.6</version> </dependency> <!--日期格式化工具--> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.10.1</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency> <!--fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.71</version> </dependency> <!--测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
|
2.easyExcel进行基本的写入
1. 创建对应的实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.domain;
import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelProperty; import lombok.Data;
import java.util.Date;
@Data public class DemoData {
@ExcelProperty(value = "字符串标题", index = 0) private String stringName;
@ExcelProperty(value = "日期标题", index = 1) private Date dateName;
@ExcelProperty(value = "数字标题", index = 3) private Double doubleName;
@ExcelIgnore private int money; }
|
2. easyExcel用到的监听器 (下面代码是复制官方的过来用的)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| package com.utils;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.fastjson.JSON; import com.dao.DemoDAO; import com.domain.DemoData; import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.List;
public class DemoDataListener extends AnalysisEventListener<DemoData> { private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class);
private static final int BATCH_COUNT = 5; List<DemoData> list = new ArrayList<DemoData>();
private DemoDAO demoDAO;
public DemoDataListener() { demoDAO = new DemoDAO(); }
public DemoDataListener(DemoDAO demoDAO) { this.demoDAO = demoDAO; }
@Override public void invoke(DemoData data, AnalysisContext context) {
System.out.println(JSON.toJSONString(data));
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); list.add(data); if (list.size() >= BATCH_COUNT) { saveData(); list.clear(); } }
@Override public void doAfterAllAnalysed(AnalysisContext context) { saveData(); LOGGER.info("所有数据解析完成!"); }
private void saveData() { LOGGER.info("{}条数据,开始存储数据库!", list.size()); demoDAO.save(list); LOGGER.info("存储数据库成功!"); } }
|
如果需要存入数据库(添加Dao类和方法) 监听器会调用
1 2 3 4
| public class DemoDAO { public void save(List<DemoData> list) { }
|
3. 编写代码:读和写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| 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.domain.DemoData; import com.utils.DemoDataListener; import org.junit.Test;
import java.io.File; import java.util.ArrayList; import java.util.Date; import java.util.List;
public class easyTest {
final String PATH = "D:\\IntelliJ IDEA 2019.1_File\\POIandEasyExcel\\easyExcel\\";
public List<DemoData> data() {
List<DemoData> list = new ArrayList<DemoData>();
for (int x = 1; x < 11; x++) { DemoData data = new DemoData(); data.setStringName("字符串" + x); data.setDateName(new Date()); data.setDoubleName((double) x); data.setMoney(x);
list.add(data); } return list; }
@Test public void easyExcelWrite() {
String fileName = PATH + "easyExcelTest.xlsx";
EasyExcel.write(fileName, DemoData.class).sheet("easyExcel模板").doWrite(data());
ExcelWriter excelWriter = null; try { excelWriter = EasyExcel.write(fileName, DemoData.class).build(); WriteSheet writeSheet = EasyExcel.writerSheet("easyExcel模板").build(); excelWriter.write(data(), writeSheet); } finally { if (excelWriter != null) { excelWriter.finish(); } } }
@Test public void easyExcelRead() {
String fileName = PATH + "easyExcelTest.xlsx";
ExcelReader excelReader = null; try { excelReader = EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).build(); ReadSheet readSheet = EasyExcel.readSheet(0).build(); excelReader.read(readSheet); } finally { if (excelReader != null) { excelReader.finish(); } } } }
|
easyExcel就不过多介绍了,因为文档里面非常清楚,已经有写好的demo。相比POI,easyExcel操作更简单,代码量更少
easyExcel内部还是会使用 POI
看似美好的东西,往往藏着陷阱。