【问题标题】:How to delete blank rows in between the sheet using POI?如何使用 POI 删除工作表之间的空白行?
【发布时间】:2012-09-12 06:00:40
【问题描述】:

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

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

【问题讨论】:

标签: java excel apache-poi


【解决方案1】:

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

    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--;
        }
    }

【讨论】:

    【解决方案2】:

    更改@Sankumarsingh 代码以使其工作

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

    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--;
          }
        }
      }
    

    【讨论】:

      【解决方案3】:

      虽然没有测试,但试试这个:

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

      【讨论】:

      • 这只会删除行的内容。我想删除两个数据填充行之间的空白行。
      【解决方案4】:

      这是一种在单元格已删除作为非空行的行时中断单元格读取的方法

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-15
        • 2012-07-16
        • 1970-01-01
        • 2013-09-06
        • 1970-01-01
        • 2019-12-31
        • 2015-04-09
        • 1970-01-01
        相关资源
        最近更新 更多