The below code can export data from database into excel sheet using php:
<?php
$filename ='excelreport.xls';
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test");
$res = mysql_query("select id, country from countries");
$contents = '';
while($row = mysql_fetch_assoc($res))
{
$contents .= $row['id']."\t".$row['country']."\n";
}
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
echo $contents;
?>
<?php
$filename ='excelreport.xls';
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test");
$res = mysql_query("select id, country from countries");
$contents = '';
while($row = mysql_fetch_assoc($res))
{
$contents .= $row['id']."\t".$row['country']."\n";
}
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
echo $contents;
?>
Comments
Post a Comment