• java操作excel——jxl和poi


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

     jxl:jxl-2.6.12.jar

    poi:poi-3.8.jar

    JXLTestMain.java

    Java代码
    View Code
      1 package com.nexusy.excel.jxl;
      2 
      3 import java.io.File;
      4 import java.io.IOException;
      5 import java.util.List;
      6 
      7 import jxl.Cell;
      8 import jxl.CellType;
      9 import jxl.NumberCell;
     10 import jxl.Sheet;
     11 import jxl.Workbook;
     12 import jxl.read.biff.BiffException;
     13 import jxl.write.Label;
     14 import jxl.write.Number;
     15 import jxl.write.WritableSheet;
     16 import jxl.write.WritableWorkbook;
     17 import jxl.write.WriteException;
     18 import jxl.write.biff.RowsExceededException;
     19 
     20 import com.nexusy.excel.Record;
     21 import com.nexusy.excel.TestUtil;
     22 
     23 public class JXLTestMain {
     24     
     25     private final static String filename = "jxltest.xls";
     26     private final static String[] headers = {"ID", "标题", "价格", "数量", "描述"};
     27     
     28     private final static int rows = 65535;
     29 
     30     public static void main(String[] args) {
     31         writeExcel();
     32         readExcel();
     33     }
     34     
     35     public static void writeExcel() {
     36         try {
     37             Thread.sleep(1000*20);
     38         } catch (InterruptedException e1) {
     39             e1.printStackTrace();
     40         }
     41         try {
     42             WritableWorkbook workbook = Workbook.createWorkbook(new File(filename));
     43             
     44             WritableSheet  sheet = workbook.createSheet("jxl测试", 0);
     45             
     46             for (int i = 0; i < headers.length; i++) {
     47                 Label label = new Label(i, 0 , headers[i]);
     48                 sheet.addCell(label);
     49             }
     50             
     51             List<Record> records = TestUtil.getRecords(rows);
     52             long s1 = System.nanoTime();
     53             int c = 1;
     54             for (Record record : records) {
     55                 sheet.addCell(new Number(0, c, record.getId()));
     56                 sheet.addCell(new Label(1, c, record.getTitle()));
     57                 sheet.addCell(new Number(2, c, record.getPrice()));
     58                 sheet.addCell(new Number(3, c, record.getQuantity()));
     59                 sheet.addCell(new Label(4, c, record.getDesc()));
     60                 c++;
     61             }
     62             
     63             workbook.write();
     64             workbook.close();
     65             long s2 = System.nanoTime();
     66             System.out.println("jxl write " + rows + " rows to excel:" + (s2-s1));
     67         } catch (IOException e) {
     68             e.printStackTrace();
     69         } catch (RowsExceededException e) {
     70             e.printStackTrace();
     71         } catch (WriteException e) {
     72             e.printStackTrace();
     73         }
     74 
     75     }
     76 
     77     public static void readExcel() {
     78         try {
     79             Thread.sleep(1000*20);
     80         } catch (InterruptedException e1) {
     81             e1.printStackTrace();
     82         }
     83         try {
     84             long s1 = System.nanoTime();
     85             Workbook workbook = Workbook.getWorkbook(new File(filename));
     86             Sheet sheet = workbook.getSheet(0);
     87             System.out.println(sheet.getName());
     88             for(int i = 0; i < sheet.getRows(); i++){
     89                 Cell[] cells = sheet.getRow(i);
     90                 for (Cell cell : cells) {
     91                     if(cell.getType() == CellType.NUMBER){
     92                         System.out.print(((NumberCell)cell).getValue()+"  ");
     93                     } else if(cell.getType() == CellType.LABEL){
     94                         System.out.print(cell.getContents()+"  ");
     95                     }
     96                 }
     97                 System.out.println();
     98             }
     99             workbook.close();
    100             long s2 = System.nanoTime();
    101             System.out.println("jxl read " + rows + " rows from excel:" + (s2-s1));
    102         } catch (BiffException e) {
    103             e.printStackTrace();
    104         } catch (IOException e) {
    105             e.printStackTrace();
    106         }
    107     }
    108 
    109 }

    POITestMain.java

    Java代码
    View Code
      1 package com.nexusy.excel.poi;
      2 
      3 import java.io.FileInputStream;
      4 import java.io.FileNotFoundException;
      5 import java.io.FileOutputStream;
      6 import java.io.IOException;
      7 import java.io.InputStream;
      8 import java.util.List;
      9 
     10 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
     11 import org.apache.poi.ss.usermodel.Cell;
     12 import org.apache.poi.ss.usermodel.Row;
     13 import org.apache.poi.ss.usermodel.Sheet;
     14 import org.apache.poi.ss.usermodel.Workbook;
     15 
     16 import com.nexusy.excel.Record;
     17 import com.nexusy.excel.TestUtil;
     18 
     19 public class POITestMain {
     20     
     21     private final static String filename = "poitest.xls";
     22     private final static String[] headers = {"ID", "标题", "价格", "数量", "描述"};
     23     
     24     private final static int rows = 65535;
     25 
     26     public static void main(String[] args) {
     27         writeExcel();
     28         readExcel();
     29     }
     30 
     31     public static void writeExcel() {
     32         try {
     33             Thread.sleep(1000*20);
     34         } catch (InterruptedException e1) {
     35             e1.printStackTrace();
     36         }
     37         Workbook wb = new HSSFWorkbook();
     38         try {
     39             FileOutputStream fileOut = new FileOutputStream(filename);
     40             
     41             Sheet sheet = wb.createSheet("poi测试");
     42             Row row = sheet.createRow(0);
     43             for (int i = 0; i < headers.length; i++) {
     44                 row.createCell(i).setCellValue(headers[i]);
     45             }
     46             
     47             List<Record> records = TestUtil.getRecords(rows);
     48             long s1 = System.nanoTime();
     49             int r = 1;
     50             for (Record record : records) {
     51                 row = sheet.createRow(r);
     52                 row.createCell(0).setCellValue(record.getId());
     53                 row.createCell(1).setCellValue(record.getTitle());
     54                 row.createCell(2).setCellValue(record.getPrice());
     55                 row.createCell(3).setCellValue(record.getQuantity());
     56                 row.createCell(4).setCellValue(record.getDesc());
     57                 r++;
     58             }
     59             
     60             wb.write(fileOut);
     61             fileOut.close();
     62             long s2 = System.nanoTime();
     63             System.out.println("poi write " + rows + " rows to excel:" + (s2-s1));
     64         } catch (FileNotFoundException e) {
     65             e.printStackTrace();
     66         } catch (IOException e) {
     67             e.printStackTrace();
     68         }
     69     }
     70 
     71     public static void readExcel() {
     72         try {
     73             Thread.sleep(1000*20);
     74         } catch (InterruptedException e1) {
     75             e1.printStackTrace();
     76         }
     77         try {
     78             long s1 = System.nanoTime();
     79             InputStream inp = new FileInputStream(filename);
     80             Workbook wb = new HSSFWorkbook(inp);
     81             Sheet sheet = wb.getSheetAt(0);
     82             System.out.println(sheet.getSheetName());
     83             
     84             for(Row row : sheet){
     85                 for(Cell cell : row){
     86                     switch (cell.getCellType()) {
     87                     case Cell.CELL_TYPE_NUMERIC:
     88                         System.out.print(cell.getNumericCellValue() + "  ");
     89                         break;
     90                     case Cell.CELL_TYPE_STRING:
     91                         System.out.print(cell.getStringCellValue() + "  ");
     92                         break;
     93 
     94                     default:
     95                         break;
     96                     }
     97                 }
     98                 System.out.println();
     99             }
    100             inp.close();
    101             long s2 = System.nanoTime();
    102             System.out.println("poi read " + rows + " rows from excel:" + (s2-s1));
    103         } catch (FileNotFoundException e) {
    104             e.printStackTrace();
    105         } catch (IOException e) {
    106             e.printStackTrace();
    107         }
    108 
    109 
    110     }
    111 
    112 }

    来源于:http://lanhuidong.iteye.com/blog/1553532

  • 相关阅读:
    获取子进程的是否在线
    python基础知识(二)
    Linux入门50指令
    LInux升级Python版本2.7.11所遇问题汇总
    java开发(1)
    winfrom 水晶报表制作
    asp.net Dock布局开发设置
    winform 窗体最大化事件
    C# 委托于跨线程异步调用
    asp.net-html图片的上传
  • 原文地址:https://www.cnblogs.com/xumin/p/2835661.html
Copyright © 2020-2023  润新知