当前位置:网站首页>XSSFWorkbook,SXSSFWorkbook以及EasyExcel读取Excel文件的比较
XSSFWorkbook,SXSSFWorkbook以及EasyExcel读取Excel文件的比较
2022-07-15 12:00:00 【[email protected]】
同时读取30w数据,比较运行时间及CPU、内存占比
1.EasyExcel
package com.apesource;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.excel.EasyExcel;
import com.apesource.entity.Order;
public class Demo01 {
public static void main(String[] args) {
long begin = System.currentTimeMillis();
// 写入100w
EasyExcel.write("E:\\test\\demo05.xlsx", Order.class)
.sheet("订单列表")
.doWrite(data());
long end = System.currentTimeMillis();
System.out.println("EasyExcel读取30w文件共耗时" +(end - begin) +"毫秒");
}
// 创建100w条订单数据
private static List<Order> data() {
List<Order> list = new ArrayList<Order>();
for (int i = 0; i < 300000; i++) {
list.add(new Order());
}
return list;
}
}

2.XSSFWorkbook
package com.apesource;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Demo10 {
public static void main(String[] args) {
long begin = System.currentTimeMillis();
// 使用
try (Workbook workbook = new XSSFWorkbook();
FileOutputStream out = new FileOutputStream("E:\\test\\demo03.xlsx")) {
Sheet sheet = workbook.createSheet();
Row headrow = sheet.createRow(0);
Cell headcell0 = headrow.createCell(0);
headcell0.setCellValue("序号");
Cell headcell1 = headrow.createCell(1);
headcell1.setCellValue("姓名");
Cell headcell2 = headrow.createCell(2);
headcell2.setCellValue("日期");
Cell headcell3 = headrow.createCell(3);
headcell3.setCellValue("红包金额");
// 获取格式编码值
DataFormat dataFormat = workbook.createDataFormat();
short dataFormatCode = dataFormat.getFormat("yyyy年MM月dd日 HH:mm:ss");
short moneyFormatCode = dataFormat.getFormat("¥#,###");
// 创建日期格式对象
CellStyle dataCellStyle = workbook.createCellStyle();
dataCellStyle.setDataFormat(dataFormatCode);
// 创建货币格式对象
CellStyle moneyCellStyle = workbook.createCellStyle();
moneyCellStyle.setDataFormat(moneyFormatCode);
for(int i = 0 ;i < 300000;i++) {
String name = "A" + i;
// 创建行
Row row = sheet.createRow(i+1);
// 创建单元格
Cell cell0 = row .createCell(0); // 序号
cell0.setCellValue(String.valueOf(i+1));
Cell cell1 = row.createCell(1); // 姓名
cell1.setCellValue(name);
Cell cell2 = row.createCell(2);// 日期
cell2.setCellStyle(dataCellStyle);// 日期格式对象
cell2.setCellValue(new Date());
Cell cell3 = row.createCell(3);
cell3.setCellStyle(moneyCellStyle);
cell3.setCellValue((int)(Math.random() *1000000));
}
// 写入文件
workbook.write(out);
long end = System.currentTimeMillis();
System.out.println("XSSFWorkbook读取30w文件共耗时" +(end - begin) +"毫秒");
} catch (IOException e) {
e.printStackTrace();
}
}
}

