Call us Toll-Free:
1-800-218-1525
Live ChatEmail us

 Sponsors

How to generate Excel reports using PHP

Michel Nadeau, 08-13-2008
Today, for many reasons, I had to convert CSV reports to Excel reports. The report generator is written in PHP. After some Google searches, I found Spreadsheet_Excel_Writer, a PEAR extension that allows to do pretty much anything there is to do with an Excel file; so I decided to use this existing module.

Step 1 - Install PEAR

I will not cover in details how to install PEAR on your system. You can read the instructions for installing it under Windows and Linux here.

Step 2 - Install PEAR extensions

Installing PEAR extensions is really easy. Whatever you're on a Windows or Linux machine, it's the same commands:

$ pear install OLE-beta
$ pear install Spreadsheet_Excel_Writer-beta

Step 3 - Getting started with a simple Excel example

Now that you have the needed PEAR extensions installed, you are ready to create a simple Excel example.

NOTE: make sure to add the PEAR path in your php.ini 'include_path'.

Include the extension:

require_once "Spreadsheet/Excel/Writer.php";

Create a workbook:

$workbook
= new Spreadsheet_Excel_Writer();
$workbook->send('test.xls');

Create a worksheet:

$worksheet
=& $workbook->addWorksheet("My Worksheet");

Write some data:

$data
= array(array("test","test"),array("test","test"));
$line = 0;
foreach (
$data as $data_line ) {
 
$col = 0;
  foreach (
$data_line as $data_col ) {
   
$worksheet->write($line,$col,$data_col);
   
$col++;
  }
 
$line++;
}

Close the file:

$workbook
->close();

This very simple example will generate a 'test.xls' file containing 2 lines and 2 columns (with 'test' in each cell) that you will be offered to download (not stored on the hard disk).

If you wish to save the Excel file on the Web server's hard disk instead of downloading it, you can do so by specifying a file name when creating the workbook and by omitting the "send()" function.

Create a workbook:

$workbook
= new Spreadsheet_Excel_Writer('test.xls');
//$workbook->send('test.xls');

Step 4 - Add some formatting

Spreadsheet_Excel_Writer allows you to add almost any formatting you want on a cell. How it works? Simple. You create your formats and when you call the "write()" you specify the format you want.

Creating a format:

// Create the Title format
$fmt_title =& $workbook->addFormat();
$fmt_title->setBold();
$fmt_title->setSize(20);
$fmt_title->setMerge();

// Write using the Title format
$worksheet->write(0,0,"My Title",$fmt_title);

This example will write "My Title" in bold, size 20 at line 0, column 0. You noticed the "setMerge()" function? When you use it in a format, Spreadsheet_Excel_Writer will merge all the cells for which you specify this format.

Merging cells:

// Create the Title format
$fmt_title =& $workbook->addFormat();
$fmt_title->setBold();
$fmt_title->setSize(20);
$fmt_title->setMerge();

// Write using the Title format
$worksheet->write(0,0,"My Title",$fmt_title);
$worksheet->write(0,1,"",$fmt_title);
$worksheet->write(0,2,"",$fmt_title);
$worksheet->write(0,3,"",$fmt_title);
$worksheet->write(0,4,"",$fmt_title);

In this example, line 0, columns 0-4 will be merged together.

Step 5 - Add images

Spreadsheet_Excel_Writer also allows to insert 24 bits Bitmap images in Excel documents.

Insert image:

$worksheet
->insertBitmap(0,0,"image.bmp");

Resources

You can do A LOT more than what is explained in this short tutorial. I suggest to read the entire Spreadsheet_Excel_Writer documentation here.

Jais, 12-18-2008
How to insert jpeg, gif and png images in the excel sheet

Aaras Sameer, 08-25-2009
good one!!!!!!!!
:)

Jean Kahigiso, 02-26-2010
Helpful! Thanks

Carlo, 04-15-2010
Hi,
I've some problems with the images!

This is my code:

<?php
require_once 'Spreadsheet/Excel/Writer.php';

$workbook = new Spreadsheet_Excel_Writer();
$workbook->send('test.xls');
$worksheet =& $workbook->addWorksheet('Prova 1');

$worksheet->insertBitmap(0,0,"mask.bmp");

$workbook->close();

?>

But The file is empty!

Can you help me?

Thanks

Dhinakaran, 11-12-2010
suberb article

darius, 01-28-2011
Thanks.
I have one question. This tool is very useful. Suppose I am getting data from a database. How can I run through a query result and display it to the user for download in Excel format?

Please show me how i can do this or link me to some relevant resources for this.
Greetings

manish, 01-31-2011
How to insert jpeg, gif and png images in the excel sheet

sundaravigneshvaran, 06-07-2011
Nice, pearl is working fine. but when hosting my site. i need to install this frame work in remote server. i cant do this things. Any other alternate solution is available. pls give me any suggestions. Thanks in advance
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

 Comments
Post your comments












Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.



About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions