如何使用POI删除工作表之间的空白行?
我想要使用POI从Excel工作表中删除空行。
我们有像shiftRow()
或removeRow()
但是不能在这种情况下使用。 请帮我完成。
请尝试下面的一段代码。 它适用于我以及多个连续的空行存在时。
for(int i = 0; i < sheet.getLastRowNum(); i++){ if(sheet.getRow(i)==null){ isRowEmpty=true; sheet.shiftRows(i + 1, sheet.getLastRowNum(), -1); i--; continue; } for(int j =0; j<sheet.getRow(i).getLastCellNum();j++){ if(sheet.getRow(i).getCell(j).toString().trim().equals("")){ isRowEmpty=true; }else { isRowEmpty=false; break; } } if(isRowEmpty==true){ sheet.shiftRows(i + 1, sheet.getLastRowNum(), -1); i--; } }
虽然没有testing,试试这个:
HSSFSheet sheet = workBook.getSheetAt(0); HSSFRow row = sheet.getRow(0); sheet.removeRow(row);
这是一种在删除非空白行的行时中断单元格的读取的方法
while(cellsIterator.hasNext()) { Cell currentCell = cellsIterator.next(); if(currentCell.getCellType() == Cell.CELL_TYPE_BLANK) { this.currentArraySheet.put("Headers",this.currentArraySheetHeaders); this.currentArraySheet.put("Rows",this.currentArraySheetRows); return; } if (currentCell.getCellTypeEnum() == CellType.STRING) { System.out.print(currentCell.getStringCellValue() + " | "); } else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) { System.out.print(currentCell.getNumericCellValue() + "| "); } else if (currentCell.getCellTypeEnum() == CellType.BOOLEAN) { System.out.println(currentCell.getBooleanCellValue() + " | "); } }