This article introduces PHP to export the data into Excel table methods. It has a certain reference value. Friends who need it can refer to it. I hope it will be helpful to everyone.
php develops and exports excel tables. How to write the code? Today I want to share this with you. What we want to do is to export the data in the database into an excel table, and export it to what we want according to our rules. Let’s directly upload the source code to you,
This is the specific logic code
$list = Db::table('form')->where('create_time', '>', $stat_time)->select() ->where('create_time','<',$end_time); if(empty($list)){ echo "<script>alert('暂时无数据');window.history.back();</script>"; exit(); } //dump($list);die; foreach ($list as $key => $value) { $tuij=Db::table('form')->where('id',$value['id'])->find(); $arr[$key]['username']=$tuij['username']; $arr[$key]['phone']=$tuij['phone']; $arr[$key]['source']=$tuij['source']; $arr[$key]['text']=$value['text']; $arr[$key]['create_time']=$value['create_time']; } if(empty($list)){ echo "<script>alert('暂时无数据');window.history.back();</script>"; exit(); } //$list为所需要导出的数据 $header=array('姓名','电话','来源','留言','提交时间'); $index=array('username','phone','source','text','create_time'); $filename="表单落地页有效推广"; $this->createtable($arr,$filename,$header,$index); }
The last line of the above code mentions a method createtable. This is a public method. You can put it in a public class or directly. In this class, the following is the source code
/** * 导出公共方法 * * @return \think\Response */ function createtable($list,$filename,$header,$index){ header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:filename=".$filename.".xls"); $teble_header = implode("\t",$header); $strexport = $teble_header."\r"; foreach ($list as $row){ foreach($index as $val){ $strexport.=$row[$val]."\t"; } $strexport.="\r"; } $strexport=iconv('UTF-8',"GB2312//IGNORE",$strexport); exit($strexport); } ``````php
[Recommended learning: PHP video tutorial]
The above is the detailed content of php export data into excel table. For more information, please follow other related articles on the PHP Chinese website!