poi读excel小例子

比较简单,直接看代码:


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 PoiReadExcel {

public static void readExcel(){
File f = new File("C:\\Documents and Settings\\Administrator\\桌面\\123.xls");
try {
            FileInputStream is = new FileInputStream(f);
            HSSFWorkbook wbs = new HSSFWorkbook(is);
            HSSFSheet childSheet = wbs.getSheetAt(0);
            System.out.println("有行数1 :" + childSheet.getLastRowNum());
            int j;
            for (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((short)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();
                    }
                }
                
                System.out.println();
                
            }
            System.out.println();
            System.out.println("有行数2 " + childSheet.getLastRowNum()+"--"+j);
        } catch (Exception e) {
            e.printStackTrace();
        }
}


}

原文地址:https://www.cnblogs.com/qlong8807/p/2741579.html