|
Hi, The following code allows the downloading of database records into an excel sheet. Can you help me so that the excel sheet gets the headings like Name Address Phone ...... each time.
include("excelwriter.inc.php");
/* * @Params * $sql=A valid SQL query. * $fileName=Name of the file wants to save as.On leaving balnk it takes the table name as file name * $download=Boolean value whether you want to save on disk or you want to force to download the file. */ function ImportData($sql,$fileName="har_excel.xls",$download=false) { // Get database object $db = new DB(); $excel = new ExcelWriter($fileName); if($excel==false) { $this->error=$excel->error; return false; }
$db->query($sql); if($db->nf()==0) { $this->error="No data found in the table"; return false; } else { while($db->next_record()){ $fields[] = $db->Record; } foreach($fields as $myfields) { $excel->writeLine($myfields); } } $excel->close(); $db->close(); if($download) { if(!headers_sent()){ $this->download_file($fileName,true); }else { $this->error="Error :Headers already Sent.Can't Download file."; } } return; }
function download_file($filename,$isDel=false) { // Get database object $db = DB();
$file=basename($filename); if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) { $file = preg_replace('/\./', '%2e', $file, substr_count($file, '.') - 1); }
// make sure the file exists before sending headers if(!$fdl=@fopen($filename,'r')) { die("<br>Cannot Open File!<br>"); exit; } else { header("Cache-Control: ");// leave blank to avoid IE errors header("Pragma: ");// leave blank to avoid IE errors header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"$file\""); header("Content-length:".(string)(filesize($filename))); sleep(1); fpassthru($fdl); } if($isDel) { @unlink($filename); } }
|