3.SXSSFWorkbook
package com.apesource;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Demo10 {
public static void main(String[] args) {
long begin = System.currentTimeMillis();
// 使用
try (Workbook workbook = new SXSSFWorkbook(100);
FileOutputStream out = new FileOutputStream("E:\\test\\demo03.xlsx")) {
Sheet sheet = workbook.createSheet();
Row headrow = sheet.createRow(0);
Cell headcell0 = headrow.createCell(0);
headcell0.setCellValue("序号");
Cell headcell1 = headrow.createCell(1);
headcell1.setCellValue("姓名");
Cell headcell2 = headrow.createCell(2);
headcell2.setCellValue("日期");
Cell headcell3 = headrow.createCell(3);
headcell3.setCellValue("红包金额");
// 获取格式编码值
DataFormat dataFormat = workbook.createDataFormat();
short dataFormatCode = dataFormat.getFormat("yyyy年MM月dd日 HH:mm:ss");
short moneyFormatCode = dataFormat.getFormat("¥#,###");
// 创建日期格式对象
CellStyle dataCellStyle = workbook.createCellStyle();
dataCellStyle.setDataFormat(dataFormatCode);
// 创建货币格式对象
CellStyle moneyCellStyle = workbook.createCellStyle();
moneyCellStyle.setDataFormat(moneyFormatCode);
for(int i = 0 ;i < 300000;i++) {
String name = "A" + i;
// 创建行
Row row = sheet.createRow(i+1);
// 创建单元格
Cell cell0 = row .createCell(0); // 序号
cell0.setCellValue(String.valueOf(i+1));
Cell cell1 = row.createCell(1); // 姓名
cell1.setCellValue(name);
Cell cell2 = row.createCell(2);// 日期
cell2.setCellStyle(dataCellStyle);// 日期格式对象
cell2.setCellValue(new Date());
Cell cell3 = row.createCell(3);
cell3.setCellStyle(moneyCellStyle);
cell3.setCellValue((int)(Math.random() *1000000));
}
// 写入文件
workbook.write(out);
long end = System.currentTimeMillis();
System.out.println("SXSSFWorkbook读取30w文件共耗时" +(end - begin) +"毫秒");
} catch (IOException e) {
e.printStackTrace();
}
}
}


综上数据可见:同是30w数据SXSSFWorkbook运行速度最快,CPU占比最小;EasyExcel次之;XSSFWorkbook运行速度最慢,CPU占比最大;
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_49194330/article/details/125791834
边栏推荐
- webview加载url提示net::ERROR_UNKNOWN_URL_SCHEME
- 欧拉Talk | 开发者社区体验坦白局7月14日19:30约起
- MQTT---Connect
- WebView loading URL prompt net:: error_ UNKNOWN_ URL_ SCHEME
- 1. OLED simple drive
- 558. Quadtree intersection / Sword finger offer II 118 Redundant edge
- 爱可可AI前沿推介(7.15)
- 51nod 1278 相离的圆
- 10分钟带你进入Swagger的世界,快来看一看吧
- To realize the remote direct electromagnetic communication of consciousness, the Dongda team, together with the National University of Singapore and others, is expected to build an electromagnetic bra
猜你喜欢

智源社区周刊#89:Yann LeCun等撰文回应Marcus符号推理争论;朱松纯团队AI价值对齐新工作登Science官网头条;马毅沈向洋等公开AI智能的综述论文

No.1 port and shipping AI enterprises in the world have integrated fly pupil, and the mature and standardized AI products of port and shipping have been applied on a large scale, which has greatly imp

HCIP回顾(1)

Detailed explanation of thread interrupt method

Recurrence of two CVE vulnerabilities

Solve the comprehensive monitoring scheme for the dynamic ring state of small and medium-sized machine rooms

Number formatting

Development practice - reasoning application development experience of shengteng cann

【MySQL】多表查询

下载完PyQt5,发现找不到designer.exe问题解决方案
随机推荐
腾讯大咖分享 | 腾讯Alluxio(DOP)在金融场景的落地与优化实践
Configuration of teacher management module and MP automatic code generation
元宇宙大爆发是谁在“跑马圈地”?
HCIP笔记(2)
Recurrence of two CVE vulnerabilities
PyGame - airplane battle (to be improved)
ESP32的串口通信(以中断和看门狗的方式进行)
To realize the remote direct electromagnetic communication of consciousness, the Dongda team, together with the National University of Singapore and others, is expected to build an electromagnetic bra
umi 3.0模版
万字详解SSH(SSH登录原理+SSH配置+模拟实现SSH免密登录)
Solve the comprehensive monitoring scheme for the dynamic ring state of small and medium-sized machine rooms
基于eTS高效开发HarmonyOS课程类应用
HCIP回顾(2)
webview加载url提示net::ERROR_UNKNOWN_URL_SCHEME
线程-interrupt方法详解
Efficient development of harmonyos course applications based on ETS
两年前的DeFi弄潮儿,龙头、蓝筹们怎么样了?
Detailed explanation of thread interrupt method
谷歌提出可靠性机器学习模型Plex:让大模型对分布外数据提出合适的预测结果
558. 四叉树交集 / 剑指 Offer II 118. 多余的边