`
lanhuidong
  • 浏览: 223514 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

java操作excel——jxl和poi比较

    博客分类:
  • Java
阅读更多

最近需要需要项目开发需要从excel导入数据到数据库,于是就开始找开源的java操作excel的框架。貌似比较流行的有jxl和poi两个框架。网上有些对这两个框架比较的文章,但都不是最近的。根据项目需要,下面对jxl和poi读写excel的性能做个比较。

 

jxl:jxl-2.6.12.jar

poi:poi-3.8.jar

 

JXLTestMain.java

package com.nexusy.excel.jxl;

import java.io.File;
import java.io.IOException;
import java.util.List;

import jxl.Cell;
import jxl.CellType;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import com.nexusy.excel.Record;
import com.nexusy.excel.TestUtil;

public class JXLTestMain {
    
    private final static String filename = "jxltest.xls";
    private final static String[] headers = {"ID", "标题", "价格", "数量", "描述"};
    
    private final static int rows = 65535;

    public static void main(String[] args) {
        writeExcel();
        readExcel();
    }
    
    public static void writeExcel() {
        try {
            Thread.sleep(1000*20);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        try {
            WritableWorkbook workbook = Workbook.createWorkbook(new File(filename));
            
            WritableSheet  sheet = workbook.createSheet("jxl测试", 0);
            
            for (int i = 0; i < headers.length; i++) {
                Label label = new Label(i, 0 , headers[i]);
                sheet.addCell(label);
            }
            
            List<Record> records = TestUtil.getRecords(rows);
            long s1 = System.nanoTime();
            int c = 1;
            for (Record record : records) {
                sheet.addCell(new Number(0, c, record.getId()));
                sheet.addCell(new Label(1, c, record.getTitle()));
                sheet.addCell(new Number(2, c, record.getPrice()));
                sheet.addCell(new Number(3, c, record.getQuantity()));
                sheet.addCell(new Label(4, c, record.getDesc()));
                c++;
            }
            
            workbook.write();
            workbook.close();
            long s2 = System.nanoTime();
            System.out.println("jxl write " + rows + " rows to excel:" + (s2-s1));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (RowsExceededException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }

    }

    public static void readExcel() {
        try {
            Thread.sleep(1000*20);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        try {
            long s1 = System.nanoTime();
            Workbook workbook = Workbook.getWorkbook(new File(filename));
            Sheet sheet = workbook.getSheet(0);
            System.out.println(sheet.getName());
            for(int i = 0; i < sheet.getRows(); i++){
                Cell[] cells = sheet.getRow(i);
                for (Cell cell : cells) {
                    if(cell.getType() == CellType.NUMBER){
                        System.out.print(((NumberCell)cell).getValue()+"  ");
                    } else if(cell.getType() == CellType.LABEL){
                        System.out.print(cell.getContents()+"  ");
                    }
                }
                System.out.println();
            }
            workbook.close();
            long s2 = System.nanoTime();
            System.out.println("jxl read " + rows + " rows from excel:" + (s2-s1));
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

 

POITestMain.java

package com.nexusy.excel.poi;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.nexusy.excel.Record;
import com.nexusy.excel.TestUtil;

public class POITestMain {
    
    private final static String filename = "poitest.xls";
    private final static String[] headers = {"ID", "标题", "价格", "数量", "描述"};
    
    private final static int rows = 65535;

    public static void main(String[] args) {
        writeExcel();
        readExcel();
    }

    public static void writeExcel() {
        try {
            Thread.sleep(1000*20);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        Workbook wb = new HSSFWorkbook();
        try {
            FileOutputStream fileOut = new FileOutputStream(filename);
            
            Sheet sheet = wb.createSheet("poi测试");
            Row row = sheet.createRow(0);
            for (int i = 0; i < headers.length; i++) {
                row.createCell(i).setCellValue(headers[i]);
            }
            
            List<Record> records = TestUtil.getRecords(rows);
            long s1 = System.nanoTime();
            int r = 1;
            for (Record record : records) {
                row = sheet.createRow(r);
                row.createCell(0).setCellValue(record.getId());
                row.createCell(1).setCellValue(record.getTitle());
                row.createCell(2).setCellValue(record.getPrice());
                row.createCell(3).setCellValue(record.getQuantity());
                row.createCell(4).setCellValue(record.getDesc());
                r++;
            }
            
            wb.write(fileOut);
            fileOut.close();
            long s2 = System.nanoTime();
            System.out.println("poi write " + rows + " rows to excel:" + (s2-s1));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void readExcel() {
        try {
            Thread.sleep(1000*20);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        try {
            long s1 = System.nanoTime();
            InputStream inp = new FileInputStream(filename);
            Workbook wb = new HSSFWorkbook(inp);
            Sheet sheet = wb.getSheetAt(0);
            System.out.println(sheet.getSheetName());
            
            for(Row row : sheet){
                for(Cell cell : row){
                    switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.print(cell.getNumericCellValue() + "  ");
                        break;
                    case Cell.CELL_TYPE_STRING:
                        System.out.print(cell.getStringCellValue() + "  ");
                        break;

                    default:
                        break;
                    }
                }
                System.out.println();
            }
            inp.close();
            long s2 = System.nanoTime();
            System.out.println("poi read " + rows + " rows from excel:" + (s2-s1));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}

 

 测试结果如下:

jxl写入65535行数据

 

jxl读取65535行数据: 

 

 

poi写入65535行数据:


 poi读取65535行数据:

 

 

  • 大小: 41.4 KB
  • 大小: 38.6 KB
  • 大小: 40.1 KB
  • 大小: 40.2 KB
  • excel.zip (3.5 MB)
  • 描述: 项目代码
  • 下载次数: 469
分享到:
评论
11 楼 zi_wu_xian 2016-09-02  
不管用POI还是JXL都是第三的操作excel的组件,都有破坏excel文件格式的可能,还是用PageOffice动态生成excel文件,操作excel文件中的数据比较好,因为是调用的Office自己的VBA接口,所以不会有格式问题。
10 楼 lanhuidong 2014-03-29  
weimeili8211 写道
我用poi,jxl都 会内存溢出

9 楼 weimeili8211 2014-03-28  
我用poi,jxl都 会内存溢出
8 楼 tony_jingzhou 2013-10-16  
tangzililiang 写道
这样比较太片面了哦,如果尝试用POI的事件驱动模式读取我相信内存占用会大大降低,JXL将文件一次性加载到内存,读取大文件是短板(原理可参照xml文件读取方式dom和sax的区别)

采用事件模型,poi读取10M以上的Excel 还是会内存溢出?
7 楼 tangzililiang 2013-07-01  
这样比较太片面了哦,如果尝试用POI的事件驱动模式读取我相信内存占用会大大降低,JXL将文件一次性加载到内存,读取大文件是短板(原理可参照xml文件读取方式dom和sax的区别)
6 楼 lanhuidong 2013-05-09  
bamboo_prince 写道
好像效率差不多嘛,你推荐用哪种啊?

我自己选择了poi
5 楼 bamboo_prince 2013-05-08  
好像效率差不多嘛,你推荐用哪种啊?
4 楼 lanhuidong 2013-05-02  
winerlinwen1 写道
我怎么读到2760行就爆了!我用poi操作的!

什么爆了?
3 楼 winerlinwen1 2013-04-28  
我怎么读到2760行就爆了!我用poi操作的!
2 楼 lanhuidong 2012-09-10  
zhangjian420 写道
这个是什么工具监视的!

jvisualvm,jdk自带的工具,在bin目录中。兄弟可有更好的监视工具推荐?
1 楼 zhangjian420 2012-07-24  
这个是什么工具监视的!

相关推荐

Global site tag (gtag.js) - Google Analytics