java 操作 excel文件 jxl 与 poi 比较

本文提供了一种使用Java读取Excel文件的方法,分别利用JXL和Apache POI两种库进行操作,并对比了它们的性能差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. import java.io.FileInputStream;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.util.Iterator;  
  5.   
  6. import jxl.Sheet;  
  7. import jxl.Workbook;  
  8.   
  9. import org.apache.poi.hssf.usermodel.HSSFCell;  
  10. import org.apache.poi.hssf.usermodel.HSSFRow;  
  11. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  12. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  13.   
  14. public class Test {  
  15.   
  16.  /** 
  17.   * @param path 
  18.   */  
  19.  public static void readExcelByJXL(String path) {  
  20.   Workbook book = null;  
  21.   InputStream input = null;  
  22.   try {  
  23.    input = new FileInputStream(path);  
  24.    book = Workbook.getWorkbook(input);  
  25.    Sheet sheet = book.getSheet(0);  
  26.    int columns = sheet.getColumns();  
  27.    for (int i = 0; i < sheet.getRows(); i++) {  
  28.     for (int j = 0; j < columns; j++)  
  29.      System.out.print(sheet.getCell(j, i).getContents()  
  30.        + "\t\t\t\t");  
  31.     System.out.println();  
  32.    }  
  33.   } catch (Exception ex) {  
  34.    ex.printStackTrace();  
  35.   } finally {  
  36.    if (book != null)  
  37.     book.close();  
  38.    if (input != null)  
  39.     try {  
  40.      input.close();  
  41.     } catch (IOException e) {  
  42.      e.printStackTrace();  
  43.     }  
  44.   }  
  45.  }  
  46.   
  47.  /** 
  48.   * @param path 
  49.   */  
  50.  public static void readExcelByPOI(String path) {  
  51.   HSSFWorkbook book = null;  
  52.   InputStream input = null;  
  53.   try {  
  54.    input = new FileInputStream(path);  
  55.    book = new HSSFWorkbook(input);  
  56.    HSSFSheet sheet = book.getSheetAt(0);  
  57.    int rows = sheet.getPhysicalNumberOfRows();  
  58.    int cells = 0;  
  59.    HSSFRow row = null;  
  60.    HSSFCell cell = null;  
  61.    // for (Iterator iterator = sheet.rowIterator();  
  62.    // iterator.hasNext();) {  
  63.    // row = (HSSFRow) iterator.next();  
  64.    for (int i = 0; i < rows; i++) {  
  65.     row = sheet.getRow(i);  
  66.     cells = row.getPhysicalNumberOfCells();  
  67.     // for (Iterator iterator2 = row.cellIterator(); iterator2  
  68.     // .hasNext();) {  
  69.     // cell = (HSSFCell) iterator2.next();  
  70.     for (short j = 0; j < cells; j++) {  
  71.      cell = row.getCell(j);  
  72.      switch (cell.getCellType()) {  
  73.      case HSSFCell.CELL_TYPE_BLANK:  
  74.       System.out.print("");  
  75.       break;  
  76.      case HSSFCell.CELL_TYPE_BOOLEAN:  
  77.       System.out.print(cell.getBooleanCellValue());  
  78.       break;  
  79.      case HSSFCell.CELL_TYPE_ERROR:  
  80.       System.out.print(cell.getErrorCellValue());  
  81.       break;  
  82.      case HSSFCell.CELL_TYPE_FORMULA:  
  83.       System.out.print(cell.getCellFormula());  
  84.       break;  
  85.      case HSSFCell.CELL_TYPE_NUMERIC:  
  86.       System.out.print(cell.getNumericCellValue());  
  87.       break;  
  88.      case HSSFCell.CELL_TYPE_STRING:  
  89.       System.out.print(cell.getStringCellValue());  
  90.       break;  
  91.      }  
  92.      System.out.print("\t\t\t\t");  
  93.     }  
  94.     System.out.println();  
  95.    }  
  96.   } catch (Exception ex) {  
  97.    ex.printStackTrace();  
  98.   } finally {  
  99.    if (input != null)  
  100.     try {  
  101.      input.close();  
  102.     } catch (IOException e) {  
  103.      e.printStackTrace();  
  104.     }  
  105.   }  
  106.  }  
  107.   
  108.  /** 
  109.   * @param args 
  110.   */  
  111.  public static void main(String[] args) {  
  112.   
  113. //测试JXL比POI速度快  
  114.   long start = System.currentTimeMillis();  
  115.   readExcelByJXL("c:/questions.xls");  
  116.   System.out.println("===================AAAAAAAAAAAAAAAa============");  
  117.   System.out.println(System.currentTimeMillis() - start);  
  118.  }  
  119.   
  120. }  
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import jxl.Sheet;
import jxl.Workbook;

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;

public class Test {

 /**
  * @param path
  */
 public static void readExcelByJXL(String path) {
  Workbook book = null;
  InputStream input = null;
  try {
   input = new FileInputStream(path);
   book = Workbook.getWorkbook(input);
   Sheet sheet = book.getSheet(0);
   int columns = sheet.getColumns();
   for (int i = 0; i < sheet.getRows(); i++) {
    for (int j = 0; j < columns; j++)
     System.out.print(sheet.getCell(j, i).getContents()
       + "\t\t\t\t");
    System.out.println();
   }
  } catch (Exception ex) {
   ex.printStackTrace();
  } finally {
   if (book != null)
    book.close();
   if (input != null)
    try {
     input.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
  }
 }

 /**
  * @param path
  */
 public static void readExcelByPOI(String path) {
  HSSFWorkbook book = null;
  InputStream input = null;
  try {
   input = new FileInputStream(path);
   book = new HSSFWorkbook(input);
   HSSFSheet sheet = book.getSheetAt(0);
   int rows = sheet.getPhysicalNumberOfRows();
   int cells = 0;
   HSSFRow row = null;
   HSSFCell cell = null;
   // for (Iterator iterator = sheet.rowIterator();
   // iterator.hasNext();) {
   // row = (HSSFRow) iterator.next();
   for (int i = 0; i < rows; i++) {
    row = sheet.getRow(i);
    cells = row.getPhysicalNumberOfCells();
    // for (Iterator iterator2 = row.cellIterator(); iterator2
    // .hasNext();) {
    // cell = (HSSFCell) iterator2.next();
    for (short j = 0; j < cells; j++) {
     cell = row.getCell(j);
     switch (cell.getCellType()) {
     case HSSFCell.CELL_TYPE_BLANK:
      System.out.print("");
      break;
     case HSSFCell.CELL_TYPE_BOOLEAN:
      System.out.print(cell.getBooleanCellValue());
      break;
     case HSSFCell.CELL_TYPE_ERROR:
      System.out.print(cell.getErrorCellValue());
      break;
     case HSSFCell.CELL_TYPE_FORMULA:
      System.out.print(cell.getCellFormula());
      break;
     case HSSFCell.CELL_TYPE_NUMERIC:
      System.out.print(cell.getNumericCellValue());
      break;
     case HSSFCell.CELL_TYPE_STRING:
      System.out.print(cell.getStringCellValue());
      break;
     }
     System.out.print("\t\t\t\t");
    }
    System.out.println();
   }
  } catch (Exception ex) {
   ex.printStackTrace();
  } finally {
   if (input != null)
    try {
     input.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
  }
 }

 /**
  * @param args
  */
 public static void main(String[] args) {

//测试JXL比POI速度快
  long start = System.currentTimeMillis();
  readExcelByJXL("c:/questions.xls");
  System.out.println("===================AAAAAAAAAAAAAAAa============");
  System.out.println(System.currentTimeMillis() - start);
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值