[PHP][PHPExcel] Create Report Excel from PHP


Description
     Create report excel from PHP , only basic write :)

How To
    1. Download PHPExcel class Download Here
    2. after download you will see 3 folder (classes , example , documentation).
    3. if you want to learn you can find at example's folder , but the crucial part is Classes Folder , without that we cannot create excel.
    4. ok now we test the basic write from php to excel. here's the folder structure in your webserver :
        - Classes
        - Index.php //we create this file

        Here's the listing code for index.php :

<?php
/** Include PHPExcel */
require_once dirname(__FILE__) . '\Classes\PHPExcel.php';

// create object
$objPHPExcel = new PHPExcel();

// create metadata
$objPHPExcel->getProperties()->setCreator("name creator")
->setLastModifiedBy("name")
->setTitle("Report")
->setSubject("Report")
 ->setDescription("Report BOD")
->setKeywords("office PHPExcel php")
->setCategory("Test result file");

// insert data cell
$objPHPExcel->setActiveSheetIndex(0)
                      ->setCellValue('A1', 'this is A1')
                      ->setCellValue('B2', 'this is B2')
                      ->setCellValue('C1', 'this is C1')
                      ->setCellValue('D2', 'this is D2');

// create object for write
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

// save path and transform to excel , choose one if you think is easy
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
   or
$objWriter->save('C:\wamp\www\iniExcel.xlsx');
?>


*Note : don't forget __FILE__ is a magic constant from PHP (GET full path and filename of the file. )

Post a Comment

Harap gunakan bahasa yang baik dan sopan, terima kasih