Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format text in Excel file via PHP

I'm loading data from my database, and exporting to an Excel file via a method I found on this site: http://www.appservnetwork.com/modules.php?name=News&file=article&sid=8

It works, but what I want to do now is format the text before it exports - change the font and text size. Does anybody have any ideas on how to do this?

like image 464
Pamela Avatar asked Nov 11 '08 03:11

Pamela


2 Answers

I have written Excel spreadsheets from PHP 5 using the PEAR :: Package :: Spreadsheet_Excel_Writer classes. You can do a lot with this package.

like image 161
Bill Karwin Avatar answered Oct 13 '22 01:10

Bill Karwin


Depending on the speed in which you wish to deploy your solution, one method is to just use the HTML table tag, store all your data in tables using style markup, and then use PHP header's option to force the browser to save is as a .xls file.

For proof of concept, copy this code into notepad, save as .xls, and then open with Excel:

<table>
<tr><th>Column 1</th><th>Column 2</th></tr>
<tr><td style="font-size:200%">Answer 1</td><td style="color:#f00">Answer 2</td></tr>
<tr><td colspan="2" style="font-weight:bold">Answer 3 with 2 columns</td></tr>
</table>

Its not the most elegant solution, but it will absolutely suit your needs.

like image 44
Eric Caron Avatar answered Oct 12 '22 23:10

Eric Caron