PHP实现简单下载xls表格
2022-04-28 16:04:24 阅读:1519
publicfunctionoutexlserver($data=array(),$title=array(),$filename='import_file_name'){
ob_end_clean();
ob_start();
header("Content-type:text/html;charset=utf-8");
header("Content-type:application/octet-stream");
header("Accept-Ranges:bytes");
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=".$filename.".xls");
header("Pragma:no-cache");
header("Expires:0");
//导出xls五个列分别是abcde
$ary=array('a','b','c','d','e');
foreach($aryas$k=>$v){
$ary[$k]=iconv("UTF-8","GBK",$v);//转码
}
$title=implode("\t",$ary);
echo"$title\n";
if(!is_array($data)){
$data=$data->all();
}
if(!empty($data)){
foreach($dataas$key=>$v){
$ary2='';
if($v['source']==1){
$source='张三';
}
if($v['source']==2){
$source='李四';
}
if($v['source']==3){
$source='王五';
}
if($v['source']==4){
$source='赵六';
}
$source=iconv("UTF-8","GBK",$source);
$source_account=iconv("UTF-8","GBK",$v['source_account']);
$prices=iconv("UTF-8","GBK",$v['prices']);
$ary2=array($v['name1'],$v['name2'],'$source,$source_account,$prices);
$datas[$key]=implode("\t",$ary2);
}
echoimplode("\n",$datas);
}
}