Java 如何使用POI删除工作表之间的空行?

Java 如何使用POI删除工作表之间的空行?,java,excel,apache-poi,Java,Excel,Apache Poi,我想使用POI从Excel工作表中删除空行 我们有类似于shiftRow()或removeRow()的方法,但它不是供使用的 在这种情况下。请帮我完成。尽管未经测试,但请尝试以下操作: HSSFSheet sheet = workBook.getSheetAt(0); HSSFRow row = sheet.getRow(0); sheet.removeRow(row); 请尝试下面的代码。当存在多个连续的空行时,它也适用于我 for(int i = 0; i < sheet.g

我想使用POI从Excel工作表中删除空行

我们有类似于
shiftRow()
removeRow()
的方法,但它不是供使用的
在这种情况下。请帮我完成。

尽管未经测试,但请尝试以下操作:

HSSFSheet sheet = workBook.getSheetAt(0);
HSSFRow row = sheet.getRow(0);
sheet.removeRow(row);

请尝试下面的代码。当存在多个连续的空行时,它也适用于我

    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--;
        }
    }
for(int i=0;i对于(int j=0;j这是一种在删除作为非空白行的行时中断单元格读取的方法

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() + " | ");
                }
}

对@Sankumarsingh代码进行更改以使其正常工作

@Sankumarsing代码不适用于我,因为它没有删除空的第一行。此问题的修复方法是:

private void removeEmptyRows(XSSFSheet sheet) {
    Boolean isRowEmpty = Boolean.FALSE;
    for(int i = 0; i <= sheet.getLastRowNum(); i++){
      if(sheet.getRow(i)==null){
        isRowEmpty=true;
        sheet.shiftRows(i + 1, sheet.getLastRowNum()+1, -1);
        i--;
        continue;
      }
      for(int j =0; j<sheet.getRow(i).getLastCellNum();j++){
        if(sheet.getRow(i).getCell(j) == null || 
        sheet.getRow(i).getCell(j).toString().trim().equals("")){
          isRowEmpty=true;
        }else {
          isRowEmpty=false;
          break;
        }
      }
      if(isRowEmpty==true){
        sheet.shiftRows(i + 1, sheet.getLastRowNum()+1, -1);
        i--;
      }
    }
  }
private void removemptyrows(XSSFSheet){
Boolean isRowEmpty=Boolean.FALSE;

对于(int i=0;我可能是一个有用的帖子),这只会删除行的内容。我想删除两个数据填充行之间的空白行。