【发布时间】:2013-05-28 19:22:11
【问题描述】:
将内容从 SQL 数据库表导出到 .csv 文件。如果我添加列名,在尝试打开下载的 .csv 文件时会出现以下错误:
但是,如果我在没有列的情况下导出它,它会很好地打开而不会出现错误。我尝试将 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;
?>
【问题讨论】:
-
在文本编辑器中查看文件的内容,而不是 excel。
-
@JamesBirkett 不幸的是,在 Excel 中查看它是必要的
-
发布仍然出现错误的 csv 文件的最短版本。
-
将
ID文本括在引号中:这样,它不会显示 SYLK 文件签名,并且引号不会影响导入为 CSV 时的值