当前位置:网站首页>Easyexcel read excel simple demo
Easyexcel read excel simple demo
2022-06-13 00:25:00 【Survivors with dreams behind their backs】
Maven
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.10</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
Excel
DemoData
package com.demo.test.easyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.format.NumberFormat;
/** * Data entity class * * @author zhao.hualuo * Create at 2021/5/31 */
@lombok.Data
public class DemoData {
@ExcelProperty(" String title ")
private String string;
@ExcelProperty(" Date title ")
@DateTimeFormat("yyyy year MM month dd Japan ")
private String dateStr;
@ExcelProperty(" Digital title ")
@NumberFormat("#.##%")
private String doubleData;
}
DemoDataListener
package com.demo.test.easyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** * Monitor * DemoDataListener Can not be spring management , Every time you read excel Both new, And then it uses spring You can construct methods to pass in * * @author zhao.hualuo * Create at 2021/5/31 */
public class DemoDataListener extends AnalysisEventListener {
private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class);
int index = 0;
/** * Every data parsing will call */
@Override
public void invoke(Object object, AnalysisContext context) {
DemoData demoData = JSONObject.parseObject(JSON.toJSONString(object), DemoData.class);
// Do business logic processing
index ++;
LOGGER.info(" Parse to a piece of data {}:{}", index, demoData.toString());
}
/** * After all data analysis is completed Will call this one */
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
// Finishing work
LOGGER.info(" All data analysis completed !" + index);
}
}
DemoMain
package com.demo.test.easyExcel;
import com.alibaba.excel.EasyExcel;
/** * Execute the main function * * @author zhao.hualuo * Create at 2021/5/31 */
public class DemoMain {
public static void main(String[] args) {
String fileName = "D:\\Documents\\person\\zhao.hualuo\\ desktop \\demo.xlsx";
EasyExcel.read(fileName, DemoData.class,new DemoDataListener()).sheet().doRead();
}
}
Execution results
15:33:32.070 [main] INFO com.demo.test.easyExcel.DemoDataListener - Parse to a piece of data 1:DemoData(string=aaa, dateStr=2020 year 01 month 01 Japan , doubleData=100%)
15:33:32.072 [main] INFO com.demo.test.easyExcel.DemoDataListener - Parse to a piece of data 2:DemoData(string=bbb, dateStr=2020 year 01 month 02 Japan , doubleData=200%)
15:33:32.073 [main] INFO com.demo.test.easyExcel.DemoDataListener - Parse to a piece of data 3:DemoData(string=ccc, dateStr=2020 year 01 month 03 Japan , doubleData=300%)
15:33:32.075 [main] INFO com.demo.test.easyExcel.DemoDataListener - Parse to a piece of data 4:DemoData(string=ddd, dateStr=2020 year 01 month 04 Japan , doubleData=400%)
15:33:32.077 [main] INFO com.demo.test.easyExcel.DemoDataListener - Parse to a piece of data 5:DemoData(string=eee, dateStr=2020 year 01 month 05 Japan , doubleData=500%)
15:33:32.078 [main] INFO com.demo.test.easyExcel.DemoDataListener - Parse to a piece of data 6:DemoData(string=fff, dateStr=2020 year 01 month 06 Japan , doubleData=600%)
15:33:32.081 [main] INFO com.demo.test.easyExcel.DemoDataListener - Parse to a piece of data 7:DemoData(string=ggg, dateStr=2020 year 01 month 07 Japan , doubleData=700%)
15:33:32.083 [main] INFO com.demo.test.easyExcel.DemoDataListener - Parse to a piece of data 8:DemoData(string=hhh, dateStr=2020 year 01 month 08 Japan , doubleData=800%)
15:33:32.083 [main] INFO com.demo.test.easyExcel.DemoDataListener - Parse to a piece of data 9:DemoData(string=iii, dateStr=2020 year 01 month 09 Japan , doubleData=900%)
15:33:32.084 [main] INFO com.demo.test.easyExcel.DemoDataListener - Parse to a piece of data 10:DemoData(string=jjj, dateStr=2020 year 01 month 10 Japan , doubleData=1000%)
15:33:32.085 [main] INFO com.demo.test.easyExcel.DemoDataListener - All data analysis completed !10
边栏推荐
- La différence entre philosophie et littérature
- Test platform series (97) perfect the case part
- 6.824 Lab 3B: Fault-tolerant Key/Value Service
- Installation of IK word breaker
- Five mock technologies of go
- 睡前小故事之MySQL起源
- 【Matlab】基础运算
- [MRCTF2020]Ez_bypass --BUUCTF
- 【HCIE论述】STP-A
- C language standard IO, for example: fread(), fwrite(), fgetc(), etc. (end)
猜你喜欢
进程间通信-共享内存shmat
The PMP examination time in March 2022 is set -- "March 27"
Real time preview of PHP in browser by vscade
Will the salary increase after obtaining PMP certification?
USTC of China University of science and technology: Minrui Wang | distribution network voltage stabilization based on transformer Multi-Agent Reinforcement Learning
How to control the display and hiding of layergroup through transparency in leaflet
How to make maputnik, a vector tile matching artifact, support GeoServer
[vscode]todo tree a to-do plug-in
What are the PMP scores?
String类中split()方法的使用
随机推荐
PLC也能制作小游戏----Codesys编写猜数字小游戏
Handling method of wrong heading of VAT special invoice
Packaging and uplink of btcd transaction process (III)
[matlab] polynomial calculation
[Error] invalid use of incomplete type 使用了未定义的类型
新增博客地址
Free lottery --- PMP renewal PDU | PMP knowledge map
ucore lab3
[supersocket 2.0] supersocket 2.0 from the beginning to the end
Test platform series (97) perfect the case part
MASA Auth - 从用户的角度看整体设计
[GXYCTF2019]禁止套娃--详解
[LeetCode]7. Integer inversion thirty-nine
电商员工离职后将产品价格改为1折出售,已被刑拘
How to visit a website
[LeetCode]14. Longest common prefix thirty-eight
MySQL index
Building crud applications in golang
Matlab【路径规划】—— 无人机药品配送路线最优化
[matlab] matrix transformation and matrix evaluation