POI和jxl写入效率
2016-05-18 18:05
218 查看
介绍了poi和jxl读取文件效率,POI获胜
jxl包:好像是1.4 低于2.0 (忘记了)
poi:poi-3.5-FINAL-20090928.jar
环境:CPU:Pentium(R) 1.4GHZ ,1.5GB内存 MyEclipse5.0 JDK1.5
机器不行,不同电脑上跑出效果不一样,但是仍然可以窥视到POI和jxl写入文件效率
/**
*
*/
package com.vteam.mul;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* @作者 Jarvis
* @创建日期 Dec 2, 2009
* @功能 测试POI JXL写入效率
*/
public class Testpj {
public int leng = 6000 ;//控制数据量大小
public int sheet = 2 ; //控制sheet创建个数
public void createPOI(){
try {
// 创建新的Excel 工作簿
HSSFWorkbook newbook = new HSSFWorkbook();
int j = 0;
while(j < sheet){ //控制sheet创建个数
HSSFSheet sheet = newbook.createSheet("test"+j);
HSSFRow row = null ;//行索引
HSSFCell cell1 = null ;//列索引
HSSFCell cell2 = null ;//列索引
for(int i = 0 ; i < leng ; i ++){
row = sheet.createRow(i) ;
cell1 = row.createCell(0);//列索引
cell2 = row.createCell(1);//列索引
if(cell1 == null){
System.out.println("cell1 null ");
}
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);// 定义单元格为字符串类型
cell1.setCellValue("A数据POI"+i);// 在单元格中输入一些内容
cell2.setCellType(HSSFCell.CELL_TYPE_STRING);// 定义单元格为字符串类型
cell2.setCellValue("B数据POI"+1);// 在单元格中输入一些内容
}
j++ ;
}
// 新建一输出文件流
FileOutputStream fp = new FileOutputStream("E:/poi.xls");
newbook.write(fp);// 把相应的Excel 工作簿存盘
fp.flush();
fp.close();// 操作结束,关闭文件
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void createJXL(){
try {
// 打开文件
WritableWorkbook book = Workbook.createWorkbook( new File("E:/jxl.xls "));
int j = 0 ;
while(j < sheet){ //控制sheet创建个数
WritableSheet sheet = book.createSheet("test"+j ,0);
Label label1 = null ;
Label label2 = null ;
for(int i = 0 ; i < leng ;i ++){
label1 = new Label(0,i,"A数据jxl"+i ); //第一个参数是列索引 第二个是行索引
label2 = new Label(1,i,"A数据jxl"+i );
sheet.addCell(label1); // 将定义好的单元格添加到工作表中
sheet.addCell(label2);
}
j++ ;
}
book.write();
book.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
long s = System.currentTimeMillis();
Testpj t = new Testpj();
t.createPOI();
t.createJXL();
long e = System.currentTimeMillis();
System.out.println("写入耗时:"+(e-s));
}
}
测试数据和结果:
创建1个sheet 时间单位:ms
创建2个sheet 时间单位:ms
数据量越大POI耗时越多,而JXL表现却很稳定
为什么结果是jxl写入效率比POI高,而在读取上poi的效率将jxl远远抛在后面。这2个工具,各有所长,因为实现方式不一样,造成了这种意想不到的结果。
jxl包:好像是1.4 低于2.0 (忘记了)
poi:poi-3.5-FINAL-20090928.jar
环境:CPU:Pentium(R) 1.4GHZ ,1.5GB内存 MyEclipse5.0 JDK1.5
机器不行,不同电脑上跑出效果不一样,但是仍然可以窥视到POI和jxl写入文件效率
/**
*
*/
package com.vteam.mul;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* @作者 Jarvis
* @创建日期 Dec 2, 2009
* @功能 测试POI JXL写入效率
*/
public class Testpj {
public int leng = 6000 ;//控制数据量大小
public int sheet = 2 ; //控制sheet创建个数
public void createPOI(){
try {
// 创建新的Excel 工作簿
HSSFWorkbook newbook = new HSSFWorkbook();
int j = 0;
while(j < sheet){ //控制sheet创建个数
HSSFSheet sheet = newbook.createSheet("test"+j);
HSSFRow row = null ;//行索引
HSSFCell cell1 = null ;//列索引
HSSFCell cell2 = null ;//列索引
for(int i = 0 ; i < leng ; i ++){
row = sheet.createRow(i) ;
cell1 = row.createCell(0);//列索引
cell2 = row.createCell(1);//列索引
if(cell1 == null){
System.out.println("cell1 null ");
}
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);// 定义单元格为字符串类型
cell1.setCellValue("A数据POI"+i);// 在单元格中输入一些内容
cell2.setCellType(HSSFCell.CELL_TYPE_STRING);// 定义单元格为字符串类型
cell2.setCellValue("B数据POI"+1);// 在单元格中输入一些内容
}
j++ ;
}
// 新建一输出文件流
FileOutputStream fp = new FileOutputStream("E:/poi.xls");
newbook.write(fp);// 把相应的Excel 工作簿存盘
fp.flush();
fp.close();// 操作结束,关闭文件
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void createJXL(){
try {
// 打开文件
WritableWorkbook book = Workbook.createWorkbook( new File("E:/jxl.xls "));
int j = 0 ;
while(j < sheet){ //控制sheet创建个数
WritableSheet sheet = book.createSheet("test"+j ,0);
Label label1 = null ;
Label label2 = null ;
for(int i = 0 ; i < leng ;i ++){
label1 = new Label(0,i,"A数据jxl"+i ); //第一个参数是列索引 第二个是行索引
label2 = new Label(1,i,"A数据jxl"+i );
sheet.addCell(label1); // 将定义好的单元格添加到工作表中
sheet.addCell(label2);
}
j++ ;
}
book.write();
book.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
long s = System.currentTimeMillis();
Testpj t = new Testpj();
t.createPOI();
t.createJXL();
long e = System.currentTimeMillis();
System.out.println("写入耗时:"+(e-s));
}
}
测试数据和结果:
创建1个sheet 时间单位:ms
插入数据量 | POI耗时 | Jxl耗时 | poi:jxl(耗时比例) |
500 | 1031 | 938 | 1.0991 |
1500 | 1125 | 1125 | 1.0000 |
3000 | 1375 | 1125 | 1.2222 |
6000 | 1812 | 1437 | 1.2610 |
插入数据量 | POI耗时 | Jxl耗时 | Jxl:poi(耗时比例) |
1000 | 1187 | 1078 | 1.1011 |
3000 | 1641 | 1281 | 1.2810 |
6000 | 2766 | 1484 | 1.8639 |
20000 | 13217 | 3315 | 3.987 |
30000 | 25975 | 4941 | 5.257 |
数据量越大POI耗时越多,而JXL表现却很稳定
为什么结果是jxl写入效率比POI高,而在读取上poi的效率将jxl远远抛在后面。这2个工具,各有所长,因为实现方式不一样,造成了这种意想不到的结果。
相关文章推荐
- Android SDK 百度地图通过poi城市内检索简介接口的使用
- Android百度地图poi范围搜索
- Android 百度地图POI搜索功能实例代码
- Java 使用poi把数据库中数据导入Excel的解决方法
- java使用POI读取properties文件并写到Excel的方法
- java使用poi读取ppt文件和poi读取excel、word示例
- Java中使用Apache POI读取word文件简单示例
- POI WORD
- 利用POI实现Word和 Html互转(可以实现在线预览)
- POI封装二:导出 Export
- java生成word文档【一】
- java生成word文档【二】
- 使用POI修改Excel后进行保存
- Java 实现导出excel表
- java POI实现excle 强制换行
- poi解析office文档
- 使用POI读写word doc文件