Writing Excel files with PHPExcel

Microsoft Excel is a very powerful application and now we’ve finally got a powerful API written in PHP5 to read/write/convert Excel files also directly from Linux (not using COM, etc). It’s far from perfect at the moment but it is working in 90% of cases. Software I’m talking about can be found here and it’s called PHP Excel.

In this article I’ll talk about how to write/create Excel files with PHP Excel. If you need to read Excel files, check How to read excel files with PHP article.

We’ll create a simple Excel2007 file with one sheet that will look like this:

A short introduction on what are we about to do (more in code comments).  We’ll create an Excel2007 file with one sheet, using Calibri font of default size 8. We’ll set currency to Euro with two decimals and red font, if value is below 0. Number format is with thousands separator, two decimal points and red font colour if value is below 0.  Our sheet title will be My sales report. Once everything is set up, we’ll write the “report” values. We’ll finish with a bit of styling (font size, bold, borders, …).

// include PHPExcel
// create new PHPExcel object
$objPHPExcel = new PHPExcel;
// set default font
// set default font size
// create the writer
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");

 * Define currency and number format.
// currency format, € with < 0 being in red color
$currencyFormat = '#,#0.## \€;[Red]-#,#0.## \€';
// number format, with thousands separator and two decimal points.
$numberFormat = '#,#0.##;[Red]-#,#0.##';

// writer already created the first sheet for us, let's get it
$objSheet = $objPHPExcel->getActiveSheet();
// rename the sheet
$objSheet->setTitle('My sales report');

// let's bold and size the header font and write the header
// as you can see, we can specify a range of cells, like here: cells from A1 to A4

// write header
$objSheet->getCell('D1')->setValue('Total Price');

// we could get this data from database, but for simplicty, let's just write it




// bold and resize the font of the last row

// set number and currency format to columns

// create some borders
// first, create the whole grid around the table
// create medium border around the table
// create a double border above total line
// create a medium border on the header line

// autosize the columns

// write the file

Here’s another trick: by changing two lines of code, you can create a PDF file. Change the line 10 with:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "PDF");

and line 84 with:


and you have a PDF file written. Isn’t that fun?! 🙂

Tags: , , , , ,

Content not available.
Please allow cookies by clicking Accept on the banner

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.