0

将内容从 SQL 数据库表导出到 .csv 文件。如果我添加列名,则在尝试打开下载的 .csv 文件时会出现以下错误:

错误 #1

错误 #2

但是,如果我在没有列的情况下导出它,它会很好地打开而不会出现错误。我尝试将 Content-Type: 从 text/csv 更改为 application/csv,但没有更改任何内容。我没主意了。

<?php
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=applications.csv");
header("Pragma: no-cache");
header("Expires: 0");

ini_set('display_errors',1);
$private=1;
error_reporting(E_ALL ^ E_NOTICE);

mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("cif") or die(mysql_error());

$query = "SELECT * FROM applications";
$select_c = mysql_query($query) or die(mysql_error());

/* The line below is what I comment out to remove the column names */
$result.="ID,LAST_NAME,FIRST_NAME,ORGANIZATION,TITLE\n"; 
/* The line above is what I comment out to remove the column names */

while ($row = mysql_fetch_array($select_c, MYSQL_ASSOC))
{
    $result.="{$row['ID']},";
    $result.="{$row['LAST_NAME']},";
    $result.="{$row['FIRST_NAME']},";
    $result.="{$row['ORGANIZATION']},";
    $result.="{$row['TITLE']},";
    $result.="\n";

}
    echo $result; 
?>
4

1 回答 1

1

看起来您有许多列不匹配。

标题行有 5 个条目,但随后的每一行有 6 个(因为结尾有逗号)。

您可以在标题行的末尾添加一个逗号,也可以从数据行中删除尾随的逗号。


编辑: SYLK 文件以“ID”开头。 http://support.microsoft.com/kb/323626

让你的第一个列名不以“ID”开头,你会很高兴的。

于 2013-05-28T19:26:42.277 回答