1. 程式人生 > >解析Excel數據

解析Excel數據

創建 xls ole 單元格 else error: 工具 hashmap value

解析Excel數據常用的方式就是使用POI和JXL工具了,這兩種工具使用起來有些類似,這裏記錄一下使用方式提供個參考

POI使用

File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
workbook = WorkbookFactory.create(fis);
//poi3.6使用一下方式創建workbook
//workbook = new HSSFWorkbook(fis);  //.xls格式
int sheetNum = workbook.getNumberOfSheets();//
Sheet的數量 System.out.println("共[" + sheetNum + "]個工作表"); for (int i = 0; i < sheetNum; i++) { System.out.println("開始讀取第[" + (i + 1) + "]個工作表"); Sheet sheet = workbook.getSheetAt(i);// 第i個Sheet String sheetName = sheet.getSheetName(); System.out.println("工作表名稱:" + sheetName);
int rowNum = sheet.getPhysicalNumberOfRows(); for (int j = 0; j < rowNum; j++) { System.out.println("開始讀取第[" + (j + 1) + "]行數據:"); Row row = sheet.getRow(j); int cellNum = row.getPhysicalNumberOfCells(); for (int k = 0; k < cellNum; k++) { Cell cell
= row.getCell(k); Object cellValue = null; //根據單元格類型拿數據 CellType cellType = cell.getCellTypeEnum(); switch (cellType) { case BOOLEAN: cellValue = cell.getBooleanCellValue(); break; case ERROR: cellValue = cell.getErrorCellValue(); break; case NUMERIC: cellValue = cell.getNumericCellValue(); break; case STRING: cellValue = cell.getStringCellValue(); break; default: break; } //poi3.6使用以下方式判斷單元格類型 /* switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: cellValue = cell.getBooleanCellValue(); break; case Cell.CELL_TYPE_ERROR: cellValue = cell.getErrorCellValue(); break; case Cell.CELL_TYPE_NUMERIC: cellValue = cell.getNumericCellValue(); break; case Cell.CELL_TYPE_STRING: cellValue = cell.getStringCellValue(); break; default: break; } */ System.out.printf("每個單元格的值:"+cellValue); } } }

註意POI版本使用上有些許的不同,poi3.6及以上.xls和.xlsx格式的Excel對應的是不同的workbook 實現類, HSSFWorkbook只能操作excel2003以下版本(.xls),XSSFWorkbook只能操作excel2007以上版本(.xlsx),並且判斷單元格類型使用的方法也有些許不同,需要註意下。

JXL的使用

Workbook book = Workbook.getWorkbook(new File(filePath));
// 獲得第一個表格對象
Sheet sheet = book.getSheet(0);  //這裏0代表只解析第一個sheet頁
// 拿到表格的行數
int row = sheet.getRows();
// 拿到表格的列數
int col = sheet.getColumns();
for (int j = 0; j < col; j++) {
    map = new HashMap<Integer, String>();
    for (int i = 0; i < row; i++) {
        Cell cell = sheet.getCell(j, i);
        String custContent = cell.getContents();
        //每個單元格的值
        System.out.printf("每個單元格的值:"+custContent);
    }
    
/*      //判斷單元格類型
        CellType cellType = cell.getType();
        if(cellType == CellType.EMPTY){
                                                           //空值
        }else if(cellType == CellType.BOOLEAN){
            ((BooleanCell) cell).getValue();            //布爾值
        }else if(cellType == CellType.BOOLEAN_FORMULA){
            ((BooleanFormulaCell) cell).getValue();    //布爾值公式
        }else if(cellType == CellType.DATE){
            ((DateCell) cell).getDate();                //日期類
        }else if(cellType == CellType.DATE_FORMULA){
            ((DateFormulaCell)cell).getDate();            //日期公式
        }else if(cellType == CellType.NUMBER){
            ((NumberCell) cell).getValue();            //數字
        }else if(cellType == CellType.NUMBER_FORMULA){
            ((NumberFormulaCell) cell).getValue();        //數字公式
        }else if(cellType == CellType.STRING_FORMULA){
            ((StringFormulaCell) cell).getString();    //字符串公式
        }else if(cellType == CellType.ERROR){
            ((ErrorCell) cell).getContents();            //錯誤消息
        }else if(cellType == CellType.FORMULA_ERROR){
            ((ErrorFormulaCell) cell).getContents();    //公式錯誤
        }else element.setValue(cell.getContents());    
*/

解析Excel數據