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

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

我可以读取和写入Excel工作表,但我想find空/空单元格的地址,并希望打印它作为结果在另一个工作表

示例结果我想要的是:

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

感谢和欣赏的讨论和答复。

您需要检查Null和空白单元格。 空单元格是从来没有使用过的,空格的是那些已经被使用过或以某种方式devise的样式,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文档中find关于遍历行和单元格的更多信息

据我了解你的问题是正确的,这应该做的伎俩:

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