POI:getPhysicalNumberOfCells 方法每行的单元格数量错误

zep*_*man 3 java excel apache-poi

我正在使用 POI java 库来读取 Excel。

我的 Exel 有一个由 8 列组成的简单结构。

问题是通过方法 getPhysicalNumberOfCells 读取列长度我得到每行不同的数字。

zep*_*man 7

问题是 getPhysicalNumberOfCells 与我的想法有不同的含义。

getPhysicalNumberOfCells 返回一行中具有内容的单元格的数量。

事实上,POI 只为每一行存储添加到 exel 中的数据,例如,如果第 0、3、5 列中有数据,getPhysicalNumberOfCells 将始终返回 3,因为 3 是“填充”单元格的数量。

为了达到获取连续单元格的逻辑数量的目的,我们使用:

getLastCellNum() 
Run Code Online (Sandbox Code Playgroud)

根据whit文档,此方法获取最后一个单元格的索引。该值增加 1,因此在上面的示例中,最大索引 5 将是 6。我认为这样做是为了简化行单元格的迭代。

另外,还有一个方法

getLastCellNum
Run Code Online (Sandbox Code Playgroud)

显示第一个单元格索引。

受官方文档启发的示例:

  short minColIdx = row.getFirstCellNum();
  short maxColIdx = row.getLastCellNum();
  for(short colIdx=minColIdx ; colIx<maxColIdx ; colIdx++) {
     Cell cell = row.getCell(colIx);
    //get value of cell
Run Code Online (Sandbox Code Playgroud)

}

  • http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ (2认同)