2009-12-02 237 views
23

我正在使用Apache POi HSSF庫將信息導入到我的應用程序中。問題是這些文件有一些額外的/空的行需要在解析之前先被移除。使用Apache POI從Excel工作表中刪除行HSSF

沒有HSSFSheet.removeRow(int rowNum)方法。只有removeRow(HSSFRow row)。這個問題,空行不能被刪除。例如:

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

在空行上給出NullPointerException,因爲getRow()返回null。 另外,正如我在論壇上看到的,removeRow()僅擦除單元格內容,但該行仍然存在爲空行。

有沒有刪除行(空或不),而不創建一個全新的工作表沒有我想要刪除的行的方式嗎?

回答

0

我試圖從一兩年前重新回到我的大腦深處以獲得與POI相關的體驗,但我的第一個問題是:爲什麼在分析之前需要刪除行?你爲什麼不趕上sheet.getRow(rowNum)電話的空結果並繼續前進?

+0

因爲我已經爲格式化爲簡單表格的Excel文件(在第一行和下面的數據的列名稱)做了解析器,如果有空行,我需要警告用戶。另外,我現在要導入的文件來自另一個應用程序,並且只有很多額外的行(空或不)才能使文件看起來更漂亮! – fmaste 2009-12-02 19:23:17

+0

好的 - 但我只是說,你可能會很好地服務於子類化現有的解析器,並讓你的子類忽略空行而不是對它們做某些事情;這樣,來自其他應用程序的文件仍然可以很好看,而且您不必死在這些文件上。 :) – delfuego 2009-12-02 19:27:05

+0

是的。我也想到了。但該文件具有以下內容:僅用於第一列(id)的行,以及下一行中的其餘列。只是因爲它看起來更好。我需要把它做成一行並刪除一行。 我認爲我會創建一個包含工作表和行索引列表的類,並在刪除行時刪除索引。它很多,但我需要刪除行,我不明白爲什麼你不能用這個庫刪除行! – fmaste 2009-12-02 19:36:10

3

HSSFRow有一個名爲setRowNum(int rowIndex)的方法。

當您必須「刪除」一行時,您將該索引放入List。然後,當您到達下一行非空時,您從該列表中獲取索引並將其設置爲setRowNum(),並從該列表中刪除該索引。 (或者你可以使用一個隊列)

2

東西沿着

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

行應該做的伎倆。

24
/** 
* 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); 
     } 
    } 
} 
+1

此解決方案在多行刪除的情況下不起作用。 – gospodin 2012-11-30 18:08:39

+0

@gospodin - 在循環中運行內部代碼,並且不要忘記每次刪除一行時將rowIndex計數器減1。 – Avik 2014-06-20 03:15:37

+1

POI目前有一個錯誤會拋出> org.apache.xmlbeans.impl.values.XmlValueDisconnectedException <當在包含常見單元格的範圍上多次使用此函數時 - https://bz.apache.org/bugzilla/ show_bug.cgi?id = 59733 – beermann 2016-12-02 14:41:45

6

我知道,這是一個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); 
     } 
    } 
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); 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
0

這個答案是在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; 
} 
相關問題