【问题标题】:Results address of empty cell in Excel sheet using XSSF使用 XSSF 的 Excel 工作表中空单元格的结果地址
【发布时间】:2014-07-17 12:16:13
【问题描述】:

我正在使用 POI 的 XSSF 阅读 Excel 表。 Excel 表格有数千行用户信息,如用户名、地址、年龄、部门等。

我可以成功读取和写入 Excel 表格,但我想找到空/空单元格的地址并希望将其打印到另一张表格中

我想要的示例结果是:

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

感谢并感谢您的讨论和回复。

【问题讨论】:

    标签: java excel selenium-webdriver apache-poi xssf


    【解决方案1】:

    您需要检查 Null 和 Blank 单元格。空单元格是从未使用过的单元格,空白单元格是已被使用或以某种方式设置样式的单元格,Excel 已决定将它们保留在文件中

    控制此提取的最简单方法是使用MissingCellPolicy。你的代码可以是这样的:

    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());
          }
    }
    

    您可以在POI docs on iterating over rows and cells 中找到更多相关信息

    【讨论】:

    • 感谢您的回复,我想打印空单元格地址,甚至遍历所有单元格
    • 将其切换为Row.RETURN_NULL_AS_BLANK,然后打开空白单元格类型,然后您就应该完成了!
    • 。一切都很好,如何获取那个空白单元格的地址。请帮助我
    • 使用CellReference,它会为你转换,查看我的编辑示例
    • 作为详细模式的答案,我在 cell.setCellValue 中使用了“CellReference”,但如果下一个单元格为空,则会将异常显示为空指针
    【解决方案2】:

    据我理解你的问题是正确的,这应该可以解决问题:

    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() ) );
    }
    

    【讨论】:

    • verbose-mode 感谢您的快速回复..是的,这是一个不错的技巧,但显示第一个空单元格就是这样
    • 它对你有用吗?我是的,不要忘记接受答案。如果没有,请随时向我发布您的问题。
    • 详细是的,这个技巧对我实现我的结果真的很有帮助,那就是齿轮..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 2011-05-11
    • 2012-06-21
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    相关资源
    最近更新 更多