//导出的数据源 二维数组
$data = [
['name' => '1','phone' =>'1的电话','pic_url' =>[]],
['name' => '2','phone' =>'2的电话','pic_url' =>[]]
];
//设置参数
$indexKey = ['name', 'phone', 'pic_url'];//与表头对应的要导出的二维数组的键
$thValue = ["姓名", '电话', '图片'];//表头
$fileName = "测试导出表";//表名
$table = '';
//需要特殊处理的单元格,对应二维数组的键
$urlArr = ['pic_url'];
//表头
$table .= "<table border='1' cellspacing='0' cellpadding='0'><thead><tr>";
foreach($thValue as $v) {
$table .= "<th> ".$v."</th >";
}
$table .= "</tr></thead><tbody>";
//主体
foreach ($data as $value) {
$table .= "<tr align='left'>";
foreach ($indexKey as $val) {
if (in_array($val,$urlArr)) {
//处理图片单元格 且合并到一格
$table .= "<td width='800'>";
//示例的链接为数组,如果不是数组将遍历去除即可
foreach ($value[$val] as $v_url) {
$table .= "<div style='width: 800px;'><a href='". $v_url ."'>" . $v_url . "</a></div>";
}
$table .= "</td>";
} else {
//普通单元格 内容前面留一个空格,防止长数字被格式化
$table .= "<td width='150'> " . $value[$val] . "</td>";
}
}
$table .= "</tr>";
}
//表结尾
$table .= "</tbody></table>";
//通过header头控制输出excel表格
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header('Content-Disposition:attachment;filename="'.$fileName.'.xls"');
header("Content-Transfer-Encoding:binary");
echo $table;