Jorge,

        I pieced together a way to do this from other suggestions I found on the 
mailing list and elsewhere. This is more of a trick to get Excel to think 
it has an Excel file, but it doesn't truly generate an Excel file.
        Thanks to Excel versions 97 and up (don't recall 95) having nice little 
HTML import abilities we can trick it into loading a plain old HTML table 
and treating it as an Excel file.  Here is how you do it.

The following headers will let you throw the file for download so the 
client can save it to their computer.  Just name it with .xls and then 
when they try to open it Excel will automatically convert it from HTML.  
You can change this to "inline" if you want IE to display it in the 
browser.

header("Content-Type: application/vnd.ms-excel; name='excel'");
header("Content-Disposition: attachment; filename=filename.xls");

Now, as far as the data goes, it's simple, just make your run of the mill 
HTML table.  The usual for loop for database records will do, and you 
will just end up with something like this

<table>
<tr>
<td>Column with Text</td>
<td>Column with Numbers</td>
<td>Column with Numbers/Text with leading zeros</td>
</tr>
<tr>
<td>I am text</td>
<td>1,500.00</td>
<td>="0012"</td>
</tr>
</table>

When Excel loads that it will make a 3 column sheet and fill in the data. 
 Any cells you want to keep with leading zerios you use ="0012" in the 
HTML table as the data.  Otherwise the column will be treated as numeric 
when it is loaded.  Text stays as text.

There are some tricks as well where you can store forumlas in the actual 
HTML table cells so when the sheet is loaded it would calculate the sum, 
avg, whatever of the columns or sheet.  I don't recall how to do that off 
the top of my head but I think you just plugged in regular formulae into 
the HTML table cells. Try it if you need it.

>From there the user might have to set some data types for some columns as 
I don't think they can be set. You might want to check some of the help 
on MS Excel 2K or XP as I'm very sure the HTML capabilities are much more 
advanced than with 97 which we still use.

I hope that helps everyone out a little.  There are limitations with this 
method, but to just get a file that someone can safely open in Excel, it 
is a very convenient way to do it rather than having to muck around with 
binary files.

Have a good weekend,

Tim Frank


>>>>>>>>>>>>>>>>>> Original Message <<<<<<<<<<<<<<<<<<

On 7/13/01, 12:58:18 PM, [EMAIL PROTECTED] (Jorge Alvarez) wrote 
regarding Export to Excel:


> One of my clients is requesting me to export some data from PHP-generated
> pages to MS Excel files. I have no clue on how to do such export with 
PHP.
> I'm using PHP/Apache/Linux Mandrake.

> What options do I have?

> Your help is very appreciated.

> Regards,

> Jorge Alvarez
> --
> Let your screen saver contribute to cancer research.
> http://members.ud.com/vypc/cancer/
> A new way to help. Sponsored by Intel.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to