PHP - 导出CSV文件

时间:2013-02-02 15:49:45

标签: php

我编写了一个脚本,该脚本使用Mysql数据库中的信息导出CSV文件。这个脚本在Linux服务器(Apache)下运行没​​有问题,但它在Windows Server(Apache)下不起作用。

我认为这个问题就在这里!!!“

header('Content-Type: text/x-csv');
header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Content-Disposition: attachment; filename=iBlock_download.csv');
header('Pragma: no-cache');
header("Content-Type: text/html; charset=UTF-8");

这里有人可以帮忙吗?

由于

脚本在这里:

header('Content-Type: text/x-csv');
header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Content-Disposition: attachment; filename=iBlock_download.csv');
header('Pragma: no-cache');
header("Content-Type: text/html; charset=UTF-8");


    include('./lib/sqlString.php');
    $_id=$_GET['_id'];



$ResultPointer = mysql_query("SELECT * FROM projekte where pro_id='$_id'");
for($i = 0, $Export = ""; $i < mysql_num_rows($ResultPointer); $i++)
{

$Daten = mysql_fetch_object($ResultPointer);

$Spalte[] = str_replace("\"", "\"\"", $Daten->pro_id);
$Spalte[] = str_replace("\"", "\"\"", $Daten->pro_desc);
$Spalte[] = str_replace("\"", "\"\"", $Daten->pro_exp);
$Spalte[] = str_replace("\"", "\"\"", $Daten->pro_animal_nr);
$Spalte[] = str_replace("\"", "\"\"", $Daten->pro_organ);
$Spalte[] = str_replace("\"", "\"\"", $Daten->pro_treatment);
$Spalte[] = str_replace("\"", "\"\"", $Daten->pro_comment);
$Spalte[] = str_replace("\"", "\"\"", $Daten->pro_genotype);
$Spalte[] = str_replace("\"", "\"\"", $Daten->pro_start_date);
$Spalte[] = str_replace("\"", "\"\"", $Daten->pro_aktion);
$Spalte[] = str_replace("\"", "\"\"", $Daten->pro_txt1);
$Spalte[] = str_replace("\"", "\"\"", $Daten->pro_aktion);
$Spalte[] = str_replace("\"", "\"\"", $Daten->pro_user_name);

$ResultPointer2 = mysql_query("SELECT * FROM pro where fn_pro_id ='$Daten->pro_id'");
$sume=mysql_num_rows($ResultPointer2);

for($i = 0, $Export = ""; $i < mysql_num_rows($ResultPointer2); $i++)
{
$sume=mysql_num_rows($ResultPointer2);

$Daten2 = mysql_fetch_object($ResultPointer2);
$Spalte[] = str_replace("\"", "\"\"", $Daten2->fn_desc);
$Spalte[] = str_replace("\"", "\"\"", $Daten2->fn_order_date);

    }

for($j = 0; $j < count($Spalte); $j++)
{
$Export .= "\"" . $Spalte[$j] . "\"";
if($j != count($Spalte)-1)
{
$Export .= ";";
}
}
$Export .= "\r\n";
$Spalte = "";
}
echo $Export;

1 个答案:

答案 0 :(得分:1)

首先......不知道你的脚本是如何做到的,但在使用csv时这是一个非常有用的功能。 (http://php.net/manual/en/function.fputcsv.php

但是......为什么你有2个定义内容类型的头功能?不需要text / html,我会坚持

header('Content-Type: text/csv');  

header('Content-Type: text/plain');