Is it possible to set raw RichText ?

Topics: User Forum
May 14, 2010 at 9:39 PM

First off: Really great Excel API for PHP !!!

In general, everything is documented well inside the docs and also the API-Reference.

I also found enough samples about the RichText stuff, but I'm looking for a way to set cells with raw rich text a'la:

Hello World!
{\i What} a nice {\b day}.

is it possible ?

Why I'm looking for such thing ?

Well, I've (x)html formatted text blocks, which I wanna put into Excel cells. Cause I'm lazy I'm trying to go around parsing my (x)html for further RichText ->getFont()->setBold() conversion. Instead of I thought to use some kind of html2RTF converter/parser and put that "raw" RTF via some kind of ->setValue($rawRTF) into the Sheet. Is it possible ?

Thanks in advance for your help !


May 14, 2010 at 10:53 PM

There is no way of doing this at the moment, although it should be possible to provide a static method that would return a PHPExcel_RichText object when passed a block of rtf or html text.

$RTF = '{\rtf1
 Hello World!
 {\i What} a nice {\b day}.
$objRichText = PHPExcel_RichText::createRichTextFromRTF($RTF);



$HTML = 'Hello World!
<br />
<i>What</i> a nice <b>day<b>.';
$objRichText = PHPExcel_RichText::createRichTextFromHTML($HTML);

May 14, 2010 at 10:54 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
May 14, 2010 at 11:57 PM

Hi Mark,

lot thanks for the fast answer !! which sounds great ! - BUT I just recognized that I also need to display "list like" structures (i.e. <ul><li>... and also cascaded <ul><li>...</li><ul><li>..</li></ul></ul>).

If such structures couldn't be used within within the mentioned static methods (doesn't matter if ::...FromRTF or ::...FromHTML), then I have to build my manual parser anyway !

May 15, 2010 at 11:01 AM

It's something that would need to be worked out. We'd need to write the convertor methods anyway, and while getting the simple bold, italic, underline and breaks might be a simple initial step, it's something that could be expanded to cover all the key elemenst of rtf/html (such as lists) in a fairly straightforward manner, as long as the rtf/html markup wasn't malformed.

I've been playing with parsers already (looking at what we can do to support VBA macros within PHPExcel, including running them in a sandbox), so a more structured conversion from rtf or html to a PHPExcel_RichText object. Not a task I could do before the next release, but certainly within a couple of months.

May 16, 2010 at 6:41 PM

Sounds great ! ... even if I don't understand why you need a RTF to PHPExcel_RichText object/conversion... which probably is founded in my wrong assumption that MS is storing text cells internally in RTF.

Sure, that was a greenly assumption ;-)


However, I'll start some quick-and-dirty conversions on my side... in the hope that they remain stable for some months ;-)...

So, please don't forget to drop a note in the news or history, when you've made anything into the direction PHPExcel_RichText::...


Lot thanks for your informations and effort !!

May 16, 2010 at 8:31 PM
JoergE wrote:

Sounds great ! ... even if I don't understand why you need a RTF to PHPExcel_RichText object/conversion... which probably is founded in my wrong assumption that MS is storing text cells internally in RTF.

The Rich Text content is stored as a series of text "runs", each associated with a series of font style settings:

  • Font Name default is Calibri
  • Font Size default is 11
  • Bold default is false
  • Italic default is false
  • SuperScript default is false
  • SubScript default is false
  • Underline default is PHPExcel_Style_Font::UNDERLINE_NONE
  • Strikethrough default is false
  • Color default is PHPExcel_Style_Color::COLOR_BLACK

So your example would comprise 5 runs:

  1. "Hello World!\n" with all defaults
  2. "What" with defaults except for italic=true
  3. " a nice " with all defaults
  4. "day" with defaults except for bold=true
  5. "." with all defaults

What we need to do is provide a mechanism to convert RTF/HTML to that series of runs, as implemented by PHPExcel, so that it can be written correctly.


May 19, 2010 at 10:02 AM

Hi Mark,

thanks a lot for your detailed response.

I'll take a detailed look in the next 2-3 weeks into it.

May be I should try to build such a static method instead of doing my initial mentioned "quick and dirty" in-between conversion.

When talking about "PHPExcel_RichText::createRichTextFromHTML($HTML)".

How do you suggest or prefer to parse the (x)html ? Via a simple xml parser like expat, or by a more complex dom API, or as simple as possible with some kind of stream parsing ?

When thinking about "lists", which are as far as I could see, don't have text-style-setting equivalents within Excel/xlsx. How would you suggest/prefer to build/convert those ?

As simple text-parts with leading "list- bullet characters" and leading space or tab indents ?