我有一个 Excel 工作表,当我在 java 中看到空单元格时,我需要打印相邻单元格的值。
我编写了代码来打印 Excel 工作表中的值,我也找到了空单元格,但我不知道如何在看到空单元格时打印相邻单元格的值
public class empty {
public static void main(String args[]) throws Exception {
InputStream ExcelFileToRead = new FileInputStream("C:\\Users\\GOMATHI\\Desktop\\data1.xls");
HSSFWorkbook wb = new HSSFWorkbook(ExcelFileToRead);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row;
HSSFCell cell;
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
row = (HSSFRow) rows.next();
Iterator cells = row.cellIterator();
while (cells.hasNext()) {
cell = (HSSFCell) cells.next();
if (cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
// logic goes here. I don't know.
System.out.println(no);
}
System.out.println(cell.toString());
}
}
// System.out.println();
}
}
Input: Excel File
Id Value
01 200
02 201
03 203
04
05 205
输出: 第 4 个单元格的值是空的,因此我需要打印其相应的 ID。 04
最佳答案
首先,我认为您的测试 if (cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK)
是一半没用,因为 cell
永远不应该无效的。
假设如此,您可以使用 row.getCell(cell.getColumnIndex() - 1)
关于java - 使用 Java 打印 Excel 工作表中的空单元格相邻单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57075276/