2012-05-03 21 views
3

我在每個子數組的開始處有一個數組數組,它​​是列的標題,後面跟着我想填充列的整數。它看起來像這樣:在php中將數組數組導出到Excell。

Array ([0] => Array ([0] => How was the Food? [1] => 3 [2] => 4) [1] => Array ([0] => How was the first party of the semester? [1] => 2 [2] => 4 [3] => 0)) 

有沒有辦法分解數組並將其導出到Excel?

+0

任何你不想導出爲CSV等非專有格式的原因? – maiorano84

+0

我也可以做。 Excel將只是您希望從網絡上獲得的首選 – user1371513

+0

,或者您希望代碼在服務器中生成excel? – Broncha

回答

12

Excel可以直接打開csv文件...嘗試

$array = Array (
     0 => Array (
       0 => "How was the Food?", 
       1 => 3, 
       2 => 4 
     ), 
     1 => Array (
       0 => "How was the first party of the semester?", 
       1 => 2, 
       2 => 4, 
       3 => 0 
     ) 
); 

header("Content-Disposition: attachment; filename=\"demo.xls\""); 
header("Content-Type: application/vnd.ms-excel;"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
$out = fopen("php://output", 'w'); 
foreach ($array as $data) 
{ 
    fputcsv($out, $data,"\t"); 
} 
fclose($out); 
+0

這就像一個魅力非常感謝你!你剛剛救了我一夜不睡 – user1371513

+0

歡迎你:D – Baba

2

如果你真的要導出陣列到Excel,看看PHPReport。我已經寫了這個類來簡化用phpexcel導出。它支持xls和xlsx。