简体   繁体   English

使用XSSF的Excel工作表中空白单元格的结果地址

[英]Results address of empty cell in Excel sheet using XSSF

I am reading an Excel sheet using POI's XSSF . 我正在使用POI的XSSF阅读Excel工作表。 The Excel sheet has thousands of rows of user information like user name, address, age, department etc. Excel工作表包含数千行用户信息,例如用户名,地址,年龄,部门等。

I can read and write the Excel sheet successfully but i want to locate empty/null cell's address and want to print it as result in another sheet 我可以成功读写Excel工作表,但是我想找到空/空单元格的地址,并希望将其打印在另一工作表中

example result what i want is: 示例结果我想要的是:

Empty cell  : C5
Empty cell  : E7
Empty cell  : H8

Thanks and appreciate for discussions and replies. 感谢并感谢您的讨论和答复。

You need to check for both Null and Blank cells. 您需要检查Null和Blank单元格。 Null cells are ones that have never been used, Blank ones are ones that have been used or styled in some way that Excel has decided to keep them around in the file 空单元格是从未使用过的单元格,空白单元格是已经使用或以Excel决定将其保留在文件中的方式设置的样式

The easiest way to control this fetching is with a MissingCellPolicy . 控制此提取的最简单方法是使用MissingCellPolicy Your code can then be something like: 然后,您的代码可以是这样的:

Row r = getRow(); // Logic here to get the row of interest

// Iterate over all cells in the row, up to at least the 10th column
int lastColumn = Math.max(r.getLastCellNum(), 10);

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
         // eg...
         System.out.println("There is data in cell " + (new CellReference(c)).formatAsString());
      }
}

You can find more on this in the POI docs on iterating over rows and cells 您可以在POI文档中找到有关迭代行和单元格的更多信息

As far as I unterstood your question right, this should do the trick: 据我对您的问题的理解正确,这应该可以解决问题:

XSSFCell cell = (XSSFCell) row.getCell( index );
if( cell == null )
{
  cell = (XSSFCell) row.createCell( index );
}
if( cell.getStringCellValue().trim().isEmpty() )
{
  String cellRef = CellReference.convertNumToColString( cell.getColumnIndex() );
  System.err.println( "Empty cell found: " + cellRef
          + Integer.toString( row.getRowNum() ) );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM