aspose cells.getRows().getCount() 和 cells.getCount() 对于 excel 工作表不断增加
aspose cells.getRows().getCount() and cells.getCount() keeps increasing for a excel worksheet
我的excel有一个三行两列的工作表(第一行是header行,第二、第三行是数据行),我用aspose com.aspose.cells.Cells class 并获取行数和单元格数如下:
Cells cells = uploadExcel.getWorksheets().get(0).getCells();
logger.info("cells.getRows().getCount()="+cells.getRows().getCount());
logger.info("cells.getCount()="+cells.getCount());
for (int i = 1; i <= cells.getRows().getCount(); i++) {
logger.info("cells.getRows().getCount()="+cells.getRows().getCount());
logger.info("cells.getCount()="+cells.getCount());
logger.info("cells.get(i, 1).getStringValue().trim()="+cells.get(i, 1).getStringValue().trim());
}
但我发现控制台输出,cells.getRows().getCount() 和 cells.getCount() 在前三个循环后继续增加,如下所示:
00:26:30,601 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getRows().getCount()=3
00:26:32,606 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getCount()=6
00:26:37,300 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getRows().getCount()=3
00:26:39,536 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getCount()=6
00:26:43,731 INFO [com.frw.object.repository.EXM103] (default task-2) cells.get(i, 1).getStringValue().trim()=15/01/2020
00:26:55,031 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getRows().getCount()=3
00:26:56,617 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getCount()=6
00:27:03,795 INFO [com.frw.object.repository.EXM103] (default task-2) cells.get(i, 1).getStringValue().trim()=16/02/2020
00:27:07,173 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getRows().getCount()=3
00:27:08,629 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getCount()=6
00:27:13,627 INFO [com.frw.object.repository.EXM103] (default task-2) cells.get(i, 1).getStringValue().trim()=
00:27:21,966 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getRows().getCount()=4
00:27:23,124 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getCount()=7
00:28:19,353 INFO [com.frw.object.repository.EXM103] (default task-2) cells.get(i, 1).getStringValue().trim()=
00:28:26,331 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getRows().getCount()=5
00:28:28,354 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getCount()=8
为什么 cells.getRows().getCount(), cells.getCount() 在前三个迭代后继续增加?
这是这些 API 的预期行为。还有一些其他 API 具有相同的行为,例如 Cells[int row, int column]、RowCollection[int row] 等。实际上,对于这些API,Aspose.Cells必须在cells模型中实例化相应的对象,以便您可以方便地使用对象操作数据。
但是如果只需要访问已有的对象,可以使用Row.getCellOrNull()/Cells.checkCell()/checkRow()等方法。如果返回的对象为空,则对应的对象还没有在单元格模型中实例化。然而,迭代现有对象的最佳方式是使用 Iterator,例如 Row.iterator()/Cells.iterator()。请参阅以下示例代码段供您参考:
例如
示例代码:
1)
//Load sample workbook
Workbook wb = new Workbook(dirPath + "sample.xlsx");
//Access first worksheet
Worksheet ws = wb.getWorksheets().get(0);
//Access cells iterator
Iterator itrat = ws.getCells().iterator();
//Print cells name in iterator
while(itrat.hasNext())
{
Cell cell = (Cell)itrat.next();
System.out.println(cell.getName() + ": " + cell.getStringValue().trim());
}
2)
Workbook book = new Workbook("sample.xlsx");
Worksheet sheet = book.getWorksheets().get(0);
Range range = sheet.getCells().getMaxDisplayRange();//You may also create your desired range (in the worksheet) using, e.g sheet.getCells().createRange("A1", "J11");
Iterator rangeIterator = range.iterator();
while(rangeIterator.hasNext())
{
Cell cell = (Cell)rangeIterator.next();
//your code goes here.
}
希望这对您有所帮助。
PS。我在 Aspose 担任支持开发人员/传播者。
我的excel有一个三行两列的工作表(第一行是header行,第二、第三行是数据行),我用aspose com.aspose.cells.Cells class 并获取行数和单元格数如下:
Cells cells = uploadExcel.getWorksheets().get(0).getCells();
logger.info("cells.getRows().getCount()="+cells.getRows().getCount());
logger.info("cells.getCount()="+cells.getCount());
for (int i = 1; i <= cells.getRows().getCount(); i++) {
logger.info("cells.getRows().getCount()="+cells.getRows().getCount());
logger.info("cells.getCount()="+cells.getCount());
logger.info("cells.get(i, 1).getStringValue().trim()="+cells.get(i, 1).getStringValue().trim());
}
但我发现控制台输出,cells.getRows().getCount() 和 cells.getCount() 在前三个循环后继续增加,如下所示:
00:26:30,601 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getRows().getCount()=3
00:26:32,606 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getCount()=6
00:26:37,300 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getRows().getCount()=3
00:26:39,536 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getCount()=6
00:26:43,731 INFO [com.frw.object.repository.EXM103] (default task-2) cells.get(i, 1).getStringValue().trim()=15/01/2020
00:26:55,031 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getRows().getCount()=3
00:26:56,617 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getCount()=6
00:27:03,795 INFO [com.frw.object.repository.EXM103] (default task-2) cells.get(i, 1).getStringValue().trim()=16/02/2020
00:27:07,173 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getRows().getCount()=3
00:27:08,629 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getCount()=6
00:27:13,627 INFO [com.frw.object.repository.EXM103] (default task-2) cells.get(i, 1).getStringValue().trim()=
00:27:21,966 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getRows().getCount()=4
00:27:23,124 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getCount()=7
00:28:19,353 INFO [com.frw.object.repository.EXM103] (default task-2) cells.get(i, 1).getStringValue().trim()=
00:28:26,331 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getRows().getCount()=5
00:28:28,354 INFO [com.frw.object.repository.EXM103] (default task-2) cells.getCount()=8
为什么 cells.getRows().getCount(), cells.getCount() 在前三个迭代后继续增加?
这是这些 API 的预期行为。还有一些其他 API 具有相同的行为,例如 Cells[int row, int column]、RowCollection[int row] 等。实际上,对于这些API,Aspose.Cells必须在cells模型中实例化相应的对象,以便您可以方便地使用对象操作数据。
但是如果只需要访问已有的对象,可以使用Row.getCellOrNull()/Cells.checkCell()/checkRow()等方法。如果返回的对象为空,则对应的对象还没有在单元格模型中实例化。然而,迭代现有对象的最佳方式是使用 Iterator,例如 Row.iterator()/Cells.iterator()。请参阅以下示例代码段供您参考:
例如
示例代码:
1)
//Load sample workbook
Workbook wb = new Workbook(dirPath + "sample.xlsx");
//Access first worksheet
Worksheet ws = wb.getWorksheets().get(0);
//Access cells iterator
Iterator itrat = ws.getCells().iterator();
//Print cells name in iterator
while(itrat.hasNext())
{
Cell cell = (Cell)itrat.next();
System.out.println(cell.getName() + ": " + cell.getStringValue().trim());
}
2)
Workbook book = new Workbook("sample.xlsx");
Worksheet sheet = book.getWorksheets().get(0);
Range range = sheet.getCells().getMaxDisplayRange();//You may also create your desired range (in the worksheet) using, e.g sheet.getCells().createRange("A1", "J11");
Iterator rangeIterator = range.iterator();
while(rangeIterator.hasNext())
{
Cell cell = (Cell)rangeIterator.next();
//your code goes here.
}
希望这对您有所帮助。
PS。我在 Aspose 担任支持开发人员/传播者。