我有一张 Excel 表格。在该工作表中,我必须检查如果 1 号单元格不为空,则 2、3、4 和 5 号单元格中的至少两个单元格不应为空。我如何通过 java POI API 做到这一点?
最佳答案
这很容易做到!假设您想依次检查每一行,您需要如下内容:
for (Row r : sheet) {
Cell c = r.getCell(0, Row.RETURN_BLANK_AS_NULL);
if (c == null) {
// First cell is empty
} else {
// First cell has data
// Count how many of the next 4 cells are filled
int next4Filled = 0;
for (int i=1; i<=4; i++) {
c = r.getCell(i, Row.RETURN_BLANK_AS_NULL);
if (c != null) next4Filled++;
}
if (next4Filled < 2) {
throw new IllegalStateException("Too many cells empty in row");
}
}
}
关于java - 我如何通过java检查Excel文件中的单元格对于特定的第一个单元格值不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20263774/