POI读取Excel浅谈

来源:互联网 发布:执行偏差算法 is 编辑:程序博客网 时间:2024/06/05 15:38


http://www.cnblogs.com/forbreak/archive/2012/07/10/2584961.html

转: POI读取Excel浅谈 
先看代码,挨句解释: 
一般遍历使用两种方式,1:得到总的行数和每行的列数,然后循环。2:使用迭代 
先看第一种: 

复制代码
Java代码  package com.golden.test;     import java.io.File;   import java.io.FileInputStream;     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;     /**  *   * @author 崔素强  *   */  public class PoiReadXls2 {       public static void main(String[] args) {           File f = new File("c:\\a.xls");           try {               FileInputStream is = new FileInputStream(f);               HSSFWorkbook wbs = new HSSFWorkbook(is);               HSSFSheet childSheet = wbs.getSheetAt(0);               // System.out.println(childSheet.getPhysicalNumberOfRows());               System.out.println("有行数" + childSheet.getLastRowNum());               for (int j = 0; j < childSheet.getLastRowNum(); j++) {                   HSSFRow row = childSheet.getRow(j);                   // System.out.println(row.getPhysicalNumberOfCells());                   // System.out.println("有列数" + row.getLastCellNum());                   if (null != row) {                       for (int k = 0; k < row.getLastCellNum(); k++) {                           HSSFCell cell = row.getCell(k);                           if (null != cell) {                               switch (cell.getCellType()) {                               case HSSFCell.CELL_TYPE_NUMERIC: // 数字                                   System.out.print(cell.getNumericCellValue()                                           + "   ");                                   break;                               case HSSFCell.CELL_TYPE_STRING: // 字符串                                   System.out.print(cell.getStringCellValue()                                           + "   ");                                   break;                               case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean                                   System.out.println(cell.getBooleanCellValue()                                           + "   ");                                   break;                               case HSSFCell.CELL_TYPE_FORMULA: // 公式                                   System.out.print(cell.getCellFormula() + "   ");                                   break;                               case HSSFCell.CELL_TYPE_BLANK: // 空值                                   System.out.println(" ");                                   break;                               case HSSFCell.CELL_TYPE_ERROR: // 故障                                   System.out.println(" ");                                   break;                               default:                                   System.out.print("未知类型   ");                                   break;                               }                           } else {                               System.out.print("-   ");                           }                       }                   }                   System.out.println();               }           } catch (Exception e) {               e.printStackTrace();           }       }   }  
0 0
原创粉丝点击