CLICK HERE TO BUY IT TODAY! OR GET IT FREE VIA TRIALPAY  

PHP Demo Application - Source Code

/Framework/Model/Libraries/OutputFormats/Excel_Advanced.php

<?php
/**
* Script Contents: Apeel_Framework_Model_Libraries_Export_OutputFormats_Excel Class
* Extends Apeel_Framework_Model_Libraries_Export_OutputFormats_Abstract
* @package Apeel_Framework_Model_Libraries
*/

/**
* Generates a document in Microsoft Excel (R) format using the PHPExcel Plugin.  
*   
* @package      Apeel_Framework_Model_Libraries 
* @version      1.1.0
* @author       John W. King (email: contact@apeelframework.net)
* @copyright    City Business Logic Limited 2001-2011
* @license      Dual MIT / GNU Lesser General Public License Version 3
*/
class Apeel_Framework_Model_Libraries_Export_OutputFormats_Excel_Advanced extends Apeel_Framework_Model_Libraries_Export_OutputFormats_Abstract
{    

    
/**
    * Uses PHPExcel plugin to create a formatted Excel Workbook.  
    * 
    * @param array $data
    * @param string $filename
    * @return void
    */
    
public function generateDocument($data$filename) {   
        
        
// Extract Data and Formatting      
        
$title $data['title'];
        
$rowData $data['information']['data'];
        
$columnFormatting $data['information']['formatting'];
         
        
// Raise Memory Limit
        
@ini_set('memory_limit''256M');
        
        
// Raise Execution Timeout
        
@ini_set('max_execution_time'300); 
        
        
// Create new instance of PHPExcel class
        
$objPHPExcel = new PHPExcel();
        
        
// Create new workbook
        
$objPHPExcel->getProperties()->setCreator("APEEL FRAMEWORK")
                                     ->
setLastModifiedBy('')
                                     ->
setTitle($title)
                                     ->
setSubject('')
                                     ->
setDescription('')
                                     ->
setKeywords('')
                                     ->
setCategory('');
        
        
// Set title of Worksheet
        
$objPHPExcel->setActiveSheetIndex(0);
        
$objPHPExcel->getActiveSheet()->setTitle($title);
      
        
// Output data
        
$rowNumber 2;
        
$columnCount 0;
        if (
$rowData) {
            foreach (
$rowData as $row) {
                if (
$row) {
                    
$columnNumber 0;
                    
$columnReference 'A';
                    foreach (
$row as $columnHeading => $columnValue) {
                        
                       if (
$rowNumber == 2) {
                           
// First row so output Headings
                           
$objPHPExcel->getActiveSheet()->setCellValue($columnReference '1'$columnHeading);
                           
$objPHPExcel->getActiveSheet()->getColumnDimension($columnReference)->setAutoSize(true);
                         
                           
$columnCount ++;
                       }                   
                      
                       
// Output Values
                       
$objPHPExcel->getActiveSheet()->setCellValue($columnReference $rowNumber$columnValue);
                       
                       
$columnNumber ++;
                       
$columnReference PHPExcel_Cell::stringFromColumnIndex($columnNumber);
                   }
                   
                   if (
$rowNumber == 2) {
                       
// First row so do Title Row formatting
                       
$objPHPExcel->getActiveSheet()->getStyle('A1:' $columnReference '1')->getAlignment()->setWrapText(true)->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
                       
$objPHPExcel->getActiveSheet()->getStyle('A1:' $columnReference '1')->getFont()->setName('Arial');
                       
$objPHPExcel->getActiveSheet()->getStyle('A1:' $columnReference '1')->getFont()->setSize(12);
                       
$objPHPExcel->getActiveSheet()->getStyle('A1:' $columnReference '1')->getFont()->setBold(true);
                        
$objPHPExcel->getActiveSheet()->getStyle('A1:' $columnReference '1')->applyFromArray(
                            array(
                                
'fill' => array(
                                     
'type'       => PHPExcel_Style_Fill::FILL_SOLID,
                                     
'color'        => array('argb' => 'FFF0F0F0'
                                 )
                            )
                        );                        
                  
                       
// Freeze Title Row / 1st Column (change from B2 to A2 if you only want to freeze top row)
                       
$objPHPExcel->getActiveSheet()->freezePane('B2'); 
                   }               
                }            
                
$rowNumber ++;
            }

            if (
$rowNumber 2) {

                
// Format Data
                
$rowNumber ++;
                
$lastColumn PHPExcel_Cell::stringFromColumnIndex($columnCount);
                
$objPHPExcel->getActiveSheet()->getStyle('A2:' $lastColumn $rowNumber)->getFont()->setName('Arial');
                
$objPHPExcel->getActiveSheet()->getStyle('A2:' $lastColumn $rowNumber)->getFont()->setSize(10);
                
$objPHPExcel->getActiveSheet()->getStyle('A2:' $lastColumn $rowNumber)->getFont()->setBold(false);

                
// Format Columns
                
for ($columnNumber 0$columnNumber $columnCount$columnNumber ++) {
                    
$columnReference PHPExcel_Cell::stringFromColumnIndex($columnNumber);
                    if (
$columnFormatting[$columnNumber]['alignment'] == 'L') {
                        
$objPHPExcel->getActiveSheet()->getStyle($columnReference '2:' $columnReference $rowNumber)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
                    } elseif (
$columnFormatting[$columnNumber]['alignment'] == 'C') {
                        
$objPHPExcel->getActiveSheet()->getStyle($columnReference '2:' $columnReference $rowNumber)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                    } elseif (
$columnFormatting[$columnNumber]['alignment'] == 'R') {
                        
$objPHPExcel->getActiveSheet()->getStyle($columnReference '2:' $columnReference $rowNumber)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
                    }
                    if (
$columnFormatting[$columnNumber]['format'] != '') {
                        
$objPHPExcel->getActiveSheet()->getStyle($columnReference '2:' $columnReference $rowNumber)->getNumberFormat()->setFormatCode($columnFormatting[$columnNumber]['format']);
                    }
                    if (
$columnFormatting[$columnNumber]['total'] == 1) {
                        
$objPHPExcel->getActiveSheet()->getStyle($columnReference $rowNumber)->getFont()->setBold(false);
                        
$objPHPExcel->getActiveSheet()->setCellValueExplicit($columnReference $rowNumber'=SUM(' $columnReference .'2' ':' $columnReference . ($rowNumber-1) . ')'PHPExcel_Cell_DataType::TYPE_FORMULA);
                        
$objPHPExcel->getActiveSheet()->getStyle($columnReference $rowNumber)->getFont()->setBold(true);
                    }
                }
            }
            
$objPHPExcel->getActiveSheet()->getStyle('A' $rowNumber);
        }
       
        
header('Content-Type: application/vnd.ms-excel');
        
header('Content-Disposition: attachment;filename="' uniqid($title '_'true) . '"');
        
header('Cache-Control: max-age=0');
        
$objWriter PHPExcel_IOFactory::createWriter($objPHPExcel'Excel5');
        
$objWriter->save('php://output'); 
        
    }   
}
  
?>

PHP Demo Source Code Index