如何不使用第三方類庫用thinkphp導(dǎo)出Excel文件

2023-02-21    分類: 網(wǎng)站建設(shè)

PHPExcel

學(xué)習(xí)tp3的程序員都知道,php導(dǎo)出excel文件只要通過第三方類庫PHPExcel即可實現(xiàn),而且簡單方便,代碼如下圖所示:
但有時因為是無法設(shè)置各個字段的寬度,有時會很多顯示的問題,所以我下面講的是用自己的方法生成的excel文件,它的原理跟生成純靜態(tài)頁面是一樣的,就是把html的文件換成excel文件而已,直接看下面代碼:
靜態(tài)頁面:Excel.html



















姓名等級備注
{$item.realname}{$item.level}{$item.detail}


控制器(節(jié)選):
function excel(){
$this->assign('list', $lists); // 賦值數(shù)據(jù)集
$content = $this->fetch('excel');
$filename = "excel/member_".date('Ymdhis').rand(10000).".xls";//導(dǎo)出的文件名稱
$handle=fopen($filename,"w");//不存在這個文件就創(chuàng)建;
fwrite($handle,$content);//將excel.html內(nèi)容寫到文件里
$file = fopen($filename,"r"); // 打開文件
// 輸入文件標(biāo)簽
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".filesize($filename));
Header("Content-Disposition: attachment; filename=" . $filename);// 輸出文件內(nèi)容
echo fread($file,filesize($file_dir . $filename)); //保存內(nèi)容,并打開或下載文件
fclose($file);
exit();
}
PHP
全部代碼如下所示:
導(dǎo)出
導(dǎo)出的文件打開后如下圖所示:

本文標(biāo)題:如何不使用第三方類庫用thinkphp導(dǎo)出Excel文件
轉(zhuǎn)載來于:http://bm7419.com/news/238552.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、全網(wǎng)營銷推廣、網(wǎng)站排名Google、網(wǎng)站收錄、自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營