php下载xls文件
生活随笔
收集整理的這篇文章主要介紹了
php下载xls文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<a class="btn btn-primary radius" style="background:green" data-title="導出數據" data-href="" onclick="downloadMessage()" ><i class="Hui-iconfont"></i> 導出數據</a>
<iframe id="ifile" style="display:none"></iframe>
前端js請求后端下載文件:
function downloadMessage(){var dom=document.getElementById('ifile');dom.src="{{url('/excel')}}"; }后端下載文件函數:
/*** 創建(導出)Excel數據表格* @param array $list 要導出的數組格式的數據* @param string $filename 導出的Excel表格數據表的文件名* @param array $header Excel表格的表頭* @param array $index $list數組中與Excel表格表頭$header中每個項目對應的字段的名字(key值)* 比如: $header = array('編號','姓名','性別','年齡');* $index = array('id','username','sex','age');* $list = array(array('id'=>1,'username'=>'YQJ','sex'=>'男','age'=>24));* @return [array] [數組]*/protected function createtable($list,$filename,$header=array(),$index = array()){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);}后端請求響應函數
function excel(){$stu = new Stu();$cash = $stu->getAllMessage();$filename = '求索人員信息';$header = array('id','姓名','學號','學院','專業','班級','部門','電話','qq');$index = array('id','name','number','college','department','class','section','tel','qq');$this->createtable($cash,$filename,$header,$index);}:
?
總結
以上是生活随笔為你收集整理的php下载xls文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android开发流程总结
- 下一篇: php 防镜像网,wordpress防止