Generating Excel Spreadsheets with Māori Macrons in PHP
January 9th, 2008 by Ewen Cumming
After recently working on an application to generate Microsoft Excel documents using a PHP script, I ran into trouble with the PEAR package “Spreadsheet Excel Writer” which didn’t correctly display UTF-8 characters (Māori macrons in this case). Here is a fix!
You just need to replace the Workbook.php file in the Spreadsheet Excel Writer package with this patched version of the file. For more information on the problem and the patch check out the comments on the bug report page (most of the good stuff is right near the bottom).
Then your spreadsheet generation code would look something like:
// Send HTTP headers to tell the browser what’s coming
$workbook->send(“file.xls”);
$workbook->setVersion(8);
$worksheet =& $workbook->addWorksheet(“My worksheet”);
$worksheet->setInputEncoding(“UTF-8″);
// .. add content to spreadsheet here
$workbook->close();
This worked for me with version 0.9.0 of the package and it should also work with 0.9.1. I’d expect the fix to be included in upcoming releases of the package, but until then the above patch works great.
Links
- http://pear.php.net/package/Spreadsheet_Excel_Writer - Spreadsheet Excel Writer - PEAR package homepage
- http://pear.php.net/bugs/bug.php?id=1572 - Description of Bug
- http://bugs.gentoo.org/attachment.cgi?id=136412 - Patched Workbook.php
Tags: Developer Resources, macrons, Māori, Open Source, PHP

March 31st, 2008 at 11:57 am
Do we have to call them “Maori macrons”? They’re just macrons!
Please rate this comment:April 9th, 2008 at 2:11 am
[...] Spreadsheet Excel Writer is a handy tool to generate MS Excel files with php. But it has some trouble handling utf-8 sources. Luckily Research e-Labs describes a simple solution: After recently working on an application to generate Microsoft Excel documents using a PHP script, I… [...]
Please rate this comment:April 25th, 2008 at 12:47 am
thanks, that saved me a lot of time
Please rate this comment:June 18th, 2008 at 10:55 pm
Thank you for this !
It is too bad the project is not being updated anymore its a very nice and simple way of doing xls output
Please rate this comment:June 20th, 2008 at 9:49 pm
This might fix the display problem but now the xls crashes excel with some strange resource error.
I though this is supposed to fix this too according to what it says in bug comments but it doesnt.
Any suggestions ?
Please rate this comment:June 26th, 2008 at 2:02 am
Hi Caleb,
That sounds like the behaviour produced if the functions in Workbook.php aren’t patched - it still works but breaks if the spreadsheets are large (over somewhere like 500 rows).
Can you confirm that the copy of Workbook.php you’re using is definitely the patched one?
Thanks,
Please rate this comment:Ewen