Just a quick update, I have not yet had the opportunity to play with any POI
code and will be unlikely to do so today as I am attending a concert
tonight, straight after work. However, following a bit of experimentation,
thanks to the heavy rain preventing us from digging the pond here, I found
that Excel will only parse the html mark-up for you if it is cut and pasted
from another source. By this, I mean that if I type into a cell the
following;
<html><em>Emphasise</em></html>
and then click on the Enter button (the green tick) in the formula bar or
press the Enter key on my keyboard, all Excel will do is enter the text into
the cell just as I have typed it. The application will not parse the mark-up
and ensure that the cell contains the word 'Emphasise' with the necessary
formatting applied. Further, I found that if I copied the contents of that
cell and tried to paste it into another cell on the same worksheet, all I
got was an exact copy of the first cell's contents. Again, Excel did not
parse the html for me. As I have indicated above, the only way to
'encourage' Excel to parse the mark-up was to copy the html from another
document - I used Notepad and WordPad to test the theory - and paste it into
an Excel cell; suggesting the formatting is a feature of the pasting process
in my opinion.
In light of this, I think it is safe to say that the only way Excel is
likely to parse html mark-up for you is if it is pasted into a cell. Reading
the mark-up from a file - and files are all POI can create/edit/read - is
unlikely to result in the effect you are seeking. To my mind, and in the
absence of further testing, the only way to accomplish what you are after
using the POI library, is to parse the html mark-up yourself, detect what
formatting should be applied to the cells contents and then create a
CellStyle object to accomplish this task.
Parsing the mark-up should be fairly straightforward as there are lots of
tools to assist you in this endeavour - the
javax.swing.text.html.parser.Parser class which is a part of the core Java
API. The one big hurdle to my mind would be the number of CellStyle objects
this might create. Excel places a limit on the number of styles a workbook
can contain and it is common practice when using POI to create a cell style
and use/re-use that object to style all identically formatted cells.
However, there is an easy way to get around this problem to my mind; using a
map object to contain the cell styles you create and generating a key from
the element tags that, together, define the styles attributes. Therefore, to
return to the example above, the parsing method would return a cell style
whose key was 'em'. You could use this key to check the map for a matching
value and use that cell style if a match is found. If no match is found, you
could add the newly created style to the map with it's accompanying key of
course, and then style the cell.
When I have the chance, I will look to see if there is a way around this
using POI but do not hold out any real hope of finding one. If I do manage
to discover anything else, I will post again.
Yours
Mark B
Me Simple wrote:
>
> Hi,
>
> I am using Apachi POI to create a excel file on runtime. The excel file
> may have variable number columns (number of columns depends on run time).
> The value for One of the column has HTML parsed html string as shown
> below:
>
> Listing 1:
>
> <html>My String</html>
>
> However, when I open the excel file, the HTML column appears as is and
> doesn't appear parsed like this one.
>
> Listing 2:
>
> My String
>
> Is there a way to display the html as shown in listing 2 in run time.
>
>
> thanks.
>
> The code used for creating a the cell is shown below:
>
>
> HSSFCellStyle headerCellStyle;
> HSSFWorkbook workBook = null;
>
> workBook = new HSSFWorkbook();
>
> HSSFCell cell = row.createCell(columnIndex);
> HSSFRichTextString textString = new HSSFRichTextString(value);
> headerCellStyle = workBook.createCellStyle();
> headerCellStyle.setFont(getHeaderFont());
> headerCellStyle.setWrapText(false);
>
> cell.setCellStyle(headerCellStyle);
> cell.setCellValue("<html>My String</html>");
>
--
View this message in context:
http://old.nabble.com/SHow-HTML-text-in-one-of-the-excel-cell-tp29144294p29149229.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]