2017-08-03 47 views
0

我有3列與Excel文檔中的數據。 例如,在中間的列上,有一些沒有數據的空白空間。計算數據矩陣內部的空單元格xlsx

我已經創建了一個對象來按照排序的方式按行檢索數據。

我遇到的問題是,我無法將空單元存儲爲空,存在於我的對象中,而且我正在丟失具有此問題的數據。 所以就像在下面的例子中,我只從行中獲取數據,每列都寫有一些數據。如果一列是空的,則不會返回任何數據。

enter image description here

我得到的一般方法單元格的值:

public static Object getCellValue(Cell cell) 
{ 

    Object cellValue = null; 
    if(cell.getCellTypeEnum() == CellType.STRING){ 

     cellValue = cell.getStringCellValue(); 

    }else if(cell.getCellTypeEnum() == CellType.NUMERIC){ 

     cellValue = cell.getNumericCellValue(); 

    }else if(cell.getCellTypeEnum() == CellType.BOOLEAN){ 

     cellValue = cell.getBooleanCellValue(); 

    }else if(cell.getCellTypeEnum() == CellType.BLANK){ 

     cellValue = ""; 

    } 
    return cellValue; 

} 

創建細胞的對象。例如,對輸出進行排序非常重要:列1行1必須嚴格分配給同一行的第2列和第3列。即使單元格爲空白/空,數據中也必須保持它們的順序矩陣。

public class ColumnsOrder { 

private String fristColumn; 
private String secondColumn; 
private String thirdColumn; 

public ColumnsOrder() { 

} 

public ColumnsOrder(String fristColumn) { 
    super(); 
    this.fristColumn = fristColumn; 
} 

public ColumnsOrder(String fristColumn, String secondColumn) { 
    super(); 
    this.fristColumn = fristColumn; 
    this.secondColumn = secondColumn; 
} 

public ColumnsOrder(String fristColumn, String secondColumn, 
     String thirdColumn) { 
    super(); 
    this.fristColumn = fristColumn; 
    this.secondColumn = secondColumn; 
    this.thirdColumn = thirdColumn; 
} 

public String getFristColumn() { 
    return fristColumn; 
} 

public String getSecondColumn() { 
    return secondColumn; 
} 

public String getThirdColumn() { 
    return thirdColumn; 
} 

} 

在這裏,我從xlsx中拿出數據,可能在這裏我做錯了什麼。

for(rowIndex = rowStart; rowIndex <= rowEnd; rowIndex++) { 

       Cell cellOne = null; 
       Cell cellTwo = null; 
       Cell cellThree = null; 

       row = sheet.getRow(rowIndex); 

       if (row != null) { 
// getting data from each column 
          cellOne  = (Cell) ((!CheckIfCellIsEmpty.isCellEmpty(row.getCell(0))) ? row.getCell(0) : ""); 
          cellTwo  = (Cell) ((!CheckIfCellIsEmpty.isCellEmpty(row.getCell(1))) ? row.getCell(1) : " "); 
          cellThree = (Cell) ((!CheckIfCellIsEmpty.isCellEmpty(row.getCell(2))) ? row.getCell(2) : " "); 

          // Creating the object using its constructor 
          columnsExcelTemp.add(new ColumnsOrder( 
                    (String)GetCellValue.getCellValue(cellOne), 
                    (String)GetCellValue.getCellValue(cellTwo), 
                    (String)GetCellValue.getCellValue(cellThree) 
                   ) 
               ); 
    } 
workbook.close(); 
} 

檢查IsEmpty函數

public static boolean isCellEmpty(final Cell cell) 
{ 

    if (cell == null) 
    { 
     return true; 
    } 

    /* 
    if (cell.getCellTypeEnum() == CellType.BLANK) { 
     return false; 
    } 

    if (cell.getCellTypeEnum() == CellType.STRING) 
    { 
     return false; 
    } 

    if(cell.getStringCellValue().isEmpty()) 
    { 
     return false; 
    } 
    */ 

    return false; 

} 
+1

您的最終目標是什麼?可能有不同的方法來處理這種情況 – Zac

+0

包含此excel數據表的任何行的對象的列表,即使其中一列爲空(在該區域內) –

回答

0

似乎我已經用Apache POI庫內置的函數解決了這個問題。

      cellOne  = row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); 
          cellTwo  = row.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); 
          cellThree = row.getCell(2, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); 

Row.MissingCellPolicy.CREATE_NULL_AS_BLANK添加第二個參數爲我的表,解決了實際問題,裏面的空細胞給空值。

如果有人需要Java-Apache POI的幫助,請在這裏寫一條評論。

0

的問題是,你所看到的在一個Excel工作表的空單元格可以是與像「一個值單元格」,也可以是不存在的脫穎而出。如果你使用row.getCell()總是檢查它是否爲null!

+0

我正在使用public static boolean isCellEmpty(最終細胞) –