用事实证明POI和jxl写入效率_用poi解析word总结
用事实证明POI和jxl写入效率由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“用poi解析word总结”。
用事实证明POI和jxl写入效率 jxl包:好像是1.4 低于2.0(忘记了)
jxl包:好像是1.4 低于2.0(忘记了)
poi:poi-3.5-FINAL-20090928.jar
环境:CPU:Pentium(R)1.4GHZ ,1.5GB内存 MyEclipse5.0JDK1.5
机器不行,不同电脑上跑出效果不一样,但是仍然可以窥视到POI和jxl写入文件效率
view plaincopy to clipboardprint?
/**
*
*/
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.hf.usermodel.HSSFCell;
import org.apache.poi.hf.usermodel.HSSFRow;
import org.apache.poi.hf.usermodel.HSSFSheet;
import org.apache.poi.hf.usermodel.HSSFWorkbook;
/**
* @作者 Jarvis
* @创建日期 Dec 2, 2009
* @功能 测试POI JXL写入效率
*/
public cla Testpj {
public int leng = 6000;//控制数据量大小
public int sheet = 2;//控制sheet创建个数
public void createPOI(){
try {
// 创建新的Excel 工作簿
HSSFWorkbook newbook = new HSSFWorkbook();
int j = 0;
while(j
HSSFSheet sheet = newbook.createSheet(“test”+j);
HSSFRow row = null;//行索引
HSSFCell cell1 = null;//列索引
HSSFCell cell2 = null;//列索引
for(int i = 0;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(newFile(“E:/jxl.xls ”));int j = 0;
while(j
WritableSheet sheet=book.createSheet(“test”+j ,0);
Label label1 = null;
Label label2 = null;
for(int i = 0;i
label1=newLabel(0,i,“A数据jxl”+i);//第一个参数是列索引 第二个是行索引
label2=newLabel(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));
}
}
/**
*
*/
package com.vteam.mul;
jxl包:好像是1.4 低于2.0(忘记了)
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.hf.usermodel.HSSFCell;
import org.apache.poi.hf.usermodel.HSSFRow;
import org.apache.poi.hf.usermodel.HSSFSheet;
import org.apache.poi.hf.usermodel.HSSFWorkbook;
/**
* @作者 Jarvis
* @创建日期 Dec 2, 2009
* @功能 测试POI JXL写入效率
*/
public cla Testpj {
public int leng = 6000;//控制数据量大小
public int sheet = 2;//控制sheet创建个数
public void createPOI(){
try {
// 创建新的Excel 工作簿
HSSFWorkbook newbook = new HSSFWorkbook();
int j = 0;
while(j
HSSFSheet sheet = newbook.createSheet(“test”+j);
HSSFRow row = null;//行索引
HSSFCell cell1 = null;//列索引
HSSFCell cell2 = null;//列索引
for(int i = 0;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(newFile(“E:/jxl.xls ”));
int j = 0;
while(j
WritableSheet sheet=book.createSheet(“test”+j ,0);
Label label1 = null;
Label label2 = null;
for(int i = 0;i
label1=newLabel(0,i,“A数据jxl”+i);//第一个参数是列索引 第二个是行索引
label2=newLabel(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));
}
jxl包:好像是1.4 低于2.0(忘记了)
}
测试数据和结果
:
为什么结果是jxl写入效率比POI高,而在读取上poi的效率将jxl远远抛在后面。这2个工具,各有所长,因为实现方式不一样,造成了这种意想不到的结果。