Created
April 19, 2012 16:50
-
-
Save oniryx/2422253 to your computer and use it in GitHub Desktop.
Export an array as downladable Excel CSV
This file contains hidden or 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
/** | |
* Export an array as downladable Excel CSV | |
* @param array $header | |
* @param array $data | |
* @param string $filename | |
*/ | |
function toCSV($header, $data, $filename) { | |
$sep = "\t"; | |
$eol = "\n"; | |
$csv = count($header) ? '"'. implode('"'.$sep.'"', $header).'"'.$eol : ''; | |
foreach($data as $line) { | |
$csv .= '"'. implode('"'.$sep.'"', $line).'"'.$eol; | |
} | |
header('Content-Description: File Transfer'); | |
header('Content-Type: application/vnd.ms-excel'); | |
header('Content-Disposition: attachment; filename='.$filename.'.csv'); | |
header('Content-Transfer-Encoding: binary'); | |
header('Expires: 0'); | |
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); | |
header('Pragma: public'); | |
header('Content-Length: '. strlen($csv)); | |
echo chr(255) . chr(254) . mb_convert_encoding($csv, 'UTF-16LE', 'UTF-8'); | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment