30

ApachePOiHSSFライブラリを使用して情報をアプリケーションにインポートしています。問題は、ファイルにいくつかの余分な/空の行があり、解析する前に最初に削除する必要があることです。

方法はありませんHSSFSheet.removeRow( int rowNum )。のみremoveRow( HSSFRow row )。これに伴う問題は、空の行を削除できないことです。例えば:

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

getRow()nullを返すため、空の行にNullPointerExceptionを与えます。また、フォーラムで読んだように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

Something along the lines of

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

should do the trick.

于 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

1 年か 2 年前の POI 関連の経験について、脳の奥深くまで手を差し伸べようとしていますが、最初の質問は、なぜ行を解析する前に削除する必要があるのか​​ということです。sheet.getRow(rowNum)呼び出しから null の結果をキャッチして先に進んでみませんか?

于 2009-12-02T18:56:30.103 に答える