我有一张excel表格。在工作表中,如果单元格1不为空,则必须检查单元格2、3、4和5中至少有两个单元格不应为空。如何通过java POI API实现这一点?
发布于 2013-11-28 11:30:01
这很容易做到!假设您想要依次检查每一行,您可能希望如下所示:
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");
}
}
}
https://stackoverflow.com/questions/20263774
复制