I want to export my mysql query result to excel sheet. for that, i used the code given in Export MySQL data to Excel in PHP It is working when i check in console but not downloading. Pls help me. I dont know why it is not downloading. Here is my code:
$startDate = $_POST['fromDate'];
$endDate = $_POST['toDate'];
$db = new Db();
$data = $db -> select("SELECT od.Id,mm.ProductName,oi.Quantity,od.OrderDate,od.FirstName,od.LastName,od.Address, od.ZipCode, od.EmailID, od.ContactNumber FROM tw_orderdetails od JOIN tw_ordereditems oi ON oi.OrderID = od.Id JOIN tw_marketingmaterials mm on mm.Id = oi.ProductID WHERE date(od.OrderDate) BETWEEN ".$db->quote($startDate)." AND ".$db->quote($endDate)."");
$fieldNames = array('Order ID','ProductName','Quantity','OrderDate','FirstName','LastName','Address','ZipCode','EmailID','ContactNumber');
$filename = "excelfilename";
$file_ending = "xls";
//header info for browser
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$filename.xls");
header("Pragma: no-cache");
header("Expires: 0");
/*******Start of Formatting for Excel*******/
//define separator (defines columns in excel & tabs in word)
$sep = "\t"; //tabbed character
//start of printing column names as names of MySQL fields
for ($i = 0; $i < count($fieldNames) ; $i++) {
echo $fieldNames[$i] . "\t";
}
print("
");
//end of printing column names
//start while loop to get data
foreach ($data as &$row) {
$schema_insert = "";
foreach ($row as &$value)
{
if(!isset($value))
$schema_insert .= "NULL".$sep;
elseif ($value != "")
$schema_insert .= "$value".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert = preg_replace("/
|
|
|/", " ", $schema_insert);
$schema_insert .= "\t" ;
print(trim($schema_insert));
print "
";
}