You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Steven Tomcavage <st...@law.upenn.edu> on 2009/11/24 18:39:39 UTC

Shifting cells to the right

I'm upgrading some code that is currently using POI 2.5 to use POI 3.5. I want the code to work with both .xls and .xlsx files, so I'm using WorkbookFactory.create(inputStream) to read my Excel file. The program has some code to shift all the cells in a row to the right, and I'm having trouble converting this code to the new systems of doing things. Here's how it is working with version 2.5:

    private void shiftCellsRight(Row r, short firstCellNum, short lastCellNum, short distanceToShift) {
        for(short i = lastCellNum; i >= firstCellNum; i--) {
            Cell c = r.getCell(i);
            if(c == null)
                continue;
            c.setCellNum((short)(i + distanceToShift));
        }
    }

The problem is that the setCellNum(short) method doesn't exist in XSSFCell. It's also depreciated in HSSFCell, but the replacement method in HSSFRow, moveCell(Cell, short), also doesn't exist in XSSFRow. How can I shift cells to the right in POI 3.5?

Thanks,

-Steven Tomcavage

--
Steven Tomcavage
Web Developer
Information Technology Services
University of Pennsylvania Law School
215-746-2303
http://www.law.upenn.edu/itsfeedback 



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Shifting cells to the right

Posted by MSB <ma...@tiscali.co.uk>.
At this stage, I am going to offer what is a complete guess.

My hunch is that you have two options. The first would be to create a patch
for XSSFRow so that it also offers the moveCell() method. Secondly, I have a
hunch that it may be possible to simply test the actual type of the object
you Cell variable c contains at run time. If it holds an HSSFCell then
simply call the HSSFRow.moveCell() method. If however it contains an
XSSFCell then it may well be that by gaining access to the xml bean CTCell,
you could change the cells location. It is certainly worth trying as all you
will need to do is add the type check and call the getCTCell() method. After
this, you can see what methods are defined/available on the object.

I have to admit that I have never done this nor do I know what import
statement would allow you to bring in the relevant type to your program nor
even which archive to add to the classpath. As I said, it was a hunch,
sorry.

Yours

Mark B


Steven Tomcavage-2 wrote:
> 
> I'm upgrading some code that is currently using POI 2.5 to use POI 3.5. I
> want the code to work with both .xls and .xlsx files, so I'm using
> WorkbookFactory.create(inputStream) to read my Excel file. The program has
> some code to shift all the cells in a row to the right, and I'm having
> trouble converting this code to the new systems of doing things. Here's
> how it is working with version 2.5:
> 
>     private void shiftCellsRight(Row r, short firstCellNum, short
> lastCellNum, short distanceToShift) {
>         for(short i = lastCellNum; i >= firstCellNum; i--) {
>             Cell c = r.getCell(i);
>             if(c == null)
>                 continue;
>             c.setCellNum((short)(i + distanceToShift));
>         }
>     }
> 
> The problem is that the setCellNum(short) method doesn't exist in
> XSSFCell. It's also depreciated in HSSFCell, but the replacement method in
> HSSFRow, moveCell(Cell, short), also doesn't exist in XSSFRow. How can I
> shift cells to the right in POI 3.5?
> 
> Thanks,
> 
> -Steven Tomcavage
> 
> --
> Steven Tomcavage
> Web Developer
> Information Technology Services
> University of Pennsylvania Law School
> 215-746-2303
> http://www.law.upenn.edu/itsfeedback 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/Shifting-cells-to-the-right-tp26500321p26508410.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org