Welcome to Dream.In.Code
Become a PHP Expert!

Join 137,420 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,985 people online right now. Registration is fast and FREE... Join Now!




heading to an Excel sheet

 
Reply to this topicStart new topic

heading to an Excel sheet

priti
14 Jan, 2008 - 11:40 PM
Post #1

New D.I.C Head
*

Joined: 26 Oct, 2007
Posts: 8


My Contributions
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);
}
}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/5/08 04:27AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month