`
vwpolo
  • 浏览: 190867 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

使用Apache POI读取Excel文件

阅读更多

    Apache POI是Apache软件基金会的开放源码函式库,用来帮助Java程序读写Microsoft Office的格式档案。POI提供了下面这几种类型对Microsoft Office的格式档案进行解析:

 

      HSSF - 提供读写Microsoft Excel XLS格式档案的功能。

      XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。

      HWPF - 提供读写Microsoft Word DOC格式档案的功能。

      HSLF - 提供读写Microsoft PowerPoint格式档案的功能。

      HDGF - 提供读Microsoft Visio格式档案的功能。

      HPBF - 提供读Microsoft Publisher格式档案的功能。  

   你可以访问POI的主页http://poi.apache.org/ 下载你喜欢的版本和了解更多的信息.

   这里只介绍使用POI读取Excel文件,在读取Excel时首先要定位Excel文件的位置,然后通过POI的API生成一个工作表HSSFWorkbook对象:

   

    File file = new File(filePath);
    FileInputStream fint = new FileInputStream(file);
    POIFSFileSystem poiFileSystem = new POIFSFileSystem(fint);
    HSSFWorkbook workbook = new HSSFWorkbook(poiFileSystem);

 可以通过HSSFWorkbook提供的getSheetAt(int sheetNum)访问对应的子工作表HSSFSheet,序号从'0'开始.在获得HSSFSheet对象后通过sheet.getRow(rowNum)方法获得子工作表的指定行HSSFRow,HSSFRow提供了getCell(short)方法访问其中的单元格对象.在处理单元格的时候要注意,不能简单使用HSSShellgetStringCellValue()方法获得单元格中的值,在我使用的版本中(2.0)没有提供自动类型转换的功能,所以在取值的时候要根据类型判断:

public static String getCell(HSSFCell cell) {
        if (cell == null)
            return "";
        switch (cell.getCellType()) {
          case HSSFCell.CELL_TYPE_NUMERIC:
              return cell.getNumericCellValue() + "";
          case HSSFCell.CELL_TYPE_STRING:
              return cell.getStringCellValue();
          case HSSFCell.CELL_TYPE_FORMULA:
              return cell.getCellFormula();
          case HSSFCell.CELL_TYPE_BLANK:
              return "";
          case HSSFCell.CELL_TYPE_BOOLEAN:
              return cell.getBooleanCellValue() + "";
          case HSSFCell.CELL_TYPE_ERROR:
             return cell.getErrorCellValue() + "";
         }
         return "";
      }

 

 HSSFCell没有提供时间类型常量,这时候你只能根据自己判断是否要将它转换成时间格式了: cell.getDateCellValue();

下面是一个简单的例子,你可以参考一下:

/**
 *  打印Excel文件 。 
 * @author vwpolo
 * <p>2009-9-15</p>
 */
public class PrintExcelTest {

  public static void main(String[] args) throws Exception {
    File file = new File("User.xls");
    FileInputStream fint = new FileInputStream(file);
    POIFSFileSystem poiFileSystem = new POIFSFileSystem(fint);
    HSSFWorkbook workbook = new HSSFWorkbook(poiFileSystem);
    HSSFSheet sheet = workbook.getSheetAt(0);
    HSSFRow rowTitle = sheet.getRow(0);
    Iterator<HSSFCell> iterTitle = rowTitle.cellIterator();
    while(iterTitle.hasNext()) {
      System.out.print(iterTitle.next().getStringCellValue()+"  ");
    }
    System.out.println("");
    HSSFRow rowUser = sheet.getRow(1);
    Iterator<HSSFCell> iterUser = rowUser.cellIterator();
    while(iterUser.hasNext()) {
      System.out.print(getCell(iterUser.next())+"  ");
    }
    System.out.println("\n");
    System.out.println("出生日期:"+rowUser.getCell((short)3).getDateCellValue().toLocaleString());
  }
  
  public static String getCell(HSSFCell cell) {
    if (cell == null)
      return "";
    switch (cell.getCellType()) {
      case HSSFCell.CELL_TYPE_NUMERIC:
        return cell.getNumericCellValue() + "";
      case HSSFCell.CELL_TYPE_STRING:
        return cell.getStringCellValue();
      case HSSFCell.CELL_TYPE_FORMULA:
        return cell.getCellFormula();
      case HSSFCell.CELL_TYPE_BLANK:
        return "";
      case HSSFCell.CELL_TYPE_BOOLEAN:
        return cell.getBooleanCellValue() + "";
      case HSSFCell.CELL_TYPE_ERROR:
        return cell.getErrorCellValue() + "";
    }
    return "";
  }
}

 

这里的User.xls文件时一个模板,

 

http://www.blogjava.net/images/blogjava_net/vwpolo/apachepoiexcel/userExcel.JPG

A1C1的单元格格式是常规格式,B1E1的单元格格式是文本,D1的单元格格式是日期

   运行上面的例子会输出:

 姓名  员工编号  所属公司  出生日期  身份证号码  
 张三  000018   上海      32117.0   370684198712066666  

 出生日期:1987-12-6 0:00:00

 在那个迭代方法中无法对日期类型的判断,所以输出格式存在问题,可以将日期格式额外处理。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics