参考资料:https://www.springcloud.cc/spring-boot.html
中文文档2:https://felord.cn/_doc/_springboot/2.1.5.RELEASE/_book/index.html
EasyExcel官网:https://alibaba-easyexcel.github.io/index.html
迁移到新网站:https://www.yuque.com/easyexcel/doc/easyexcel
1. 导入依赖
1 2 3 4 5 6 7 8 9 10
| <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.72</version> </dependency>
|
2. 写操作
2.1 pojo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelProperty; import lombok.Data;
import java.util.Date;
@Data public class DemoData { @ExcelProperty("字符串标题") private String string; @ExcelProperty("日期标题") private Date date; @ExcelProperty("数字标题") private Double doubleData;
@ExcelIgnore private String ignore; }
|
2.2 测试类
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
| import com.alibaba.excel.EasyExcel; import com.demo.pojo.DemoData; import org.junit.Test;
import java.util.ArrayList; import java.util.Date; import java.util.List;
public class TestEasyExcel { private List<DemoData> data() { List<DemoData> list = new ArrayList<DemoData>(); for (int i = 0; i < 10; i++) { DemoData data = new DemoData(); data.setString("字符串" + i); data.setDate(new Date()); data.setDoubleData(0.56); list.add(data); } return list; }
@Test public void simpleWrite() { String fileName = "D:/TestEasyExcel.xlsx"; EasyExcel.write(fileName, DemoData.class).sheet("sheet1").doWrite(data()); } }
|
3. 读操作
3.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 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
| import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.fastjson.JSON; import com.demo.pojo.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) { 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("存储数据库成功!"); } }
|
3.2 Dao
1 2 3 4 5 6 7 8 9 10 11 12
| import com.demo.pojo.DemoData;
import java.util.List;
public class DemoDAO { public void save(List<DemoData> list) { } }
|
3.3 测试类
1 2 3 4 5 6 7 8
| @Test public void simpleRead() { String fileName = "D:/TestEasyExcel.xlsx"; EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).sheet().doRead(); }
|