PHP鍊式操作輸出excel(csv)

WBOY
發布: 2016-08-08 09:32:54
原創
977 人瀏覽過

工作中常會遇到產品營運讓導出一些簡單的比較規範的數據,這時候要是有一個簡單的方法可以用就簡單多了。下面是我的一個輸出簡單的excel(csv)的方法類,用到了鍊式操作。說到鍊式操作,在jquery可能常用到,是不是也感覺到鍊式操作用起來很爽,我們也在這個類別中實作下鍊式操作。

其實鍊式操作很簡單的,就是在前一個類別方法最後回傳一個該類別的物件($this),提供給下一個方法呼叫。

<?<span>php </span><span>class</span><span> Array2csv{ </span><span>/*</span><span> *@var string $ext 扩展名 </span><span>*/</span> <span>private</span> <span>$ext</span> = 'csv'<span>; </span><span>/*</span><span>* 
     * @desc构造方法
     * @param string $filename 要输出的文件名
     * @param string $ext 扩展名 </span><span>*/</span> <span>public</span> <span>function</span> __construct(<span>$filename</span>,<span>$ext</span>=<span>null</span><span>){ </span><span>ob_start</span><span>(); </span><span>header</span>("Content-type: text/html;charset=utf-8"<span>); </span><span>header</span>("Content-type: application/x-csv"<span>); </span><span>if</span>(PHP_SAPI == 'cli') <span>echo</span> "CLI模式下不能导出csv文件\r"<span>; </span><span>$this</span>->ext = <span>$ext</span> === <span>null</span> ? <span>$this</span>->ext : <span>$ext</span><span>; </span><span>header</span>("Content-Disposition: attachment;filename=".<span>$filename</span>.".".<span>$this</span>-><span>ext); </span><span>ob_flush</span><span>(); </span><span>return</span> <span>$this</span><span>;
    } </span><span>/*</span><span>* 
     * @desc 打印excel标题
     * @param array $title 要输出的标题行
     * @param object Array2csv 对象本身 </span><span>*/</span> <span>public</span> <span>function</span> title(<span>$title</span><span>){ </span><span>$title</span> = <span>implode</span>(",", <span>$title</span><span>); </span><span>echo</span> <span>$title</span>."\n"<span>; </span><span>return</span> <span>$this</span><span>;
    } </span><span>/*</span><span>* 
     * @desc 打印一行excel内容
     * @param array $body 要输出的内容
     * @param object Array2csv 对象本身 </span><span>*/</span> <span>public</span> <span>function</span> body(<span>$body</span><span>){ </span><span>if</span>(!<span>is_array</span>(<span>$body</span>) || <span>empty</span>(<span>$body</span><span>)) { </span><span>return</span> <span>false</span><span>;
        } </span><span>$body</span> = <span>implode</span>(",", <span>$body</span><span>); </span><span>echo</span> <span>$body</span>."\n"<span>; </span><span>return</span> <span>$this</span><span>;
    } </span><span>/*</span><span>* 
     * @desc 打印多行excel内容
     * @param array $bodyArr 要输出的多行内容
     * @param object Array2csv 对象本身 </span><span>*/</span> <span>public</span> <span>function</span> multiBody(<span>$bodyArr</span><span>){ </span><span>if</span>(!<span>is_array</span>(<span>$bodyArr</span>) || <span>empty</span>(<span>$bodyArr</span>)) <span>return</span> <span>false</span><span>; </span><span>foreach</span> (<span>$bodyArr</span> <span>as</span> <span>$key</span> => <span>$value</span><span>) { </span><span>if</span>(<span>is_array</span>(<span>$value</span><span>)){ </span><span>$value</span> = <span>implode</span>(",", <span>$value</span><span>); </span><span>echo</span> <span>$value</span>."\n"<span>;
            }
        } </span><span>return</span> <span>$this</span><span>;
    }
} </span><span>$test</span> = <span>new</span> Array2csv('test'<span>); </span><span>$arr</span> = <span>array</span><span>( </span><span>array</span>('luluyrt@163.com','奔跑的Man1','奔跑的userman'), <span>array</span>('luluyrt@163.com','奔跑的Man2','奔跑的userman'), <span>array</span>('luluyrt@163.com','奔跑的Man3','奔跑的userman'), <span>array</span>('luluyrt@163.com','奔跑的Man4','奔跑的userman'), <span>array</span>('luluyrt@163.com','奔跑的Man5','奔跑的userman'), <span>array</span>('luluyrt@163.com','奔跑的Man6','奔跑的userman'<span>)
); </span><span>$test</span>->title(<span>array</span>('测试','呵呵','哈哈'))->body(<span>array</span>('100,sadkl','sdsas','sdvsvdd分'))->multiBody(<span>$arr</span>);
登入後複製

 

輸出的csv如下圖所示:

但是這裡面有個問題,從編碼是utf-8的資料庫中取出來的漢字輸出excel(csv)會亂碼,這個時候你要注意,要在獲取資料之前設定資料庫的編碼,比如說我的需要的是utf-8進行輸出,這個時候就要

<span>$link</span> = <span>mysqli_connect</span>(<span>$host</span>, <span>$user</span>, <span>$passwd</span>, <span>$db</span><span>); </span><span>mysqli_query</span>(<span>$link</span>, "set names utf8");
登入後複製

 

進行編碼轉換方可正常顯示,下面是我的資料庫資訊和編碼前後效果:

資料庫資訊

設定資料庫編碼前後的對比

send Me~

以上就介紹了PHP鍊式操作輸出excel(csv),包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板