参考如下范例:
header("Context-type:application/vnd.ms-excel");
header("Context-Disposition:attachement;filename=outExce.xls");
echo "
echo "
00-35A-00";echo "
";//生成excel
private function downloadRecordAddressInfoToExcel($records){
$noAddressInfoCount=0;
$address=array();
if(is_array($records)){
$tem_address= "
$table_td_s="
";$table_td_number_s="
";$table_td_e="
";$tem_address.= $table_td_s. "姓名".$table_td_e;
$tem_address.=$table_td_s."地址".$table_td_e;
$tem_address.=$table_td_s."电话".$table_td_e;
$tem_address.=$table_td_s. "订单号".$table_td_e;
$tem_address.=$table_td_s."时间".$table_td_e;
$step=0;
foreach($records as $k=>$record){
$address=json_decode($record['address'],true);
if(is_array($address)){
if($step<1){
header("Content-type:application/vnd.ms-excel;");
Header("Accept-Ranges:bytes");
header("Content-Disposition:filename=".$record['p_name'].".xls");
header("Pragma: no-cache");
header("Expires: 0");
$step++;
}
$tem_address.='
';$tem_address.=$table_td_s. $address['name'].$table_td_e.$table_td_s .$address['address'].$table_td_e.$table_td_number_s.$address['mobile'].$table_td_e;
$tem_address.=$table_td_number_s.$record['num'].$table_td_e.$table_td_s.date('Y-m-d H:i:s',$record['createtime']).$table_td_e;
$tem_address.='
';}else{
$noAddressInfoCount++;
$tem_address.="
";}
}
}
if(!empty($tem_address) &&$step>0){
$tem_address.= "
";echo $tem_address;
}else{
echo 'total:'.count($records).'
Null data total:'.$noAddressInfoCount;
}
}