Exporting data from php to excel

PHP-Zend picture PHP-Zend · Jul 23, 2012 · Viewed 88.5k times · Source

I need to export data from php (data retrieved from mysql database) to excel. I'm using Zend Framework. I need to do some changes to my data before exporting to excel. Actually, what I really need is to generate a monthly cash book.

I have read a lot of documents, but ended up in a mess. I really can't understand how I should begin. Do I really need PEAR? Do I need to download any class library?Isn't there a simple way to do this?

Thanks in advance

Answer

Samuel Hapak picture Samuel Hapak · Jul 23, 2012

I would suggest you to use great PHPExcel library. It is really powerful, supports variety of formats, can do visual formatting and is easy to use.

You can find more about it at their webpage: http://phpexcel.codeplex.com/. (PHPExcel has already moved at https://github.com/PHPOffice/PHPExcel)

2019:

PHPExcel has now been superceded by PhpSpreadsheet GitHub .

Example of usage:

    $objPHPExcel = new PHPExcel();
    /** You can set many properties */
    $objPHPExcel->getProperties()->setCreator("My company")
                 ->setLastModifiedBy("John Doe")
                 ->setTitle("Annual report")
                 ->setSubject("Sales")
                 ->setDescription("Annual sales report by John Doe")
                 ->setCategory("Finance");

    /** Choose one of sheets */
    $activeSheet = $objPHPExcel->setActiveSheetIndex(0);
    /** Simply set value of cell */
    $activeSheet->setCellValue("A1", 'plural');

You can do a lot more of course, reading excel files, setting visual styles, creating plots, expressions and lot more.