Insert a Row in Excel Using Java Apache POI - java

I am developing a desktop application related to Excel sheets. I have some problems inserting rows between two rows. Is there any possibility to do this in Java using Apache POI?
Workbook wb3=WorkbookFactory.create(new FileInputStream("Book1.xls"));
Sheet sh=wb3.getSheet("sheet1");
//Reading the available rows using (sh.getRow(1))
//Here i need to insert second row (????)
//I have third row here which already exists (sh.getRow(3))

I have a solution which is working very well:
Workbook wb3=WorkbookFactory.create(new FileInputStream("Book1.xls"));
Sheet sh=wb3.getSheet("sheet1");
int rows=sh.getLastRowNum();
Shift the number of rows down the sheet.
sh.shiftRows(2,rows,1);
Here
2 -- Position at which we need to insert row
rows -- Total rows
1 -- How many rows we are going to insert
The reason why we are doing the above process is to make an empty row; only then can we create a new row.
Now we shifted the rows, then we can do our stuff
Coding :
sh.createRow(1);
The above code is used to insert a row at the 1st position, as we defined.

Related

Shifting rows only in specific columns

I'm trying to shift rows down in my excel sheet and insert new data to the top of the list.
I'm able to shift rows down as:
int row = getItemsListSize(); <- which returns size of the list which contains elements
sheet.shiftRows(1, sheet.getLastRowNum(), rows);
Then I insert new data starting from row 1 and that way newest data is always on top of the list and older data gets moved down.
My problem is that I have second table also on that sheet and amount of items inside it isn't same as in first table. Currently also contents of this table gets moved down although no new items aren't added. Because of that it looks weird.
Is there way to only shift rows inside specific columns? For example: Move rows only in columns A-D.
No; from an excel perspective, there is no such thing as two separate tables. It's just one big sheet with rows and columns. Apache POI is a library that gives you access to excel's data structures. It's not a generalized 'mess with sheets' library. You'd have to write the code for such such a thing yourself.
More generally you're misusing excel a bit. The right move usually is to treat data, as, well, data. You should not be having 2 unrelated tables on the same sheet. Use the + button at the bottom left (or the appropriate POI option, which also supports sheets) to add another sheet.
If you want a sheet for visual reasons, make a third sheet, and that is a sheet that your POI code should never touch - it will render the data from the other (data) sheets. You can now add graphs, logos, whatever you want, and you won't run into issues where moving things around a bit 'because it looks nicer visually' then results in your POI-based code being broken.

Copy cell formula automatically while adding new row in excel table using apache POI

I have a simple table in excel which has a column serial no with formula =ROW()-ROW(Member_Data[[#Headers],[S. No.]]). Whenever I add new row to the table serial no is calculated automatically.when i added new row using Apache POI it didn't copied formula to the next row also style should be copied.
I have set AreaReference source = new AreaReference("A1:C8", SpreadsheetVersion.EXCEL2007);, which specifies my table range till 8th row.But its not working in any row between 1 to 8.
Currently I am copying 1st row formulas to new row using setcellformula as workaround which is working. I need alternate solution which is not dependent on 1st row. My task is only for read operation.

How to delete range of rows in Excel using Apache POI?

From Java, using Apache POI I just want to delete range of rows in my Excel sheet. For example: I want to delete row 45 to row 150. How to do that?

Applying Excel sheet drop down list validations using Java (Apache poi API)?

I've done coding for Excel sheet drop down list for a specific column but I want to apply validation.
For example - once the any value of drop down was selected in first row it'll not display in the second row.
How can I achieve this?

getLastRowNum not returning correct number of rows

I am having a small conundrum in using the getLastRowNum(). I am trying to get the number of rows with data from an Excel sheet. Supposing I have 3 rows, it should return in a print out statement stating 3 rows. My issue now is no matter how many rows I populate, it is returning me a certain fixed number. The following is excerpt of the code:
FileInputStream fis = new FileInputStream("C:\\Test Data\\Login.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheet("Login");
System.out.println("No. of rows : " + sheet.getLastRowNum());
My Excel Sheet consist of the following
From the attached image, I should be getting row count of 4, but I am getting 6 instead.
Any advice is deeply appreciated. Thank you in advance.
Speaking about attached file, it should return row number of 3 because of the XSSFSheet.getLastRowNum() is zero-based. But if your receive a number of 6, it seems you have 3 additional blank rows in your sheet.
If you just open existing file and it contains blank rows, here is example how to clean it. But if you fill the table by yourself, it seems you need to check you code for places where this empty line could be created.
it should return 3,
Check blank rows in your excel file and remove them
click Find & Select
click Go To Special
Select Blanks and click OK
Delete Sheet Rows
I was facing the same issue, just delete all rows below the last row of your sheet(i.e. 4) and save. This is the quick fix.

Categories

Resources