Mr.Putersmit
Mr.Putersmit

Reputation: 1

output data as excel file

i am trying to open a file for export as excel. i can see the data in firebug but there is no option to offer the file to the user. what am i missing here? i have included my code that handles the query etc and would welcome your comments. many thanks

header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-type: application/vnd.ms-excel; name='excel'");
header("Content-Disposition: attachment; filename=".$_GET['e'].".xls");
header("Pragma: no-cache"); 
header("Expires: 0"); 
$e = $_GET['e'];
// Load the common classes
require_once('../../../includes/common/KT_common.php');

// Load the tNG classes
require_once('../../../includes/tng/tNG.inc.php');

// Make a transaction dispatcher instance
$tNGs = new tNG_dispatcher("../../../");

// Make unified connection variable
$conn_conn= new KT_connection($conn, $database_conn);


    switch($e) {


       case "act":
            mysql_select_db($database_conn, $conn); 

      $select = "SELECT service, activity, company, user, item, filebox, date FROM act";  
      $export = mysql_query($select); 
      $count = mysql_num_fields($export); 
      for ($i = 0; $i < $count; $i++) { 
      $header .= mysql_field_name($export, $i)."\t"; 
      } 
      while($row = mysql_fetch_row($export)) { 
      $line = ''; 
      foreach($row as $value) { 
      if ((!isset($value)) OR ($value == "")) { 
      $value = "\t"; 
      } else { 
      $value = str_replace('"', '""', $value); 
      $value = '"' . $value . '"' . "\t"; 
      } 
      $line .= $value; 
      } 
      $data .= trim($line)."\n"; 
      } 
      $data = str_replace("\r", "", $data); 
      if ($data == "") { 
      $data = "\n(0) Records Found!\n"; 
      } 
      print "$header\n$data"; 

      break;

Upvotes: 0

Views: 1237

Answers (1)

Mark Baker
Mark Baker

Reputation: 212412

The real problem is that you need to quote the file name in your Content-Disposition header, but you only need one Content-Type header, and a few of the other caching headers listed below should help with potential issues with IE over ssl (if you use that)

header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
header ('Content-Type: application/vnd.ms-excel');
header ('Content-Disposition: attachment; filename="'.$_GET['e'].'.xls"');
header ('Content-Transfer-Encoding: binary');

As a point of note: you're not actually creating an Excel xls file, just a tab-separated value file with a .xls extension

Upvotes: 1

Related Questions