POI讀取Excel

原理:先讀取有多少行多少列,然後根據行裏遍歷。
原文:http://www.iteye.com/topic/759437

package cn.itcast.ajax;
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; 
public class AjaxDemo1 {
    public static void main(String[] args) {  
        File f = new File("c:\\c.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();  
        }  
    }  

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章