当前位置:网站首页>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.
边栏推荐
- Flink实时仓库-DWD层(交易域-加购维度退化处理)模板代码
- Simulation volume leetcode [general] 150. evaluation of inverse Polish expression
- gin 模版
- win11系统错误:由于找不到 iertutil.dll,无法继续执行代码。重新安装程序可能会解决此问题
- SSH免密登录-两台虚拟机建立免密通道 双向信任
- Teacher Wang Shuyao's notes on operations research 09 linear programming and simplex method (Application of simplex table)
- pytorch的技巧记录
- 上采样之反卷积操作
- resize2fs: 超级块中的幻数有错(Bad magic number in super-block )
- LeetCode 879. 盈利计划
猜你喜欢

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

【charles日常问题】开启charles,使用不了钉钉

外包干了3年,跳槽后转自动化测试工资是原来的2倍,秘诀原来是......

buck电路boot和ph引脚实测

VMware16创建虚拟机:无法创建新虚拟机,不具备执行此操作的权限

DM data guard cluster setup

MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)

聊天机器人有何用处?有何类型?看完这些就明白了!

做开发4年13K,想转行自动化测试,薪资还能涨吗···
![[Charles' daily problems] when you open Charles, you can't use nails](/img/ef/037fc416175d4de769ac6484cb53df.png)
[Charles' daily problems] when you open Charles, you can't use nails
随机推荐
OCR光学字符识别方法汇总
Redis基础篇
Flink实时仓库-DWD层(流量域)模板代码
图像加噪声与矩阵求逆
模拟卷Leetcode【普通】222. 完全二叉树的节点个数
Revolution of game assets
Flink real-time warehouse DWD layer (transaction domain - additional purchase dimension degradation processing) template code
【C语言刷LeetCode】2332. 坐上公交的最晚时间(M)
Win11vmware turns on the virtual machine and restarts on the blue screen and the solution that cannot be started
ECCV 2022 lightweight model frame Parc net press apple mobilevit code and paper Download
好文佳句摘录
Cvpr2022oral special series (I): low light enhancement
数据库系统概述
Teacher Wu Enda's machine learning course notes 00 are written in the front
WPF 界面布局必知基础
2022年SQL经典面试题总结(带解析)
Problems encountered in vmware16 installing virtual machines
Excel文件读写(创建与解析)
[C language brush leetcode] 1054. Bar code with equal distance (m)
使用VsCode配置MySQL实现连接、查询、等功能