At work, we often encounter product operations that ask us to export some simple and relatively standardized data. At this time, it would be much simpler if there was a simple method to use. Below is a method class of mine that outputs simple excel (csv), using chain operations. Speaking of chain operations, they may be often used in jquery. Do you also feel that chain operations are very comfortable to use? We also implement down-chain operations in this class.
In fact, the chain operation is very simple. It is to return an object of this class ($this) at the end of the previous class method and provide it to the next method call.
<?<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>);
The output csv is as shown below:
But there is a problem here. The Chinese character output to excel (csv) taken from the database with UTF-8 encoding will be garbled. At this time, you should pay attention to setting the encoding of the database before obtaining the data. For example, what I need It is utf-8 for output. At this time, it is necessary to
<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");
Only after encoding conversion can it be displayed normally. Here is my database information and the effect before and after encoding:
Database information
Comparison before and after setting up database encoding
send Me~
The above introduces the PHP chain operation output to excel (csv), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.