Created
July 3, 2013 18:01
-
-
Save dbernar1/5921067 to your computer and use it in GitHub Desktop.
A sane way of creating excel files inside PHP - requires the PHPExcel library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function wf_fbc_create_and_send_excel_spreadsheet_to_browser( $headings, $data ) { | |
ini_set('include_path', ini_get('include_path'). PATH_SEPARATOR . dirname( __FILE__) . '/lib/phpexcel/Classes/'); | |
include 'PHPExcel.php'; | |
include 'PHPExcel/Writer/Excel5.php'; | |
$objPHPExcel = new PHPExcel(); | |
$objPHPExcel->setActiveSheetIndex(0); | |
$objPHPExcel->getDefaultStyle()->getFont()->setName('Times New Roman'); | |
$objPHPExcel->getDefaultStyle()->getFont()->setSize(10); | |
$objPHPExcel->getActiveSheet()->getStyle('1')->applyFromArray(array( 'font' => array('bold' => true,),)); | |
foreach ( $headings as $i => $heading ) { | |
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $heading); | |
} | |
foreach ( $data as $i => $row ) { | |
if ( 0 == ($i+2) % 2 ) { | |
$objPHPExcel->getActiveSheet()->getStyle( $i+2 )->applyFromArray(array( | |
'fill' => array( | |
'type' => PHPExcel_Style_Fill::FILL_SOLID, | |
'startcolor' => array( 'argb' => 'FFF0F0F0', ) | |
), | |
) ); | |
} | |
foreach ( $row as $j => $value ) { | |
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( $j, $i+2, $value ); | |
} | |
} | |
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); | |
$objWriter->save('php://output'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment