原理主要是php代码生成excel文件,被输出内容以‘,’隔开,自动识别逗号符隔开的内容放入excel内不同的表格内,需要换行处加上转义符\n,输出内容涉及中文的需转化编码utf-8为gbk。
方式一:
header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:p_w_upload;filename=pay-".time().".xls"); header('Pragma: no-cache'); header('Expires: 0'); foreach ($datas as $val) { echo iconv('UTF-8','GBK//IGNORE',implode("\t",$val))."\n"; }
方式二:
$str_list = ''; foreach($result as $k=>$v){ $str_list .= $arr[$k]['createtime'].",".$arr[$k]['sid'].",".iconv('utf-8','GBK',$arr[$k]['accountingtype']).",".iconv('utf-8','GBK', $arr[$k]['accountingitem']).",".iconv('utf-8','GBK',$arr[$k]['moneytype'])."," . iconv('utf-8','GBK',$arr[$k]['remark']) . "," . iconv('utf-8','GBK',$arr[$k]['accountingtype1']). "," .iconv('utf-8','GBK',$arr[$k]['balance']). ""."\n"; } $bankcard = iconv('utf-8','GBK','时间'); //中文转码 $accountname = iconv('utf-8','GBK','商家id'); $money = iconv('utf-8','GBK','收/支'); $moenytype = iconv('utf-8','GBK','记账项目'); $bankname = iconv('utf-8','GBK','账户类型'); $bankbranch = iconv('utf-8','GBK','内容'); $provice = iconv('utf-8','GBK','发生金额'); $diqu = iconv('utf-8','GBK','账户余额'); $str = $bankcard.",".$accountname.",".$money.",".$moenytype.",".$bankname.",".$bankbranch.",".$provice.",".$diqu."\n"; $filename = date('YmdHis').'.csv'; $this->export_csv($filename,$str.$str_list);