-2

我想向 Excel 文件发送一个数组,这是一个包含不同类别的数组。

每个类别应进入一列。我编写的代码是将所有值发送到 Excel 上的一个字段。我在哪里弄错了?

$shop['id']=$id_array;
    $shop['name']=$name_array;
    $shop['cat1'] = $cat1;
    $shop['cat2'] = $cat2;
    $shop['cat3'] = $cat3;


    $id_excel=implode(",",$shop['id']);
    $name_excel=implode(",",$shop['name']);
    $cat1_excel=implode(",",$shop['cat1']);
    $cat2_excel=implode(",",$shop['cat2']);
    $cat3_excel=implode(",",$shop['cat3']);

}
    $filename ="cat.xls";
    $contents = "$id_excel \t $name_excel \t $cat1_excel \t $cat2_excel \t $cat3_excel \t \n";
    header('Content-type: application/ms-excel');
    header('Content-Disposition: attachment; filename='.$filename);
    echo $contents;
4

2 回答 2

1

尝试使用这个不错的插件:PHPExel 使用这个插件很简单

于 2012-10-19T13:00:13.040 回答
0

请尝试返回 CSV 文件而不是 Excel。这是代码:

<? $shop['id']=$id_array;
    $shop['name']=$name_array;
    $shop['cat1'] = $cat1;
    $shop['cat2'] = $cat2;
    $shop['cat3'] = $cat3;



    $id_excel[]=implode(",",$shop);

    $filename ="cat.csv";
    $contents = implode("\n",$id_excel);
    header('Content-type: text/csv');
    header('Content-Disposition: attachment; filename='.$filename);
    echo $contents;
?>
于 2012-10-19T12:58:51.717 回答