首页 > php教程 > php手册 > 正文

PHP链式操作输出excel(csv),链式csv

WBOY
发布: 2016-06-13 09:19:23
原创
1048 人浏览过

PHP链式操作输出excel(csv),链式csv

工作中经常会遇到产品运营让导出一些简单的比较规范的数据,这时候要是有一个简单的方法可以用就简单多了。下面是我的一个输出简单的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如下图所示:

$link = mysqli_connect($host, $user, $passwd, $db); mysqli_query($link, "set names utf8");

 

进行编码转换方可正常显示,下面是我的数据库信息和编码前后效果:

数据库信息

设置数据库编码前后的对比

send Me~

相关标签:
csv
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板