export excel file
/*
+------------------------------------------------------------------+
Function will be use for generate export excel file.
@params-> $fileName : download file name
$columns : column field name in file
$listArr : list data of array
+------------------------------------------------------------------+
*/
function exportExcel($fileName,$columns,$listArr)
{
$this->load->helper('download');
$handle1 = fopen($fileName,'w');
$fileTextArray = array_values($columns);
$fileText = implode("\t",$fileTextArray)."\n";
fwrite($handle1, $fileText);
foreach($listArr as $list)
{
$fileText = implode("\t",$list)."\n";
fwrite($handle1, $fileText);
}
fclose($handle1);
$this->force_download($fileName);
unlink($fileName);
}
/*
+------------------------------------------------------------------+
Function will be use for excel download.
@params-> $file : download file name
+------------------------------------------------------------------+
*/
function force_download($file)
{
if ((isset($file))&&(file_exists($file)))
{
$fileName = str_replace("./","",$file);
header("Content-length: ".filesize($file));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$fileName.'"');
readfile($file);
}
else
echo "No file selected";
}
No comments:
Post a Comment