简体   繁体   English

Apache poi HSSF 返回不正确的物理行数

[英]Apache poi HSSF returns incorrect no of physical rows

I am working on user given Excel file (.xls) and using Apache poi HSSF to read the worksheet.我正在处理用户给定的 Excel 文件 (.xls) 并使用 Apache poi HSSF 读取工作表。 The Excel contains 16 rows physically. Excel 实际包含 16 行。

  • 13th row is empty.第 13 行是空的。
  • 14th row has data第 14 行有数据
  • 15th row is empty第 15 行是空的
  • 16th row has data第16行有数据

In this case, worksheet.getPhysicalRows() returns 13.在这种情况下, worksheet.getPhysicalRows()返回 13。
While, worksheet.getLastRowNum() returns 16.worksheet.getLastRowNum()返回 16。

And note if I deleted 13th row, getPhysicalRows() returns 14.请注意,如果我删除了第 13 行, getPhysicalRows()将返回 14。
if I also deleted 16th row, getPhysicalRows() still returns 14.如果我还删除了第 16 行, getPhysicalRows()仍然返回 14。

The last row with valid data is not read at all.根本不读取具有有效数据的最后一行。

Have you encountered this behaviour with XLS?您在使用 XLS 时遇到过这种行为吗?

Edit: I am not sure if this code can help the question.编辑:我不确定这段代码是否可以帮助解决这个问题。

 excelDocumentStream = new FileInputStream(new File(xlFilePath));
 POIFSFileSystem fsPOI = new POIFSFileSystem(new BufferedInputStream(excelDocumentStream));
 workbook = new HSSFWorkbook(fsPOI);
 int numOfRows = workSheet.getPhysicalNumberOfRows();
 int lastRow = workSheet.getLastRowNum();

do it like this像这样做

int rows = 0;
if(sheet.getPhysicalNumberOfRows()>0)
   rows=sheet.getLastRowNum() - sheet.getFirstRowNum()+1;

Hope this helps!!!希望这可以帮助!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM