views:

930

answers:

4

I'm using the Apache POi HSSF library to import info into my application. The problem is that the files have some extra/empty rows that need to be removed first before parsing.

There's not a HSSFSheet.removeRow( int rowNum ) method. Only removeRow( HSSFRow row ). The problem with this it that empty rows can't be removed. For example:

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

gives a NullPointerException on empty rows because getRow() returns null. Also, as I read on forums, removeRow() only erases the cell contents but the row is still there as an empty row.

Is there a way of removing rows (empty or not) without creating a whole new sheet without the rows that I want to remove?

A: 

I'm trying to reach back into the depths of my brain for my POI-related experience from a year or two ago, but my first question would be: why do the rows need to be removed before parsing? Why don't you just catch the null result from the sheet.getRow(rowNum) call and move on?

delfuego
Because I already made a parser for Excel files that are formatted as a simple table (column names at the first row and data below) and I need to warn the user if there are empty rows. Also, the files that I want to import now are from another application and have a lot of extra rows (empty or not) only to make the file look nicer!
fmaste
OK -- I'd just say, though, that you'd probably be well-served just to subclass your existing parser and let your subclass ignore empty rows rather than do something with them; that way, the files from the other app can still look nice and you don't have to die on those files. :)
delfuego
Yes. I also think of that. But the file has things like: a row only for the first column (the id), and the rest of the columns in the row below. Just because it looks nicer. I need to make it a single row and delete one.I think that I would make a class containing a sheet and a list of row indexes and delete the indexes when I delete a row. Its to much, but I need to delete row, I don't understand why you cannot delete row with this library!
fmaste
A: 

The HSSFRow has a method called setRowNum(int rowIndex).

When you have to "delete" a row, you put that index in a List. Then, when you get to the next row non-empty, you take an index from that list and set it calling setRowNum(), and remove the index from that list. (Or you can use a queue)

True Soft
A: 

Something along the lines of

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

should do the trick.

ammoQ
A: 
 /**
 * 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);
        }
    }
}
AndreAY