gpt4 book ai didi

java - TreeMap 缺少对象行中的键

转载 作者:行者123 更新时间:2023-12-02 01:00:26 24 4
gpt4 key购买 nike

我正在使用 Apache POI 库将 Excel 工作表中的值读取到 Java 程序中。

我遍历表格的每一行来获取我需要的值。

在对象 Row 中,有一个 TreeMap,其中包含 XSSFCell 对象作为值。

通常我会得到以下 TreeMap:

enter image description here

其中包含键 4。该值通常是图中选择的空字符串。

出于某种原因,对于某些对象,我得到以下 TreeMap:

enter image description here

缺少键 4 的地方。

两个行对象都属于同一个表。

这就是我使用对象行的方式:

 XSSFSheet mySheet = myWorkBook.getSheet("nameOfSheet");
Iterator<Row> rowIterator = mySheet.iterator();

while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// here I call my method
}

最佳答案

您可以通过调用传入索引和 MissingCellPolicy 的“getCell()”来防止这导致应用程序中的不一致。 ,可能是 Row.RETURN_BLANK_AS_NULL。

Apache POI guide解释:

In some cases, when iterating, you need full control over how missing or blank rows and cells are treated, and you need to ensure you visit every cell and not just those defined in the file. (The CellIterator will only return the cells defined in the file, which is largely those with values or stylings, but it depends on Excel).

In cases such as these, you should fetch the first and last column information for a row, then call getCell(int, MissingCellPolicy) to fetch the cell. Use a MissingCellPolicy to control how blank or null cells are handled.

// Decide which rows to process
int rowStart = Math.min(15, sheet.getFirstRowNum());
int rowEnd = Math.max(1400, sheet.getLastRowNum());

for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
Row r = sheet.getRow(rowNum);
if (r == null) {
// This whole row is empty
// Handle it as needed
continue;
}

int lastColumn = Math.max(r.getLastCellNum(), MY_MINIMUM_COLUMN_COUNT);

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
}
}
}

关于java - TreeMap 缺少对象行中的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57771823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com