返回首页 2018-02

PHP导出table到excel

2018-02-28 21:36:40 评论: 0

这几天在做报表,需要提供的Excel文件下载,本来想用phpexcel插件来生成文件的,但是感觉有点老了,又不提供更新,phpSpreadsheet又还不是稳定版本,肯定一堆错误,考虑到用到生成表格的地方不多,于​​是就利用生成HTML的表格表单来实现下面就是我的生成表单并下载的演示:

/ * <br /> <br /> <br /> <br /> <br /> 创建(导出)Excel数据表格<br /> <br /> <br /> > <br /> <br /> @param array $ lists要导出的数据格式的数据<br /> <br /> <br /> <br /> <br /> @param string $ fileName导出的Excel表格数据表的文件名<br /> <br /> <br /> <br /> <br /> <br /> @param array $ header Excel表格的表头<br /> <br /> <br /> <br /> <br /> @param array $ index $ list数组中与Excel表格表头$ header中每个项目对应的字段的名字(key value) <br /> <br /> <br /> 比如:$ header = array('编号','姓名','性别','年龄'); < <br /> <br /> <br /> <br /> <br /> $ index = array('id','username','sex','age'); <br /> <br /> <br /> <br /> <br /> $ lists = array(array('id'=> 1,'username'=>'YQJ','sex'=>'男','age'=> 24)); <br /> <br /> < > <br /> <br /> <br /> / <br /> <br /> <br /> <br /> <br /> <br /><br /> <br /> <br /> <br /> <br /><br /> <br /> <br /> <br /> <br /> 公共函数createTable($ lists,$ fileName,$ header,$ index = array()){<br /> <br /> <br /> <br /> <br /> error_reporting(0); <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> </ strong> > </ p> <br /> <br /> <br /> <br /> <table width = '50%'border ='1'> <br /> <br /> < / span> > <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <登记/> <br /> <br /> 为($ I = 0; $ I <计数($头); $ I ++){<BR /> <br /> <br /> <br /> <登记/> <br /> $表。=“</ P> <br /> <br /> <br /> <br /> <br /> <TH> {$头[$ i]于} </日> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> /> } <br /> <br /> <br /> <br /> <br /> <br /> $ table。=“</ tr> <br /> <br /> <br /> <br /> </ thead> <br /> <br /> <br /> <br /> < ($ list as $ list){<br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> < br / 登记/> <br /> <br /> <br /> $表= “</ p> <br /> <br /> <br /> <br /> <br />。 <TR>”; < ($ i = 0; $ i <count($ list); $ i ++){&lt; br /> <br /> <br /> <br /> <br /> <br / > </ p> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br / vnd.ms - Excel中。</ list> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> < / span> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br />登记/> <br /> 。$表= “</ TR> <br /> <br /> <br /> <br /> <br /> <p>”; <br /> <br /> < <br /> <br /> <br /></ tr> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> < br / <br /> <br /> <br /></ tr> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br / <br /> <br /> <br /> } <br /> <br /> <br /> <br /> $ table。=“</ tbody> <br /> <br /> <br /> <br /> <br /> </ table> <br /> <br /> <br /> <br /> <p> </ body> </ html>“; <br /> <br /> <登记/> <br /> <br /> <br /> ob_end_clean(); <br /> <br /> <br /> <br /> <br /> <br /> 标题(“内容类型:应用程序/下载“); <br /> <br /> <br /> <br /> <br /> 标题(”Pragma:no-cache“); <br /> <br /> < <br /> <br /> <br /> header('Content-Encoding:utf-8'); <br /> <br /> <br /> <br /><br /> <br /> header(“Content-Type:application / vnd.ms-excel”); <br /> <br /> <br /> <br /> <br /> <br /> header ('Content-Disposition:attachment; filename =''。$ fileName。'。xls''); <br /> <br /> <br /> <br /> <br /> 标题(“Cache-Control:must-revalidate,post-check = 0,pre-check = 0”); <br /> <br /> <br /> <br /> <br /> <br /> header ('Content-Transfer-Encoding:binary'); <br /> <br /> <br /> <br /> header('Cache-Control:max-age = 0') ; <br /> <br /> <br /> exit($ table);

}

更多