2017-01-25 41 views
0

我在我的excel閱讀器中遇到問題。它不讀取空白或空白單元格,並跳到具有值的下一個單元格。在java中讀取Excel文件沒有讀取或計算空白單元格爲空

例子..

header1 | header2 | header3 | header4 
row1 |   | row3 | row4 
row1 | row2 |   | row4 
     | row2 | row3 | 

結果:

header1 | header2 | header3 | header4 
row1 | row3 | row4 | 
row1 | row2 | row4 | 
row2 | row3 |   | 

預期發生:

header1 | header2 | header3 | header4 
row1 | null | row3 | row4 
row1 | row2 | null | row4 
null | row2 | row3 | null 

「」(空字符串)

這裏是我的代碼:

readXLSFile方法:

private boolean readXLSFile(String batchRunNbr, String filename) throws IOException, ParseException { 

     List sheetData = new ArrayList(); 
     FileInputStream fis = null; 
     try { 
      if ((filename.endsWith(".xlsx")) || (filename.endsWith(".XLSX"))) { 
       log.info("Reading xlsx file..."); 
       fis = new FileInputStream(filename); 
      } else{ 
       this.errorMessageTxt = this.errorMessageTxt+this.htmlNextLine+ 
         "Unable to process the file. Please save the file to the latest Excel *.xlsm or *.xlsx file."; 
       return true; 
      } 


      XSSFWorkbook workbook = new XSSFWorkbook(fis); 
      XSSFSheet sheet = workbook.getSheetAt(0); 

      Iterator<Row> rows = sheet.rowIterator(); 
      int counter = 0; 
      while (rows.hasNext()) { 
       counter++; 
       XSSFRow row = ((XSSFRow) rows.next()); 
       Iterator<Cell> cells = row.cellIterator(); 

       List data = new ArrayList(); 
       while (cells.hasNext()) { 
        Cell cell = (XSSFCell) cells.next(); 

        data.add(cell); 
       } 
       sheetData.add(data); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
      this.errorMessageTxt = this.errorMessageTxt+this.htmlNextLine+ 
        e.getMessage()+this.htmlNextLine+e.getCause(); 
      return true; //There is an error, return true. 
     } finally { 
      if (fis != null) { 
       fis.close(); 
      } 
     } 

     processExcelSheet(sheetData, batchRunNbr, filename); 

這裏是我的processExcelSheeet方法:

private void processExcelSheet(List sheetData, String batchRunNbr, String inputFileName) { 
     DateFormat formatter; 
     formatter = new SimpleDateFormat("dd.MM.yyyy"); 
     boolean firstTime = true; 
     try { 
      for (int i = 0; i < sheetData.size(); i++) { 
       List list = (List) sheetData.get(i); 
       if (firstTime) { 
        firstTime = false; 
       } else { 
        for (int ii = 0; ii < list.size(); ii++) { 

         XSSFCell cell = (XSSFCell) list.get(ii); 
         switchCase(formatter, ii, cell); 
        } 

        insertToStaging(batchRunNbr, inputFileName);//This method inserts the data to the database based on what it reads above. 
       } 
       //log.info("COMPLETED!"); 
      } 

     } catch (Exception e) { 
      log.info("loadStagingTable Error:" + e); 
      //this.errorMessageTxt = this.errorMessageTxt+this.htmlNextLine+ 
      //  "Error Loading to KLTL Data Staging. "+e.getMessage()+","+e.getCause(); 
      e.printStackTrace(); 
     } 
    } 

我懷疑問題是關於row.hasNext? 請幫忙..非常感謝。如果您需要更多詳細信息,請在下面進行評論。

+0

你檢查過'sheetData'嗎?根據你的結果,它不應該與'rows.hasNext'或'cells.hasNext'有關,如果它返回false,你的結果集中不會有第4行 – Prisoner

+0

哦..我試着把放在空單元格上,它相應地讀取。但空白或空白單元格,它跳過空白單元格..我真的不知道我的代碼是什麼問題..找不到任何解決方案。 –

+0

那麼你有沒有在調試器中檢查'sheetData'?或在'sheetData.add(data)'前面檢查'data.size()'' – Prisoner

回答

0

到目前爲止,這是非常酷的代碼!

只是一個瘋狂的猜測:我敢打賭,當你得到「sheet.rowIterator()」時,你失去了你的空值。這是有道理的b/c「迭代器」經常忽略空值。

那麼......如果是這樣的話......你如何解決它?

也許這將工作:

定義後 「表」 嘗試做這樣的事情

for(int i = 0; i<sheet.getLastRowNum(); i++){ 
    XSSFRow row = ((XSSFRow) sheet.getRow(i)); 
    for(int j = 0; j<row.getLastCellNum(); j++){ 
    String val = row.getCell(j).getStringCellValue(); 
    System.out.println(val.isEmpty()?"null":val);//todo: rewrite as readable "if" statement 
    } 
} 

;)編碼快樂!

+0

對不起,但這段代碼不起作用。它返回NullPointer。 –