Skip to content

Instantly share code, notes, and snippets.

@GarryOne
Last active January 30, 2019 13:51
Show Gist options
  • Save GarryOne/b4f6320f7c8659a1c3145a59f8122103 to your computer and use it in GitHub Desktop.
Save GarryOne/b4f6320f7c8659a1c3145a59f8122103 to your computer and use it in GitHub Desktop.
PHPExcel arrayToExcelDownload() function
<?php
function arrayToExcelDownload($data, $filename = FALSE) {
if(!isset($filename)) {
$filename = 'crm_export_' . date('m.d.Y') . 'xlsx';
}
$excel = new PHPExcel();
$excel->setActiveSheetIndex(0);
$excel->getActiveSheet()->setTitle("Raport clienti");
$col = 0;
foreach($data[0] as $columnName => $value) {
$excel->getActiveSheet()->setCellValueByColumnAndRow($col++, 1, $columnName);
}
$row = 2;
foreach ($data as $ach) {
foreach(array_values($ach) as $columnIndex => $value) {
$excel->getActiveSheet()->setCellValueByColumnAndRow($columnIndex, $row, $value);
}
$row++;
}
foreach (range(0, --$col) as $column) {
$excel->getActiveSheet()->getStyleByColumnAndRow($column, 1)->getFont()->setBold(true);
$excel->getActiveSheet()->getColumnDimensionByColumn($column)->setAutoSize(true);
}
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename='".$filename."'");
header("Cache-Control: max-age=0");
$objWriter = PHPExcel_IOFactory::createWriter($excel, "Excel5");
return $objWriter->save("php://output");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment