导出数组到Excel


Exporting an array to Excel

我试图导出数组的数组excel。我把它设置为一个标题变量,和一个数据变量,基本上建立一个巨大的字符串在导出中执行。但是,只传递header变量。让我展示一些代码:

这是在设置参数:

    str_replace(" ", "_", $getData['name']);
    $filename = $getData['name']."_summary.xls";
    header("Content-Type: application/x-msdownload");
    header("Content-Disposition: attachment; filename='"$filename'"");
    header("Pragma: no-cache");
    header("Expires: 0");

转到函数获取信息:

foreach($tempRS as $key=>$value)
{
    foreach($value as $iKey=>$iValue)
    {
        if($count == 6)
        {
            $iValue = str_replace('"', '""', $iValue);
            $iValue = '"'.$iValue.'"'."'n";
            $data .= trim($iValue);
            $count = 0;
        }
        else
        {
            $iValue = str_replace('"', '""', $iValue);
            $iValue = '"'.$iValue.'"'."'t";
            $data .= trim($iValue);
            $count++;
        }
    }
}
$header = "ROW HEADER 1'tROW HEADER 2'tROW HEADER 3'tROW HEADER 4'tROW HEADER 5'tROW HEADER 6'n";
print "$header'n$data";

我似乎不明白为什么我在导出中丢失了$data变量

为什么不让fputcsv()为您生成CSV数据呢?或者更好的是,您可以使用PHPExcel来输出本地的。xls/.xlsx文件,并在生成的电子表格中实际使用格式和公式,而不是将。csv伪装成Excel文件。

首先,用echo代替print。Print会导致大量的开销,因为它既返回又回显数据。

其次,不要把变量放在引号里,使用
echo $header ."'n".$data;

回答你的问题,foreach循环真的循环吗?您是否检查了$data是否包含任何数据?

一个更好的解决方案可能是:

$header = '';
echo $header;
foreach() loop here {
    //echo the data here instead of putting it in a variable
} 
或者使用http://nl.php.net/fputcsv
更好

我测试了你的代码,似乎你的trim()方法正在修剪你的'n't

如果你删除它,你的代码应该是好的。

下面是我在excel中导出产品数组的代码。

这段代码只有一个小问题:Excel不想打开它,因为格式问题,但如果你点击"是",当它提示一个错误信息,你会没事的…

开放式办公室没问题

正在修复…

$filename = "products_exports " . date("Y-m-d H_i_s") . ".xls";
/**
 * Tell the browser to download an excel file.
 */
header("Content-Type: application/x-msdownload; charset=utf-8");
header("Content-Disposition: attachment; filename='"$filename'"");
header("Pragma: no-cache");
header("Expires: 0");

/**
 * The header of the table
 * @var string
 */
$header = "";
/**
 * All the data of the table in a formatted string
 * @var string
 */
$data = "";
//We fill in the header
foreach ($productArray as $i => $product) {
    $count = 0;
    foreach ($product as $key => $value) {
        if($count == count((array)new Product("")) - 1){
            $header.= $key;
        }
        else{
            $header.= $key . "'t";
            $count++;
        }
    }
    break; /*One loop is enough to get the header !*/
}

//And then the data by the product and all the categories
/*Where $productArray = This can be your custom array of object. eg.
  array[0] = new YouCustomObject(your params...);
*/
foreach ($productArray as $i => $product) {
    $count = 0;
    foreach ($product as $key => $value) {
        if($count == count((array)new Product("")) - 1){
            $value = str_replace('"', '""', $value);
            $value = '"' . $value . '"' . "'n";
            $data .= $value;
            $count = 0;
        }
        else{
            $value = str_replace('"', '""', $value);
            $value = '"' . $value . '"' . "'t";
            $data .= $value;
            $count++;
        }
    }
}
echo $header ."'n". $data;