0
最初我已经使用了4行数据,然后我删除了最后两行。问题是,我仍然得到4作为行的总数。我应该怎么做才能更新计数。我怎样才能从Excel使用Apache poi更新行数
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = (Sheet) workbook.getSheetAt(0);
System.out.println("No of rows:"+firstSheet.getPhysicalNumberOfRows());
Iterator<Row> iterator = firstSheet.iterator();
try{
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
if(!cell.getStringCellValue().isEmpty()){
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
writer.append(cell.getStringCellValue());
break;
}
}
}
}
}finally{
inputStream.close();
workbook.close();
writer.flush();
writer.close();
}
转到Excel文档,选择删除的行并清除内容。如果您在单元格中输入了任何内容,则会将其标记为已使用并将包含在物理行数/单元格中,除非您将其清除。 –
@TarasShpulyar我已通过选择使用清除内容选项删除的行清除了内容。但我仍然得到相同的行数。 – user1553680