How to create new rows in apache poi 3.6?

2019-06-04 21:36发布

问题:

I am using Apache POI 3.6 and java in our application.

I have data in row numbers 9 to 30.

Now, I want to include new rows after the row number 25. After doing like this, the old data in 26 to 30 was destroyed.... I want to add that new rows without destroy the old row's data...

We can manually, create new rows by just right click the mouse on the row Header like row number 25 and select insert then it will include the 26 row without deleting anything about the old values.

How I do it programatically using apache poi and java?

回答1:

First you need to do move down all the rows from 25 onwards by doing a shift

sheet1.shiftRows(25, sheet1.getLastRowNum(), 5);

this will move down all rows from 25 by 5 places

then insert the new rows in that position

row1 = sheet1.getRow(25); 
                HSSFCell cell1 = row1.createCell(0);
                cell1.setCellValue("text: The new line goes here");