30

我正在使用 Apache POi HSSF 库将信息导入我的应用程序。问题是文件有一些额外/空行需要在解析之前先删除。

没有HSSFSheet.removeRow( int rowNum )办法。只有removeRow( HSSFRow row ). 问题在于无法删除空行。例如:

sheet.removeRow( sheet.getRow(rowNum) );

在空行上给出 NullPointerException,因为getRow()返回 null。此外,正如我在论坛上阅读的那样,removeRow()只删除了单元格内容,但该行仍然作为空行存在。

有没有一种方法可以在不创建没有我要删除的行的情况下创建一个全新的工作表的情况下删除行(是否为空)?

4

7 回答 7

34
 /**
 * Remove a row by its index
 * @param sheet a Excel sheet
 * @param rowIndex a 0 based index of removing row
 */
public static void removeRow(HSSFSheet sheet, int rowIndex) {
    int lastRowNum=sheet.getLastRowNum();
    if(rowIndex>=0&&rowIndex<lastRowNum){
        sheet.shiftRows(rowIndex+1,lastRowNum, -1);
    }
    if(rowIndex==lastRowNum){
        HSSFRow removingRow=sheet.getRow(rowIndex);
        if(removingRow!=null){
            sheet.removeRow(removingRow);
        }
    }
}
于 2010-08-24T07:00:37.797 回答
9

我知道,这是一个 3 年前的问题,但我最近不得不解决同样的问题,而且我必须用 C# 来解决。这是我与 NPOI 一起使用的功能,.Net 4.0

    public static void DeleteRow(this ISheet sheet, IRow row)
    {
        sheet.RemoveRow(row);   // this only deletes all the cell values

        int rowIndex = row.RowNum;

        int lastRowNum = sheet.LastRowNum;

        if (rowIndex >= 0 && rowIndex < lastRowNum)
        {
            sheet.ShiftRows(rowIndex + 1, lastRowNum, -1);
        }
    }
于 2012-07-04T13:11:44.047 回答
3

类似的东西

int newrownum=0;
for (int i=0; i<=sheet.getLastRowNum(); i++) {
  HSSFRow row=sheet.getRow(i);
  if (row) row.setRowNum(newrownum++);
}

应该做的伎俩。

于 2009-12-03T09:00:35.153 回答
2

HSSFRow一个方法叫做setRowNum(int rowIndex).

当您必须“删除”一行时,您将该索引放入List. 然后,当您到达下一行非空时,您从该列表中获取一个索引并将其设置为调用setRowNum(),然后从该列表中删除该索引。(或者你可以使用队列)

于 2009-12-03T08:54:17.877 回答
1

我的特殊情况(它对我有用):

    //Various times to delete all the rows without units
    for (int j=0;j<7;j++) {
      //Follow all the rows to delete lines without units (and look for the TOTAL row)
      for (int i=1;i<sheet.getLastRowNum();i++) {
        //Starting on the 2nd row, ignoring first one
        row = sheet.getRow(i);
        cell = row.getCell(garMACode);
        if (cell != null) 
        {
          //Ignore empty rows (they have a "." on first column)
          if (cell.getStringCellValue().compareTo(".") != 0) {  
            if (cell.getStringCellValue().compareTo("TOTAL") == 0) {
              cell = row.getCell(garMAUnits+1);
              cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
              cell.setCellFormula("SUM(BB1" + ":BB" + (i - 1) + ")");
            } else {
              cell = row.getCell(garMAUnits);
              if (cell != null) {
                int valor = (int)(cell.getNumericCellValue());
                if (valor == 0 ) {
                  //sheet.removeRow(row);
                  removeRow(sheet,i);
                }
              }
            }
          }
        }
      }
    }
于 2012-07-23T10:46:15.930 回答
1

这个答案是对 AndreAY 的答案的扩展,为您提供删除行的完整功能。

public boolean deleteRow(String sheetName, String excelPath, int rowNo) throws IOException {

    XSSFWorkbook workbook = null;
    XSSFSheet sheet = null;

    try {
        FileInputStream file = new FileInputStream(new File(excelPath));
        workbook = new XSSFWorkbook(file);
        sheet = workbook.getSheet(sheetName);
        if (sheet == null) {
            return false;
        }
        int lastRowNum = sheet.getLastRowNum();
        if (rowNo >= 0 && rowNo < lastRowNum) {
            sheet.shiftRows(rowNo + 1, lastRowNum, -1);
        }
        if (rowNo == lastRowNum) {
            XSSFRow removingRow=sheet.getRow(rowNo);
            if(removingRow != null) {
                sheet.removeRow(removingRow);
            }
        }
        file.close();
        FileOutputStream outFile = new FileOutputStream(new File(excelPath));
        workbook.write(outFile);
        outFile.close();


    } catch(Exception e) {
        throw e;
    } finally {
        if(workbook != null)
            workbook.close();
    }
    return false;
}
于 2015-08-16T04:23:27.927 回答
0

我正试图回到我的大脑深处,了解一两年前与 POI 相关的经验,但我的第一个问题是:为什么在解析之前需要删除行?为什么不直接从sheet.getRow(rowNum)调用中捕获 null 结果并继续前进?

于 2009-12-02T18:56:30.103 回答