Creating Excel documents in PHP


Posted in: php | Save to del.icio.us | Twit This! 12 Mar 2008

PEAR’s ‘Excel Writer’ is a nifty module to create Excel documents form PHP. Its a port of the perl
Spreadsheet::WriteExcel module. It supports cell formating, formulas, multiple worksheets, page size setting, notes and more.
Below is given a small example code to show how easy it is to create a Excel document. The created document can be saved to the server or sent to the browser directly.

require_once 'Spreadsheet/Excel/Writer.php';
 
// Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();
 
/* Sending HTTP headers, this will popup a file save/open
dialog in the browser when this file is run
*/
$workbook->send('excelTest.xls');
 
// Create 2 worksheets
$worksheet1 =& $workbook->addWorksheet('worksheet 1');
$worksheet2 =& $workbook->addWorksheet('worksheet 2');
 
// Set header formating for Sheet 1
$header =& $workbook->addFormat();
$header->setBold();		// Make it bold
$header->setColor('black');	// Make foreground color black
$header->setFgColor("green");	// Set background color to green
$header->setHAlign('center');	// Align text to center
 
// Write some data on Sheet 1
$worksheet1->write(0, 0, 'Name', $header);
$worksheet1->write(0, 1, 'Age', $header);
$worksheet1->write(1, 0, 'Sameer Borate');
$worksheet1->write(1, 1, 30);
 
// Set header formating for Sheet 2
$header =& $workbook->addFormat();
$header->setBold();		// Make it bold
$header->setFgColor("red");	// Set background color to red
$header->setHAlign('center');	// Align text to center
 
// Write some data on Sheet 2
$worksheet2->write(0, 0, 'Name', $header);
$worksheet2->write(0, 1, 'Age', $header);
$worksheet2->write(1, 0, 'Tom Peters');
$worksheet2->write(1, 1, 30);
 
// Send the file to the browser
$workbook->close();
?>



Share this post

Share on Facebook
Share on Twitter
Share on StumbleUpon
Share on Delicious
Share on Digg
Share on Technorati
Share on Reddit
Feeds RSS Subscribe to site Feed

Other related posts

  • No Related Post


9 Responses

1

Steven

March 11th, 2009 at 2:52 am

I understood how to fill an excel file witha data. But how can I add makro VBA code to the excel file, so that the user has a set of working makros delivered with the data. Thanks in advance, Steven.

sameer

March 11th, 2009 at 3:11 am

No you cannot put macros using PEAR::Spreadsheet_Excel_Writer.

3

Steven

March 11th, 2009 at 6:45 am

OK, it doesn’t work with PEAR’s ‘Excel Writer’ … are there any other ways to create excel files with VBA code inside from a web server?

sameer

March 11th, 2009 at 7:56 am

Not that I know of. You can try one of these although they are for .NET languages. The first one can also be used with PHP as said on their web page.

1. http://www.aspose.com/categories/file-format-components/aspose.cells-for-.net-and-java/default.aspx
2. http://www.carlosag.net/Tools/ExcelXmlWriter/
3. http://www.codeplex.com/ExcelPackage

5

Steven

March 12th, 2009 at 12:42 am

… I’ll have a try. Thanks!

6

frezz

June 25th, 2009 at 1:59 am

a nice post! it’s so helpful..
now,how if i want to export from php but i’ve data from database and use adodb library.
i’ve tried like this : $worksheet1->write_string(5,0,$res->fields[NAME]);
But it’s also not worked…
thanks

7

unknowMan

July 21st, 2009 at 2:26 pm

require_once ‘Spreadsheet/Excel/Writer.php’;
is this file in the PHP library?

sameer

July 21st, 2009 at 8:44 pm

Its in the PEAR ‘Excel Writer’ library

9

Andreas E.

January 19th, 2010 at 7:49 am

yup, but you MUST install OLE too, which is a dependency of SEW, otherwise nothing will work.

Comment Form

Use the html <code> tag to insert small source code snippets

For longer code examples use http://pastie.org/.

Get latest updates by E-mail

About this blog

This site is a digital habitat of Sameer, a freelance web developer working from Pune.More

Recent Comments

  • sameer: You can try this in your templates header.php : http://pastie.org/867569 [...]
  • avanthi: I played it back by using selenium RC [...]
  • avanthi: Ohh, ok no problem, here the actual issue is with IE, when i play back in firefox it is working fine [...]
  • Veerendra: Hi sameer great plugin to filter content. I was searching this kind of filtering plugin for doing [...]
  • sameer: My apologies! I'm not conversant with SharePoint. [...]
  • avanthi: Is it possible to automate share point people picker control through selenium. When i record throug [...]
  • sameer: Check to see if the 'IDE > options > format' is set to HTML. [...]
  • sameer: Google strips any newline characters form the text. Although it does accept it with the online trans [...]

  • Users Online

    • 7 Users Online
    • 6 Guests, 1 Bot