Skip to content

Instantly share code, notes, and snippets.

@dbernar1
Created July 3, 2013 18:01
Show Gist options
  • Save dbernar1/5921067 to your computer and use it in GitHub Desktop.
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
<?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