0

I am using POI to manipulate an existing excel sheet.
I want to remove rows in the begining and add rows at the end. When I remove rows I move all rows after them up.

int numOfRows = sheet.getLastRowNum()+1;
int extraRows = numOfRows - maxAllowedRows;             
for(int i=1;i<=extraRows;i++){  //i intialized with 1 to ignore header row.
    sheet.removeRow(sheet.getRow(i));
}
sheet.shiftRows(extraRows+1,sheet.getLastRowNum(),-extraRows);

In current run of the program it works fine. But when the program runs again, as many rows as were removed and shifted in first run equal number of blank rows appear in the sheet at the bottom and any new row that I try to add at the bottom of the sheet gets added after these blank rows.

Is there a way to avoid appearance of blank rows after the remove and shift operation (i.e. Is there any way to actaully delete the rows?)

Abhishek
  • 37
  • 1
  • 1
  • 7

2 Answers2

0

Similar Question Here

I thought the shiftRows method adjusted the (region? - sorry, forget the spreadsheet terminology).

Community
  • 1
  • 1
Mike
  • 3,186
  • 3
  • 26
  • 32
  • I have seen the link provided by you before posting this question. My prob is a bit diffrent. _I need to read an excel then add rows to it and there is a limit on max num of rows in the excel. So before adding new rows I need to check that num of rows doesn't exceeds the limit. And if that's the case I need to remove rows at the top in the sheet and then add rows at bottom. Now when I do this it works fine the first time. But in the second run, as many blank rows as filled rows I removed and shifted in fist run appears in the sheet. And any newly added row gets added after these blank rows_ – Abhishek Jul 18 '12 at 11:25
0

I'm no expert on POI and it's giving me some headaches but my understanding is that you can add your new rows over the blank rows. You'll need to keep track of where the blank rows start but then add the rows using HSSFSheet.addRow(index)

s1ni5t3r
  • 711
  • 2
  • 7
  • 17