当前位置:网站首页>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
边栏推荐
- After so long use, CSDN has finally opened a blog
- What are the PMP scores?
- 6.824 Lab 4B: Sharded Key/Value Service
- Maya modeling VI
- [matlab] matrix
- [LeetCode]26. Removes duplicates from a sorted array thirty-three
- [LeetCode]7. Integer inversion thirty-nine
- The whole process from entering URL to displaying page (interview)
- How to control the display and hiding of layergroup through transparency in leaflet
- Machining Industry MES system Mold Industry MES system CNCl Medium Industry MES System MES code scanning and reporting MES data collection
猜你喜欢
进程间通信-共享内存shmat
【HCIE论述】组播IGMP-A
Real time preview of PHP in browser by vscade
PMP training organization
Browser cache execution process
[Error] invalid use of incomplete type 使用了未定义的类型
How to visit a website
Use of split() method in string class
Tsinghua Bosch joint ml center, thbi lab:cheng Yang Ying | realize safety reinforcement learning through the value at risk of constraints
Stm32f4 development of DMA transmission to GPIO port
随机推荐
Vscode实现PHP在浏览器实时预览
PLC can also make small games ----- CoDeSys can write small games of guessing numbers
How to make maputnik, a vector tile matching artifact, support GeoServer
Go custom collation
6.824 Lab 4B: Sharded Key/Value Service
ucore lab1
TypeError: wave.ensureState is not a function
Basics of network security (1)
The e-commerce employee changed the product price to 10% off after leaving the company, and has been detained
Will PM (Project Manager) take the PMP Exam?
哲學和文學的區別
Make the tasks in the scheduled task XXL job flexible
Do you really use the buffer buffer in PostGIS?
PLC也能制作小遊戲----Codesys編寫猜數字小遊戲
Kaust:deyao Zhu | value memory map: a graph structured world model based on off-line reinforcement learning
PMP renewal | PDU specific operation diagram
New blog address
Go implements concurrent non blocking caching
[Error] invalid use of incomplete type 使用了未定义的类型
Delphi2009 connecting Oracle11g