You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Andreas <sp...@donvanone.de> on 2012/12/10 12:07:42 UTC

Cast HSSFRichTextString to XSSFRichTextString

Hello,

i have to read an excel file wehere the format can be hssf or xssf,
so i chose SS to do the work.
Finaly i want so save the edited document as an xslx-excel-file.
Some cells have to be copied from the input file but i can't find 
a way to xopy a RichTextString from HSSF to XSSF.

This is how i do it:

int type = cell.getCellType();
Cell targetCell = targetRow.createCell(index);
targetCell.setCellStyle(targetStyle);
CreationHelper createHelper = targetWorkbook.getCreationHelper();

switch (type) {
    [..]
    case Cell.CELL_TYPE_STRING :       
              targetCell.setCellValue(cell.getRichStringCellValue());
          break;
        [..]
}


This only works if the input is an xssf-file, otherwise i get:

java.lang.ClassCastException:
org.apache.poi.hssf.usermodel.HSSFRichTextString
cannot be cast to org.apache.poi.xssf.usermodel.XSSFRichTextString
at org.apache.poi.xssf.usermodel.XSSFCell.setCellValue
(XSSFCell.java:343)

and the only solution i see, is to copy only the string text
without any formatting.

Is there any solution?


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


Re: Cast HSSFRichTextString to XSSFRichTextString

Posted by Mark Beardsley <ma...@tiscali.co.uk>.
Yes, I thought that might be the case.

The better option IMO *is* to use POI as this will allow you to write an
application that can be run on any platform, will be robust and run quickly
as you are manipulating files rather than applications. Even though it is
likely to be a fairly involved process to copy all of the attributes of a
cell between workbooks, you will only have to write it once. Converting the
xls files into xlsx ones using OLE/COM is an additional step you would have
to undertake for every file you process, will restrict you to running on a
Windows platform as COM is a Windows technology of course and it will run
fairly slowly as you would be controlling an instance of Excel.

One possible approach I did not mention is to use OpenOffice and write what
is - or at least was - termed a UNO Client Application. If OpenOffice does
now support writing the xlsx file format, then you can work entirely with
it. UNO is quite complex as it relies on an approach where you ask managers
for objects that provide services. This complexity was deemed necessary to
support a varety of different programming languages. Using OpenOffice means
your application would be platform independent but it does demand that you
install OpenOffice wherever the application is to be run. Somewhere, I have
a simple UNO application that performs file conversions; if I can find it, I
will post the code so that can see the type of thing I am talking about.
Also, I will look out the JACOB and SWT code that I wrote and do likewise
with that, again once I can find it.



--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Cast-HSSFRichTextString-to-XSSFRichTextString-tp5711676p5711682.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


Re: Cast HSSFRichTextString to XSSFRichTextString

Posted by Andreas <sp...@donvanone.de>.
Thank you for the answer, even if it's not the one I hoped for...

Will have a deeper look at your ideas.


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


Re: Cast HSSFRichTextString to XSSFRichTextString

Posted by Mark Beardsley <ma...@tiscali.co.uk>.
POI does not support conversion between the two different file formats. You
would have to write code to retrieve all of the features about the cell and
it's contents from the binary format file and code to recreate them within
the OOXML format file. There could also be a few bumps along the way so to
speak as you would have to check and ensure fonts were available within the
target workbook.

It might be worthwhile looking at a simple file format convertor -
JODConvertor is the one that springs to mind but I cannot be certain it
would allow you to save the file into the OOXML file format. Might still be
worth taking a look here http://www.artofsolving.com/opensource/jodconverter

One trick that I do know will work is to use OLE\COM if you are working on a
Windows platform. With an additional library such as JACOB or the OLE32
sub-package of the Standard Widget Toolkit (SWT and part of Eclipse
usually), you can write Java code to control Excel directly and this will
certainly allow you to open an xls file and re-save it as an xlsx. Once you
have the files all converted into the target format, processing should be
trivial.





--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Cast-HSSFRichTextString-to-XSSFRichTextString-tp5711676p5711679.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