Export an Array of Arrays to Excel in php

后端 未结 3 1563
半阙折子戏
半阙折子戏 2020-12-09 22:27

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 thi

3条回答
  •  抹茶落季
    2020-12-09 22:38

    Excel can open csv file directly ... try

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

提交回复
热议问题