2011-09-28 184 views
0

我正試圖將數組數組導出到excel。我將它設置爲一個頭變量和一個數據變量,該變量基本上構建了要在導出中執行的巨大字符串。但是,只有標題變量正在經歷。讓我告訴一些代碼:將數組導出到Excel

這是設置參數:

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變量。

+0

這裏被定義$的數據? $數據是否有可能超出範圍? – superultranova

+0

我在foreach循環之後回顯了數據,以確保它包含我希望它包含的數據。否則,我會在進入循環之前清除$ data,之後不做任何處理。 – IceBlueFire

+0

那麼你可以回顯數據?這不能解決你的問題嗎?或者只有在設置標題時纔會出現問題? – superultranova

回答

2

爲什麼不只是fputcsv()爲您生成CSV數據?或者更好的做法是,您可以使用PHPExcel輸出原生.xls/.xlsx文件,並在生成的電子表格中實際使用格式和公式,而不是將.csv僞裝成Excel文件。

1

首先,使用echo而不是print。打印導致開銷的負載,因爲它既返回並回顯數據。

其次,不要把變量引號內,使用

echo $header ."\n".$data; 

爲了得到你的問題,並在foreach循環實際上循環?如果包含任何數據,您是否檢查過$數據?

一個更好的解決方案可能是這樣的:

$header = ''; 
echo $header; 

foreach() loop here { 
    //echo the data here instead of putting it in a variable 
} 

或者,也許更好,使用http://nl.php.net/fputcsv

+0

是的,我追溯到確保$ data變量實際上持有我想要的。它只是不被放入Excel中,這是我遇到的問題。 – IceBlueFire

+1

*「打印導致開銷的負載」* - LOL。 – hakre

+0

此外,改變回顯和分離出你的回聲,仍然不起作用。 – IceBlueFire

0

我測試你的代碼,看來你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;