簡體   English   中英

如果第一個和最后一個單元格值為空或空白,如何讀取 Excel 工作表行的每個單元格的值?

[英]How to read each and every cell's value of the row of Excel sheet if 1st and last cell value is Null or Blank?

這是excel表

-----------------------------------------------------------------------
col1 | Col2 | col3 | col4 | col5| col6| col7|   and so on
-----------------------------------------------------------------------
1    | 2    |   3  |   4   |  5 |  6|    7|     and so on
------------------------------------------------------------------------
     |      |   3  |   4   |  5 |   |     |     and so on
------------------------------------------------------------------------

我的輸出應該是 Blank Blank 3 4 5 Blank Blank

我正在使用Iterator<Cell> cell=row.cellIterator讀取此 Excel 表我想使用 Blank 讀取每個單元格值,但使用CellIterator我只能從第二行讀取 3 4 5 。

如何閱讀帶有空格的第二行? 我還看到如果第一個單元格和最后一個單元格不是空白,那么CellIterator成功讀取了空白值。

如果該行的第一個和最后一個單元格為空,如何在java中讀取特定行的每個單元格值?

我將假設您使用 Apache POI 進行 Excel 操作。

CellIterator 將只返回已在文件中定義的單元格,這主要意味着具有值或格式的單元格。

參考http://poi.apache.org/spreadsheet/quick-guide.html#Iterate+over+cells%2C+with+control+of+missing+%2F+blank+cells

迭代單元格,控制缺失/空白單元格

在某些情況下,在迭代時,您需要完全控制如何處理缺失或空白行和單元格,並且您需要確保訪問每個單元格,而不僅僅是文件中定義的單元格。 (CellIterator 將只返回文件中定義的單元格,這些單元格主要是具有值或樣式的單元格,但它取決於 Excel)。

在這種情況下,您應該獲取一行的第一列和最后一列信息,然后調用 getCell(int, MissingCellPolicy) 來獲取單元格。 使用 MissingCellPolicy 來控制如何處理空白或空單元格。

// Decide which rows to process
int rowStart = Math.min(15, sheet.getFirstRowNum());
int rowEnd = Math.max(1400, sheet.getLastRowNum());

for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
   Row r = sheet.getRow(rowNum);
   if (r == null) {
      // This whole row is empty
      // Handle it as needed
      continue;
   }

   int lastColumn = Math.max(r.getLastCellNum(), MY_MINIMUM_COLUMN_COUNT);

   for (int cn = 0; cn < lastColumn; cn++) {
      Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
      if (c == null) {
         // The spreadsheet is empty in this cell
      } else {
         // Do something useful with the cell's contents
      }
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM