POI和jxl写入效率

来源:互联网 发布:芒果tv有mac版吗 编辑:程序博客网 时间:2024/06/04 18:40

介绍了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

 

插入数据量

POI耗时

Jxl耗时

poi:jxl(耗时比例)

500

1031

938

1.0991

1500

1125

1125

1.0000

3000

1375

1125

1.2222

6000

1812

1437

1.2610

创建2个sheet 时间单位:ms

插入数据量

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个工具,各有所长,因为实现方式不一样,造成了这种意想不到的结果。

0 0