Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1235
  • Last Modified:

PHP -> Excel

I need a good class to build an XLS spreadsheet from PHP.
I now use the PEAR's class which does a great job, but its problem is that it still does not support unicode and I need to use chinese symbols in it.
I saw that this class is a port of a CPAN class that does support unicode.
The best solution would be if someone had already ported the unicode support into this PEAR class, but even if you could suggest another class with comparable functionality it would be fine.
4 Solutions
you could use a pipe to a Perl script that uses the CPAN module to create the XLS. It may not be ideal, but I think it would be the least work for you and allow you to do what you want fairly quickly. You're not the only person requesting unicode (and other charset) support, so they will probably provide it in an upcoming version.

alkalinedAuthor Commented:
I hope they do release another version.. There has been more than 3 months since the last one.

What do you mean by using a pipe? Should I write a Perl script that connects to the database, retrieves the data and writes the XLS?
Because that would be really inconsistent and inconvenient :(

..or is there a way to just use the perl class from within the php code?
The alternative solution would be to use CSV file format - it's pretty simple to generate and the Unicode support will depend only on application used for opening the generated file. Here's the example:

$foo = array
  'one'   => 'two',
  'three' => 'four',
  'five'  => 'six'

$csv_data = '';

foreach ($foo as $key => $val)
  $csv_data .= "$key;$val\r\n";

header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename=data.csv');


This will generate file called "data.csv", which you may open in Excel like a regular spreadsheet.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Hi Alkalined,

You can also try to save regular HTML table as .xls file. You can add formatting to it, but formulas would be difficult to implement (but possible). Such a "fake" excel files may be easily opened by excel with no difference for users. Another approach is to use XML to create excels, this approach is more powerful than HTML tables, however this still not a real excel file.

The advantage of methods described above, is that you can use unicode to populate your excel sheets.

I have just been down this road of generating Excel from PHP.

I tried a few different methods (including PEAR) and ended up writing a Delphi dll (you can access via CGI or COM+ on windows) using the Flexcel component from (I think they have VB and C++ versions too).

This was a difficult solution, but it does have a couple of nice points. The main one being, you create your excel templates in excel itself (as opposed to coding each cell like in PEAR) and within the template define variables that will be replaced with actual data.

I have no idea if this solution supports various character sets, but I found it the most flexible method (and also most difficult to implement).
alkalinedAuthor Commented:
Come on guys, so far nobody has offered an acceptable solution without so many cons..
- nicholassolutions: I need to retrieve data from database from a perl script,leading to inconsistency of code - unacceptable
- NchMch: yes, there is no unicode problem with this but i cannot add formatting and I need a beautiful report - unacceptable
- harveykane: this truly sounds like the most complicated solution that can exist. And it doesn't answer the real problem: entering chinese - hardly acceptable
- peyox: maybe the best solution so far. Although it's actually not an XLS :( . Also when I tried it all borders were gone but I guess it's just a matter of tricking it.. - probably acceptable

I actually found a solution myself, in pure php, and it did what I needed...
Even more, a friend of mine suggested me another solution, php again, so I can even use templates to fill in.
So I'll give you guys a couple more days to see if someone can fully deserve the points. Else I think I'll give the points to peyox or maybe split them somehow.
alkalinedAuthor Commented:
Obviously no more input will appear..

What I did was to modify the beginning of function writeStringBIFF8 in Worksheet.php to:
     $str = iconv("UTF-8","UTF-16LE",$str);          
     $strlen = strlen($str) / 2;
     $encoding  = 0x1;

..and ported function writeUnicode from the CPAN class
Then added the following code to my script:

//not sure which one of the 3 lines below does the job

$xls =& new Spreadsheet_Excel_Writer();
$xls->_codepage = '65001';

It worked for me, although I'm not entirely sure how.. :)
However, until the next version appear, it seems like a satisfactory patch.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now