将mysql查询的结果导出到Excel工作表

时间:2015-05-29 04:24:55

标签: php mysql export-to-excel

<?php

  // Original PHP code by Chirp Internet: www.chirp.com.au
  // Please acknowledge use of this code by including this header.
  include 'includes/config.php'; 
  $filename = "PropertyAssetClass.xls"; // File Name

// Download file
header("Content-Disposition: attachment; filename=\"$filename\""); 
header("Content-Type: application/vnd.ms-excel");
$sql = "SELECT d.approved_comps as 'Approved Comps'
               FROM   properties.tblpropertyassetclass_property_mapping a 
                      INNER JOIN properties.tblproperty b 
                              ON a.propertyid = b.propertyid 
                      INNER JOIN properties.tblmls_add_derived c
                              ON b.address = c.propertyaddress
                      INNER JOIN properties.tblcompdetails d
                              ON c.mlsnumber = d.mlsnumber
               WHERE  a.propertyassetclassid=$_GET[propertyassetclassid]";


$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$flag = false;
//echo $sql ."\n";
while($row = mysql_fetch_assoc($result)) {
    if(!$flag) {
      // display field/column names as first row
      echo implode("\t", array_keys($row)) . "\r\n";
      $flag = true;
    }      
    echo implode("\t", array_values($row)) . "\r\n";
  }


// ?>

我正在尝试将approved_comps的值输出到Excel工作表。当我运行此代码时,将创建Excel工作表但不输出任何内容。甚至不显示字段名称Approved Comps。我知道这个mysql查询是合法的。我可以运行此查询而不会出现任何语法错误。可能是查询没有返回任何内容吗?但仍然不应该有字段名称吗?

0 个答案:

没有答案