Home >php教程 >php手册 >三行代码实现ThinkPHP导出Excle方法,不依赖第三方库

三行代码实现ThinkPHP导出Excle方法,不依赖第三方库

WBOY
WBOYOriginal
2016-06-07 11:34:121698browse

Excle导出方法
ThinkPHP最简单的Excle导出方法,不依赖第三方库,三行代码就能实现。虽然ThinkPHP用过很多年、但是在导出Excle方式还是新手,下面的代码仅供参考。如果大神们发现本方法有什么不妥之处,请多多指教,大家一起探讨,一起进步,谢谢各位啦。
此方法比较简单就直接上代码了。
首先是Action里的代码    /**<br>      *<br>      * 导出Excel<br>      */<br>     public function expExcel(){//导出Excel<br>         $fileName = $_SESSION['loginUserName'].date('_YmdHis');<br>         $xlsModel = D('Gyjyl');<br>         $xlsData  = $xlsModel->where($map)->select();<br>         $this->assign('list',$xlsData);<br>         header('pragma:public');<br>         header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$fileName.'.xls"');<br>         header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印<br>         $this->display();<br>          <br>     }把上面控制器的数据输出到expExcel.html 模板里
expExcel.html:    <table> <br>         <tbody> <br>             <th>{$Think.session.loginUserName}-{$vo['jyrq']|date="Y-m-d",###}-柜员交易量</th> <br>         </tbody> <br>         <thead> <br>         <tr> <br>             <th>class="{$_REQUEST._sort}">网点名称</th> <br>             <th>柜员号</th> <br>             <th>柜员名</th> <br>             <th>网银笔数</th> <br>             <th>短信笔数</th> <br>             <th>活期笔数/金额</th> <br>             <th>定期笔数/金额</th> <br>             <th>交易日期</th> <br>         </tr> <br>         </thead> <br>         <tbody> <br>         <volist><br>             <tr> <br>                 <td>{$vo['gyjg']|getDepartmentNameToJgh}</td> <br>                 <td>{$vo['gybh']}</td> <br>                 <td>{$vo['gybh']}</td> <br>                 <td>{$vo['wybs']}</td> <br>                 <td>{$vo['dxbs']}</td> <br>                 <td>{$vo['hqbs']}/{$vo['hqje']}</td> <br>                 <td>{$vo['dqbs']}/{$vo['dqje']}</td> <br>                 <td>{$vo['jyrq']|date="Y-m-d",###}</td> <br>             </tr> <br>         </volist><br>         </tbody> <br>     </table>最后就是前台调用的代码了<a><span>导出EXCEL</span></a>最后说几句:本方法的重点是 用Html的header属性 把Html格式重置为Excle文件。

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn