【发布时间】:2021-10-15 19:53:17
【问题描述】:
在我的代码中,我试图只读取“A”列中的数据。在屏幕截图中,我附上了示例数据的外观。由于空行,我在读取内容时遇到问题,因为它抛出空指针异常。我还需要验证空白行。请给我你的想法
public void verifyAllSupportingLogs() throws Exception {
Sheet sheet = getFilenameSupportingLogs("c:\\DataFile");
int rowCount = sheet.getPhysicalNumberOfRows();
XSSFCell firstColumnCell = null;
int firstColumnRowCount = 0;
ArrayList<String> InnerArray = new ArrayList<>();
String cellValues = null;
String GetsupportingLogs1 = null;
for (int i = 0; i < rowCount; i++) {
try {
XSSFRow row = (XSSFRow) sheet.getRow(i);
firstColumnCell = row.getCell(0);
} catch (NullPointerException nullPointerException) {
System.out.println("Cell is null at index: " + i);
}
if (firstColumnCell != null) {
if (firstColumnCell.getStringCellValue().length() > 0) {
firstColumnRowCount = i;
}
}
}
for (int j = 0; j <= firstColumnRowCount; j++) {
XSSFRow row = (XSSFRow) sheet.getRow(j);
XSSFCell cell = row.getCell(0);
String valuesFromExcel = cell.getStringCellValue();
InnerArray.add(valuesFromExcel);
cellValues = InnerArray.toString();
}
String GetsupportingLogs = con.clickOnSupportingLogsTextbox(GetsupportingLogs1);
System.out.println("GetsupportingLogs" + GetsupportingLogs);
con.checkValueSupportingLogs(cellValues, GetsupportingLogs);
}
public void checkValueSupportingLogs(String GetsupportingLogs, String cellValue) throws Exception {
java.lang.String[] cellValue1 = cellValue.split("\n");
for (String cellValue2 : cellValue1) {
if (GetsupportingLogs.contains(cellValue2)) {
System.out.println("Success, this string is in the supporting logs.");
} else {
reportFailure("ERROR! This string is not in the supporting logs.");
}
}
}
【问题讨论】:
-
@pburgr 对此有什么想法吗?
-
迭代行...检查行是否为空...在此迭代中迭代单元格...检查单元格是否为空...使用 if/then... 在 C# 中就像“ if (row == null) continue; if (row.GetCell(j) != null) {...} POI 在工作表上也有获取第一行的功能。