Php 将数据输出为excel文件

Php 将数据输出为excel文件,php,mysql,Php,Mysql,我正在尝试打开一个文件以导出为excel。我可以在firebug中看到数据,但没有向用户提供该文件的选项。我错过了什么?我已经包括我的代码,处理查询等,并欢迎您的意见。非常感谢 header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Conte

我正在尝试打开一个文件以导出为excel。我可以在firebug中看到数据,但没有向用户提供该文件的选项。我错过了什么?我已经包括我的代码,处理查询等,并欢迎您的意见。非常感谢

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;
标题(“内容类型:应用程序/强制下载”);
标题(“内容类型:应用程序/八位字节流”);
标题(“内容类型:应用程序/下载”);
标题(“内容类型:application/vnd.ms-excel;name='excel');
标题(“内容处置:附件;文件名=“.$\u GET['e']”..xls”);
标题(“杂注:无缓存”);
标题(“到期日:0”);
$e=$_GET['e'];
//加载公共类
require_once('../../../includes/common/KT_common.php');
//加载tNG类
require_once('../../../includes/tng/tng.inc.php');
//创建一个事务调度程序实例
$tNGs=新的tNG\U调度程序(“../../”);
//创建统一的连接变量
$conn_conn=新的KT_连接($conn,$database_conn);
交换机(e美元){
案例“法案”:
mysql\u select\u db($database\u conn,$conn);
$select=“选择服务、活动、公司、用户、项目、文件框、act日期”;
$export=mysql\u查询($select);
$count=mysql\u num\u字段($export);
对于($i=0;$i<$count;$i++){
$header.=mysql\u字段\u名称($export,$i)。“\t”;
} 
而($row=mysql_fetch_row($export)){
$line='';
foreach($行作为$值){
如果((!isset($value))或($value==“”){
$value=“\t”;
}否则{
$value=str_replace(“,“,”,$value);
$value=“.”.$value.“.”“\t”;
} 
$line.=$value;
} 
$data.=修剪($line)。“\n”;
} 
$data=str_replace(“\r”,”,$data);
如果($data==“”){
$data=“\n(0)找到记录!\n”;
} 
打印“$header\n$data”;
打破

真正的问题是,您需要在内容处置头中引用文件名,但您只需要一个内容类型头,下面列出的一些其他缓存头应有助于解决IE over ssl的潜在问题(如果您使用该标题)


需要注意的是:您实际上并没有创建Excel xls文件,只是一个以标签分隔的值文件,扩展名为.xls

您有3个内容类型标题,每个标题都不同。您基本上是说“这个文件是一块松饼,这个文件是一壶沸水,这个壶也是一块袖珍绒毛。”除非你经常做袖珍松软松饼汤,否则这不可能是真的。
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');