Latest News & Blog


Lightweight PHP Class to export to Excel XLS Format

Tuesday 21 April, 2009


PHP export to XLS class

Here is a lightweight, simple and fast PHP Class which exports array data to Excel's .XLS format.

The array data could come from MySQL or any other data source, but as long as its in the normal PHP array format, then this script will produce a xls out of it.

I wrote this because we didnt want any of fuss or bulk many other exporting scripts seemed to have. It is being released for free under the GNU LGPL Licence.

Project Page


The class usage is documented within the zip. It also follows;


A quick script to demo the use of the export-xls.class.php script.
//include the export-xls.class.php file

$filename = 'test.xls'; // The file name you want any resulting file to be called.

//create an instance of the class
$xls = new ExportXLS($filename);

//lets set some headers for top of the spreadsheet

$header = "Test Spreadsheet"; // single first col text

//add blank line
$header = null;

//add 2nd header as an array of 3 columns
$header[] = "Name";
$header[] = "Age";
$header[] = "Height";


Lets add some sample data

Of course this can be from a SQL query or anyother data source

//first line
$row[] = "Jack";
$row[] = "24";
$row[] = "6ft 5";

//second line
$row = array();
$row[] = "Jim";
$row[] = "22";
$row[] = "5ft 5";

//add a multi dimension array
$row = array();
$row[] = array(0 =>'Jess', 1=>'54', 2=>'4ft');
$row[] = array(0 =>'Luke', 1=>'6', 2=>'2ft');

 You can return the xls as a variable to use with;
 $sheet = $xls->returnSheet();


 You can send the sheet directly to the browser as a file



Written By...

Lee Simpson

Software Engineer, Linux Wizard, proud father of 3 boys