php 在php中将数组数组导出到Excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10424847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:07:23  来源:igfitidea点击:

Export an Array of Arrays to Excel in php

phparraysexcel

提问by user1371513

I have an Array of arrays at the beginning of each sub array is the header of the column followed by integers that I want to populate the column. It looks something like this:

我在每个子数组的开头都有一个数组数组,它​​是列的标题,后面是我想要填充该列的整数。它看起来像这样:

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 ) )

Is there a way to break up the array and get it to export to Excel?

有没有办法分解数组并将其导出到Excel?

回答by Baba

Excel can open csv file directly ... try

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);

回答by verni

If you really want to export array to excel, have a look at PHPReport. I have written that class to simplify exporting with phpexcel. It supports xls and xlsx.

如果您真的想将数组导出到 excel,请查看PHPReport。我编写了该类以简化使用 phpexcel 的导出。它支持 xls 和 xlsx。