Spreadsheet_Excel_Writer is an PHP api that can help to write an excel file. From the tutorial, it’s so easy to use the class to write an excel.

To use this class, you should install php-pear module. Ini ubuntu it’s so easy just run sudo apt-get install php-pear.

After pear installed, you can install spreadsheet_excel_writer automatically by call sudo pear install Spreadsheet_Excel_Writer. But unfortunately my network is using proxy so that I couldn’t do this. Alternatifely I install it manually.

Default the pear package should be placed under /usr/share/pear, but you can change by edit /etc/php5/apache2/php.ini. Search the line that contain include_path and add the path where you save your pear package.

Download Spreadsheet_Excel_Writer package at http://pear.php.net/package/Spreadsheet_Excel_Writer/download. Extract the downloade file under /usr/share/pear. You can rename it to Spreadsheet to make you easier to code. This package has dependencies :

  • PEAR Package: OLE 0.5 or newer
  • PHP Version: PHP 4.1.0 or newer

OLE is a pear package, you can install using the same way. Download, extract to /usr/share/pear and you can write an excel file using your php code now 🙂

Here is the example code to make an excel file using this package:

// include package
include ('Spreadsheet/Excel/Writer.php');

// create empty file
$excel = new Spreadsheet_Excel_Writer('Book1.xls');

// add worksheet
$sheet =& $excel->addWorksheet('Untitled');

// add note
$sheet->write(0, 0, 647384.65);
$sheet->writeNote(0, 0, 'This assumes g=16%');

// add URLs as cell values
$sheet->writeUrl(1, 0, 'http://www.yahoo.com/', 'Yahoo!');
$sheet->writeUrl(2, 0, 'http://www.google.com/', 'Google');
$sheet->writeUrl(3, 0, 'http://www.php.net/', 'PHP.net');

// save file to disk
if ($excel->close() === true) {
echo 'Spreadsheet successfully saved!';
} else {
echo 'ERROR: Could not save spreadsheet.';

Have a nice code!