当前位置:网站首页>Excel file reading and writing (creation and parsing)
Excel file reading and writing (creation and parsing)
2022-07-29 07:12:00 【m0_ sixty-seven million four hundred and one thousand two hundr】
Can be done Excel The mainstream technologies of document processing include :
① Apache POI ( be based on DOM The way to analyze , Load the file directly into memory , Faster , fit Excel Application scenarios with a small amount of file data )
② JXL ( fit Excel Application scenarios with a small amount of file data )
③ Alibaba EasyExcel ( Adopt the parsing mode of line by line reading , Notify the processing of the parsing result of each line in the observer mode (AnalysisEventListener), Therefore, it is more suitable for those with a large amount of data Excel File parsing )
( One of the most common is Apache POI,Alibaba EasyExcel.)
One .Apache POI
POI Provides parsing of files in different formats :
HSSF Used to parse the old version Excel file (.xls), Because of the old version of Excel Files can only exist 65535 Row data , So it's not often used , It is commonly used at present XSSF analysis Excel file (.xlsx).
First add jjar Packet dependency :
1. Workbook
Workbook The interface represents a Excel file , Usually create its implementation class object to create (/ load )Excel file , Common implementation classes are XSSFWorkbook.
(1). establish Excel file
try (Workbook workbook = new XSSFWorkbook();
FileOutputStream out =
new FileOutputStream("E:\ The ape \demo.xlsx")) {
// take workbook The data contained in the object , Through output stream , Write to Excel file
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
}
(2). load ( analysis )Excel file
// Through the input stream , Read excel file
FileInputStream in = new FileInputStream("E:\ achievement .xlsx");
// Pass the input stream into Workbook
Workbook workbook = new XSSFWorkbook(in);
Note that the input stream is also closed , You can call in.close(), We usually use try catch Block to automatically close :
try (Workbook workbook = new XSSFWorkbook();
FileOutputStream out =
new FileOutputStream("E:\demo.xlsx")) {
// take workbook The data contained in the object , Through output stream , Write to Excel file
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
}
(3)sheet ( workbook )
adopt Workbook To carry out the workbook sheet Creation and acquisition of
①.sheet The creation of
// Create Workbook with default name
Sheet sheet1 = workbook.createSheet();
// Create a workbook with a custom name
Sheet sheet2 = workbook.createSheet(" Customize Workbook ");
②.sheet Acquisition
// Get by workbook subscript Sheet
Sheet sheet01 = workbook.getSheetAt(0);
// Get by workbook name Sheet
Sheet sheet02 = workbook.getSheet("Sheet0");
③. Number of workbooks
int sheetNum = workbook.getNumberOfSheets();
System.out.println(" Number of workbooks :" + sheetNum);
④. Data row of workbook
System.out.println(" workbook 0 The data line :" + sheet0.getLastRowNum());
(3).Row( data row )
①. Create data rows
Row row0 = sheet0.createSheet(0);
②. Get first / Last line subscript
int first = sheet.getFirstRowNum();
int last = sheet.getLastRowNum();
③. Get the specified line according to the subscript
Row row = sheet.getRow(0); // first line
④. Traverse the specified area row
// Traverse all lines
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
System.out.println(row);
}
(4).Cell( Cell )
①. Creation and acquisition of cells
Cell cell0 = Row.createCell(0); // cell The creation of
Cell cell = Row.getCell(0); // cell Acquisition
②. Set column header ( The first line of the form ) The content of
cellHead.setCellValue(" Serial number ");
cellHead1.setCellValue(" full name ");
cellHead2.setCellValue(" Gender ");
cellHead3.setCellValue(" Date of creation ");
②. Get cell contents
System.out.println(" Serial number :" + cell0.getNumericCellValue());
System.out.println(" full name :" + cell1.getStringCellValue());
③. Set cell contents
cell0.setCellValue(Math.random()*1000);
④. Get cell type
CellType type = cell.getCellType();
⑤. Format cell
// Create cell format
// Get format encoding
DataFormat dataFormat = workbook.createDataFormat();
short formatCode = dataFormat.getFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(" Cell format encoding :" + formatCode);
// establish cellStyle Cell format object
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(formatCode); // Set cell format encoding
...
// By setting cellStyle Cell format object , To display the date normally
Cell cell0 = Row.createCell();
cell0.setCellStyle(cellStyle); // Format cell
cell0.setCellValue(new Date()); // Save date to this cell
Two . Super large Excel Reading and writing of documents
1. Use POI write in ===> SXXSFWorkbook
It can write a very large amount of data , Such as 100w Even more , Write data fast , Less memory .
It will generate temporary files during writing , The default is 100 Pieces of data are saved to memory , If the default data amount is exceeded , The previous data will be written to the temporary file , By setting SXSSFWorkbook To set the number of rows held in memory each time , When this value is reached , I'll take the data flush To disk , In this way, there will not be insufficient memory .
SXSSFWorkbook write in 10w Row data ( Because of the time problem, use 10w Line data to test )
The implementation code is as follows :
// Workbook Implementation class of SXSSFWorkbook:
try (Workbook workbook = new SXSSFWorkbook();
FileOutputStream out = new FileOutputStream("E:\10w.xlsx")) {
long begin = System.currentTimeMillis();
Sheet sheet = workbook.createSheet();
// Create column header
Row headRow = sheet.createRow(0);
// Set column header cell content
Cell cellVal0 = headRow.createCell(0);
cellVal0.setCellValue(" Serial number ");
// Create data rows
for(int i = 0;i < 100000;i++) {
String name = "A" + i;
// Create lines
Row row = sheet.createRow(i+1);
// Create cells
Cell cell0 = row.createCell(0); // Serial number
cell0.setCellValue(String.valueOf(i+1));
}
workbook.write(out);
long after = System.currentTimeMillis();
System.out.println("SXSSFWorkbook write in 10w Time consuming for row data :" + (after-begin) + "ms");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The operation results are as follows :
CPU Use as follows 
2. Use Alibaba EasyExcel write in
First download Alibaba EasyExcel relevant jar package 
Use EasyExcel write in 10w Order data
// easyExcel: It is fast to read large files , Memory usage is small
// write in 10w Order data
long begin = System.currentTimeMillis();
// 10w
EasyExcel.write("E:\easy10w.xlsx",Order.class).sheet(" Order list ").doWrite(data());
long after = System.currentTimeMillis();
System.out.println(" Total time consuming " + (after-begin) + "ms");
}
// establish 100w Order data
private static List<Order> data(){
List<Order> list = new ArrayList<Order>();
for(int i = 0;i < 100000;i++) {
list.add(new Order());
}
return list;
}
The operation results are as follows :
CPU The usage rate is as follows :
thus it can be seen ,10w The data of , by comparison ,SXSSFWorkbook The fastest ,CPU The highest occupancy rate ;EasyExcel Low running speed ,CPU The lowest occupancy . because poi Memory consumption is very high ,EasyExcel It can greatly reduce the occupation of memory ( as a result of EasyExcel Adopt the parsing mode of line by line reading , Not all file data is loaded into memory at one time , Is to read data line by line on the disk ), Therefore, it is recommended to use EasyExcel.
边栏推荐
- [C language brush leetcode] 67. binary sum (E)
- 【C语言刷LeetCode】1054. 距离相等的条形码(M)
- LeetCode 879. 盈利计划
- 怎么会不喜欢呢,CICD中轻松发送邮件
- 接口测试实战项目03:执行测试用例
- After three years of outsourcing, the salary of automatic testing after job hopping is twice that of the original. The secret is
- Improved Pillar with Fine-grained Feature for 3D Object Detection论文笔记
- fillder使用
- 模拟卷Leetcode【普通】150. 逆波兰表达式求值
- vagrant box 集群 处理
猜你喜欢

