0

我想将我的 php 脚本中的不同数组写入 excel 文件的不同行。我知道有很多方法可以做到这一点。

我有一个小脚本,它下载一个 excel 文件,但不支持我的数组插入到内容中。如果需要任何更改,请帮助我。

谢谢

    $shop['id']=$column['id'];
    $shop['name']=$column['name'];
    $shop['cat1'] = $cat1;
    $shop['cat2'] = $cat2;
    $shop['cat3'] = $cat3;
    $all_shops[$column['id']] = $shop; 



$filename ="cat.xls";
$contents = "$shop['cat2']";
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
echo $contents;
4

1 回答 1

-1

最简单的方法是在输出中将内容作为 HTML 流式传输。(例如:将扩展名 .html 更改为 .xls,它将在 Excel 中顺利打开)

$shop['id']=$column['id'];
$shop['name']=$column['name'];
$shop['cat1'] = $cat1;
$shop['cat2'] = $cat2;
$shop['cat3'] = $cat3;
$all_shops[$column['id']] = $shop; 

$content = "<table>";
foreach($all_shops as $k => $shop){
   $content .= "<tr><td>ID</td><td>{$shop['id']}</td></tr>"; // add other columns
}
$content .= "</table>";

$filename ="cat.xls";
$contents = "$shop['cat2']";
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
echo $contents;
于 2012-10-19T10:59:25.860 回答