Jump to content

Recommended Posts

I am getting a Excel error message that the file is in different format or may be corrupted when I try to open the file I download from my site. My code is follows:

<?php
session_cache_limiter('none');
session_start();
include "db_conn_open.php"; 
$filename = 'MarketBasket_Report_' . date("Y-m-d").'.xls';


 header("Content-type: application/xls");
 header("Content-Disposition: attachment; filename=$filename");


/*
$sql = "SELECT a.ordName, b.id, Date_format(b.ordersent_timestamp,'%c/%e/%Y')as 'Order Sent', b.orderid as 'Order ID', DATE_Format( b.orderconfirmation_timestamp,'%c/%e/%Y' ) as 'Order Confirmation', b.ponumber as PO FROM orders as a inner join PunchOutLog as b on a.OrdId = b.OrderId";
*/


if(isset($_GET['startdate']) && !empty($_GET['startdate']) && isset($_GET['enddate']) && !empty($_GET['enddate'])){

$start_date =  date('m/d/Y',strtotime($_GET['startdate']));
$end_date =  date('m/d/Y',strtotime($_GET['enddate']));



$sql = "SELECT a.ordName, b.id, Date_format(b.ordersent_timestamp,'%m/%d/%Y')as 'Order Sent', b.orderid as 'Order ID', DATE_Format( b.orderconfirmation_timestamp,'%m/%d/%Y' ) as 'Order Confirmation', b.ponumber as PO FROM orders as a inner join PunchOutLog as b on a.OrdId = b.OrderId WHERE Date_format(b.ordersent_timestamp,'%m/%d/%Y') > '$start_date' and Date_format(b.ordersent_timestamp,'%m/%d/%Y')< '$end_date' ORDER BY b.ordersent_timestamp  DESC";




}
/*else
{

$sql = "SELECT a.ordName, b.id, Date_format(b.ordersent_timestamp,'%c/%e/%Y')as 'Order Sent', b.orderid as 'Order ID', DATE_Format( b.orderconfirmation_timestamp,'%c/%e/%Y' ) as 'Order Confirmation', b.ponumber as PO FROM orders as a inner join PunchOutLog as b on a.OrdId = b.OrderId";



}
*/


$sep = "\t";

$result=mysql_query($sql);

for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "\t";  } 


 while($row = mysql_fetch_row($result))  { 
 $schema_insert = ""; 
 for($j=0; $j<mysql_num_fields($result);$j++) {
 if(!isset($row[$j])) {
 $schema_insert .= "NULL".$sep; 
 }
 elseif ($row[$j] != ""){
 $schema_insert .= "$row[$j]".$sep;
 }
 else 
 {
 $schema_insert .= "".$sep; 
}   
 }      
$schema_insert = str_replace($sep."$", "", $schema_insert);
  $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert); 
  $schema_insert .= "\t"; 
print(trim($schema_insert)); 
print "\n";      } 
?>
Link to comment
https://forums.phpfreaks.com/topic/280396-content-type-header-giving-me-error/
Share on other sites

That's MS Excel embeds it's own meta data into the file.  You just need to accept that this message will apear and that you will need to click to open anyway when the propt comes up.  You could just save as CSV and have Excel open the CSV, that would avaoid the warning.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.