您的位置:首页 > 编程语言 > Java开发

java操作Excel效率之poi与fastexcel比较

2009-06-10 10:00 471 查看
最近看到有关fastexcel的消息

自己做了个小测试

POI代码如下

Java代码

public

void
testPoiExcel(String strPath)
throws
Exception {

HSSFWorkbook wb = new
HSSFWorkbook(
new
FileInputStream(strPath));

HSSFSheet sheet = wb.getSheetAt(0
);

HSSFRow row;

HSSFCell cell;

for
(
int
i = sheet.getFirstRowNum(); i < sheet.getLastRowNum(); i++) {

row = sheet.getRow(i);

for
(
int
j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {

cell = row.getCell((short
) j);

}

}

public void testPoiExcel(String strPath) throws Exception {

HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(strPath));

HSSFSheet sheet = wb.getSheetAt(0);

HSSFRow row;
HSSFCell cell;

for (int i = sheet.getFirstRowNum(); i < sheet.getLastRowNum(); i++) {
row = sheet.getRow(i);
for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
cell = row.getCell((short) j);
}
}
}


fastExcel代码如下

Java代码

public

void
testFastExcel(String strPath)
throws
Exception {

Workbook workBook = FastExcel.createReadableWorkbook(new
File(strPath));

workBook.open();

Sheet s = workBook.getSheet(0
);

String[] row;

String cell;

for
(
int
i = s.getFirstRow(); i <= s.getLastRow(); i++) {

row = s.getRow(i);

for
(
int
j = s.getFirstColumn(); j <= s.getLastColumn(); j++) {

cell = s.getCell(i, j);

}

}

workBook.close();

}

public void testFastExcel(String strPath) throws Exception {

Workbook workBook = FastExcel.createReadableWorkbook(new File(strPath));
workBook.open();

Sheet s = workBook.getSheet(0);

String[] row;
String cell;

for (int i = s.getFirstRow(); i <= s.getLastRow(); i++) {
row = s.getRow(i);
for (int j = s.getFirstColumn(); j <= s.getLastColumn(); j++) {
cell = s.getCell(i, j);
}
}

workBook.close();
}


Excel一行数据如下
【AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAA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AAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA,AAAAAAAA



测试结果如下

POI 2272条 5782毫秒

FastExcel 2272条 828毫秒

在我的机器上设置的jvm内存 poi只能跑2272条数据,再多就内存溢出了。

FastExcel做到48000条还没溢出时间是14093毫秒

为什么这么大区别 我自己感觉是 POI用的是大对象HSSFRow,HSSFCell而Fastexcel是String[]和String

毕竟在集合中数组是占用空间最小的

但是这样的话 fastexcel不能操作Excel的样式了 ,如果是做简单的Excel导入导出首选FastExcel

Source.rar
(1.1 MB)

下载次数: 230

声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。

推荐链接



6月份JAVA技术沙龙活动通知

在繁琐中挣扎还是简化自主管理?

houwen

等级: 初级会员



文章: 2

积分: 30

来自: 上海



Java代码

public

static

void
main(String [] args){

writeExcel("testPoi.xls"
);

System.out.println(readExcel(new
File(
"testPoi.xls"
)));

}

private

static

void
writeExcel(String fileName) {

HSSFWorkbook wb = new
HSSFWorkbook();

for
(
int
sheetIndx =
0
; sheetIndx <
5
;sheetIndx++){

HSSFSheet sheet = wb.createSheet("sheet"
+ sheetIndx);

HSSFRow row;

HSSFCell cell;

for
(
int
rowIndex =
0
; rowIndex <
10
; rowIndex++) {

row = sheet.createRow(rowIndex);

for
(
int
cellIndex =
0
; cellIndex <
5
; cellIndex++) {

cell = row.createCell(cellIndex);

cell.setCellValue("hello"
);

}

}

}

FileOutputStream fos = null
;

try
{

initFile(fileName);

fos = new
FileOutputStream(fileName);

wb.write(fos);

}catch
(Exception e){

e.printStackTrace();

} finally
{

try
{

fos.flush();

fos.close();

} catch
(Exception e) {

e.printStackTrace();

}

}

}

private

static
String readExcel(File file) {

StringBuffer strBuffer = new
StringBuffer();

HSSFWorkbook wb;

FileInputStream fs = null
;

try
{

fs = new
FileInputStream(file);

wb = new
HSSFWorkbook(fs);

int
sheetNum = wb.getNumberOfSheets();

for
(
int
sheetIndx =
0
; sheetIndx < sheetNum ;sheetIndx++){

HSSFSheet sheet = wb.getSheetAt(sheetIndx);

HSSFRow row;

HSSFCell cell;

strBuffer.append(wb.getSheetName(sheetIndx)+ "/r/n"
);

for
(
int
i = sheet.getFirstRowNum(); i < sheet.getLastRowNum(); i++) {

row = sheet.getRow(i);

for
(
int
j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {

cell = row.getCell(j);

strBuffer.append(cell.toString() + "/t"
);

}

strBuffer.append("/r/n"
);

}

}

}catch
(Exception e){

} finally
{

try
{

fs.close();

} catch
(IOException e) {

e.printStackTrace();

}

}

return
strBuffer.toString();

}

private

static

void
initFile(String filePath) {

File file = new
File(filePath);

if
(!file.exists()){

try
{

file.createNewFile();

} catch
(IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

public static void main(String [] args){
writeExcel("testPoi.xls");
System.out.println(readExcel(new File("testPoi.xls")));
}

private static void writeExcel(String fileName) {
HSSFWorkbook wb = new HSSFWorkbook();
for(int sheetIndx = 0; sheetIndx < 5 ;sheetIndx++){
HSSFSheet sheet = wb.createSheet("sheet" + sheetIndx);
HSSFRow row;
HSSFCell cell;
for (int rowIndex = 0; rowIndex < 10; rowIndex++) {
row = sheet.createRow(rowIndex);
for (int cellIndex = 0; cellIndex < 5; cellIndex++) {
cell = row.createCell(cellIndex);
cell.setCellValue("hello");
}
}
}
FileOutputStream fos = null;
try{
initFile(fileName);
fos = new FileOutputStream(fileName);
wb.write(fos);
}catch(Exception e){
e.printStackTrace();

} finally {
try {
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

private static String readExcel(File file) {
StringBuffer strBuffer = new StringBuffer();
HSSFWorkbook wb;
FileInputStream fs = null;
try {
fs = new FileInputStream(file);
wb = new HSSFWorkbook(fs);
int sheetNum = wb.getNumberOfSheets();
for(int sheetIndx = 0; sheetIndx < sheetNum ;sheetIndx++){
HSSFSheet sheet = wb.getSheetAt(sheetIndx);
HSSFRow row;
HSSFCell cell;
strBuffer.append(wb.getSheetName(sheetIndx)+ "/r/n");
for (int i = sheet.getFirstRowNum(); i < sheet.getLastRowNum(); i++) {
row = sheet.getRow(i);
for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
cell = row.getCell(j);
strBuffer.append(cell.toString() + "/t");
}
strBuffer.append("/r/n");
}
}
}catch (Exception e){

} finally {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return strBuffer.toString();
}
private static void initFile(String filePath) {
File file = new File(filePath);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


poi当cell对象过多的时候 会造成内存溢出的

而且样式什么的设置 如果过多的话也会内存溢出

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: