Skip to content

Instantly share code, notes, and snippets.

@ken-muturi
Last active August 29, 2015 14:01
Show Gist options
  • Save ken-muturi/74fb37316b09d155d592 to your computer and use it in GitHub Desktop.
Save ken-muturi/74fb37316b09d155d592 to your computer and use it in GitHub Desktop.
Export to excel
<?php
$actual_headers = array();
foreach ($data['fields'] as $field)
{
$actual_headers [] = $data['actual_headers'][$field];
}
$path = realpath(BASEPATH."../assets/fmt/xls/");
$fname = $title."-".time().".csv";
$fp = fopen("{$path}/{$fname}", 'w');
fputcsv($fp, $actual_headers);
foreach ($data['result']->result() as $result)
{
$row = array();
foreach ($data['fields'] as $field)
{
$row [] = $result->{$field};
}
fputcsv($fp, $row);
}
fclose($fp);
header('Content-type: application/csv');
header("Content-Disposition: inline; filename=".$fname);
readfile("{$path}/{$fname}");
exit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment