输出数据为excel文件

时间:2010-09-18 11:22:48

标签: php mysql

我正在尝试打开导出为ex​​cel的文件。我可以在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;

1 个答案:

答案 0 :(得分:1)

真正的问题是您需要在Content-Disposition标头中引用文件名,但是您只需要一个Content-Type标头,下面列出的一些其他缓存标头应该可以帮助解决IE上的潜在问题ssl(如果你使用那个)

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');

作为一个注意事项:你实际上并没有创建一个Excel xls文件,只是一个扩展名为.xls的制表符分隔值文件