POI 入門-----讀取 Excel 07以上版本
阿新 • • 發佈:2018-12-17
- 用for迴圈讀取Excel
package com.csii.excel; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Iterator; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class TestReadExcel { public static void main(String[] args) throws IOException { String filePath = "C:\\Users\\ljj\\Desktop\\workDay\\我的事件流\\公共交易試.xlsx"; File file = new File(filePath); FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } // 07版本以後的excel 物件 XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fileInputStream); // 獲取第一個工作表sheet XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); // 獲取非空行 int rowNum = xssfSheet.getLastRowNum(); // 用於計數 int i = 1; for (int indexRow = 0; indexRow < rowNum; indexRow++) {// 迴圈每一行 XSSFRow xssfRow = xssfSheet.getRow(indexRow); int colNum = xssfRow.getLastCellNum(); // 迴圈每一列 for (int colIndex = 0; colIndex < colNum; colIndex++) {// 迴圈每一列 // 建立一個 單元格(Cell) 物件 Cell cell = xssfRow.getCell(colIndex); if (cell != null) {// 單元格非 null,才輸出 String value = ""; switch (cell.getCellType()) { // 單元格的三種類型:字元(STRING)、數值 (NUMERIC)、空白(BLANK) case STRING: value = cell.getStringCellValue(); break; case NUMERIC: // 轉換成string型別 value = String.valueOf(cell.getNumericCellValue()); break; case BLANK: break; default: break; } System.out.println(i++ + ":" + value); } else {// 單元格為null,繼續尋找下一列的單元格 continue; } } } } }
測試資料如下:
2. 用 Iterater 來迴圈,此方法比for迴圈更方便
public class ExcelRead{ private static XSSFRow row; @SuppressWarnings("unused") public static void main(String[] args) throws IOException { String fileName = "C:\\Users\\ljj\\Desktop\\testExcel\\wc.xlsx"; String[] arrExcel = readExcel(fileName); int i = 0; for (String string : arrExcel) { System.out.println(i + ":" + string); } } /** * 返回一個欄位陣列 * @param fileName * @return String[] * @throws IOException */ @SuppressWarnings("unused") public static String[] readExcel(String fileName) throws IOException { File file = new File(fileName); if (file == null) { System.out.println("讀取檔案失敗!"); } FileInputStream fileInputStream = new FileInputStream(file); // 建立excel物件 XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fileInputStream); if (file.exists() && file.isFile()) { System.out.println("open file succeed!"); } else { System.out.println("Error to open openworkbook.xlsx file!"); } // 獲取excel表物件 XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); Iterator<Row> rowIterator = xssfSheet.iterator(); // 欄位名 String strName = ""; // 級欄位數 int i = 0; // 存欄位陣列 String[] arr = new String[24]; while (rowIterator.hasNext()) { row = (XSSFRow) rowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { // 建立單元格物件 Cell cell = cellIterator.next(); switch (cell.getCellType()) {// 根據單元格型別分別獲取獲取值 // string 型別 case STRING: // 獲取單元格值 strName = cell.getStringCellValue(); strName = strName.trim(); // 存入陣列中輸出 arr[i++] = strName; break; // number型別 case NUMERIC: // 獲取單元格值 strName = String.valueOf(cell.getNumericCellValue()); strName = strName.trim(); // 存入陣列中輸出 arr[i++] = strName; break; default: break; } } } return arr; } }