Solved

PHP -> Excel

Posted on 2004-09-21
7
1,203 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:alkalined
7 Comments
 
LVL 15

Assisted Solution

by:nicholassolutions
nicholassolutions earned 20 total points
ID: 12124167
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.

Cheers,
Matt
0
 
LVL 1

Author Comment

by:alkalined
ID: 12130619
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?
0
 
LVL 1

Assisted Solution

by:NchMch
NchMch earned 20 total points
ID: 12136824
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:
<?php

$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');
echo($csv_data);

?>

This will generate file called "data.csv", which you may open in Excel like a regular spreadsheet.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Accepted Solution

by:
peyox earned 40 total points
ID: 12139807
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.

Cheers.
0
 
LVL 2

Assisted Solution

by:harveykane
harveykane earned 20 total points
ID: 12173974
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 www.tmssoftware.com (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).
0
 
LVL 1

Author Comment

by:alkalined
ID: 12177296
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.
0
 
LVL 1

Author Comment

by:alkalined
ID: 12235065
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
mb_internal_encoding("UTF-8");
iconv_set_encoding('output_encoding',"UTF-8");
iconv_set_encoding('internal_encoding',"UTF-8");            

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

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now