user510058
user510058

Reputation: 83

Apache POI: How to insert column in Excel file

I'm using POI to manipulate data in Excel files for a university project. I'm having trouble inserting a new column in an existing Excel. I tried to use

Cell c = createCell(int column);
c.setCellValue("someValue");

but it seems that if the column already exists, it replaces the existing data. What I need is to shift all the other columns one column to the right when I insert the new one.

I searched over the internet but I couldn't find a solution to that. Is there a way to do it, without iterating all the cells in the row and moving them one-by-one ?

Upvotes: 7

Views: 16706

Answers (2)

Jim Garrison
Jim Garrison

Reputation: 86774

According to this post from 2008, which I got from Google "poi insert column", POI does not presently have an API to do this. You will need to iterate over the rows and adjust all references yourself.

Upvotes: 1

Brad Gardner
Brad Gardner

Reputation: 1627

To my knowledge, POI doesn't support this directly. You could create a copy of the worksheet and copy the data over, leaving room for your new column. You also need to take into consideration the need to re-write any formulas in the columns beyond your insertion point and any formulas that use those cells.

Upvotes: 6

Related Questions