Cvpr2022oral special series (I): low light enhancement

Federal learning backdoor attack summary (2019-2022)

Windows 上 php 7.4 连接 oracle 配置

Idea cannot find a database solution

vim文本编辑器的一些使用小技巧

Flink real-time warehouse DWD layer (transaction domain - additional purchase dimension degradation processing) template code

buck电路boot电容短路和断路实测波形

Vmware16 create virtual machine: win11 cannot be installed

LeetCode 879. 盈利计划

【charles日常问题】开启charles,使用不了钉钉
随机推荐
Unity free element special effect recommendation
后缀自动机(SAM)讲解 + Luogu p3804【模板】后缀自动机 (SAM)
MySQL queries are case sensitive
330. 按要求补齐数组
LeetCode 879. 盈利计划
Cesium reflection
Teacher Wang Shuyao's notes on operations research 09 linear programming and simplex method (Application of simplex table)
Pod基本介绍
[C language brush leetcode] 67. binary sum (E)
Decompilation of wechat applet
buck电路boot电容短路和断路实测波形
MySQL----多表查询
模拟卷Leetcode【普通】222. 完全二叉树的节点个数
我的个人网站不让接入微信登录,于是我做了这个
微信小程序的反编译
Simulation volume leetcode [normal] 061. rotating linked list
记 - 踩坑-实时数仓开发 - doris/pg/flink
Spark Learning Notes (VII) -- spark core core programming - RDD serialization / dependency / persistence / partition / accumulator / broadcast variables
Simulation volume leetcode [normal] 222. number of nodes of complete binary tree
聊天机器人有何用处?有何类型?看完这些就明白了!