解析Excel數據經常使用的方式就是使用POI和JXL工具了,這兩種工具使用起來有些相似,這裏記錄一下使用方式提供個參考工具
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),而且判斷單元格類型使用的方法也有些許不一樣,須要注意下。spa
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){ cell.getContents() //空值 }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()); */