2017-06-23 154 views
0

如何從我的Excel xlsx表單中獲取特定的單元格值? 例如我想從小區6和8 得到所有值這是我做了什麼 - 我得到的整排Java - Apache POI讀取特定單元格中的XSSF文件

public void readExcelFile() { 

    try { 
     InputStream ExcelFileToRead = new FileInputStream("D:\\20161111_Gesamtliste_aller_Anschreiben.xlsx"); 
     XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead); 

     XSSFSheet sheet = wb.getSheetAt(0); 
     XSSFRow row; 
     XSSFCell cell; 
     Iterator rows = sheet.rowIterator(); 
     int read = 0; 

     while (rows.hasNext()) { 
      read++; 
      row = (XSSFRow) rows.next(); 

      Iterator cells = row.cellIterator(); 
      while (cells.hasNext()) { 
       cell = (XSSFCell) cells.next(); 

       if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) { 
        System.out.print(cell.getStringCellValue() + " "); 
       } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { 
        System.out.print(cell.getNumericCellValue() + " "); 
       } else { 
       } 
      } 
      System.out.println(); 

     } 

     System.out.println(read); 

    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 

} 
+1

請問[this](https://stackoverflow.com/a/12857969/6565719)有幫助嗎? –

回答

2
Row row = (XSSFRow) rows.next(); 
    int rowcellnum = row.getLastCellNum(); 
    for (int cn = 0; cn < rowcellnum; cn++) { 
     String cellVal = ""; 
     Cell cell = row.getCell(cn); 
     if (cell == null) { 
      //HANDLE NULL 
     } 
     if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) { 
      System.out.print(cell.getStringCellValue() + " "); 
     } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { 
         System.out.print(cell.getNumericCellValue() + " "); 
     } else { 
     } 
    } 

基於指數即CN您可以訪問特定的細胞。

相關問題