Skip to content

Instantly share code, notes, and snippets.

@gridphp
Last active June 14, 2020 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gridphp/70e96ee4f0225e2a6af2f76b672b2abf to your computer and use it in GitHub Desktop.
Save gridphp/70e96ee4f0225e2a6af2f76b672b2abf to your computer and use it in GitHub Desktop.
PHPExcel sample excel output of array ($arr) - https://www.gridphp.com
include_once(dirname(__FILE__).'/excel/PHPExcel/IOFactory.php');
$objPHPExcel = new PHPExcel();
// autosize excel columns
foreach(range('A','Z') as $columnID)
$objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true);
$objPHPExcel->getActiveSheet()->fromArray($arr, NULL, 'A1');
// write excel2007 standard xlsx
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$filename = $this->options["export"]["filename"];
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
// if ssl and IE, remove above cache-control and add following
if ( (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") || $_SERVER["SERVER_PORT"] == "443" )
{
header('Cache-Control: private');
header('Pragma: token');
}
else
{
header('Cache-Control: max-age=0');
}
$objWriter->save('php://output');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment