m35 2015-07-09 16:33:48 17252次浏览 11条评论 17 10 0

yii2-thecsv(Yii2框架csv数据导出扩展)

github: https://github.com/13552277443/yii2-thecsv

1、安装

运行 php composer.phar require m35/thecsv

添加 "m35/thecsv": "*"

2、使用

<?php
use m35\thecsv\theCsv;
theCsv::export('tableName'); // return true if success

3、参数及示例

3.0、参数列表

3.0.1、string类型

直接指定表名称,下载该表所有数据,自动生成表字段名称。

3.0.2、array类型参数

3.1、示例:导出数据表(以user表为例子)

3.1.1、导出数据表完整数据

theCsv::export('user');

3.1.2、导出user表的用户名和密码

theCsv::export([
    'table' => 'user',
    'fields' => ['username', 'password'],
]);

3.1.3、导出user表除status字段外的所有数据

theCsv::export([
    'table' => 'user',
    'fields' => ['status'],
    'exceptFields' => true,
]);

3.1.4、导出user表的用户名和密码,自定义表头

theCsv::export([
    'table' => 'user',
    'fields' => ['username', 'password'],
    'header' => ['账户', '密码'],
]);

3.1.5、导出user表的用户名和密码,不要表头

theCsv::export([
    'table' => 'user',
    'fields' => ['username', 'password'],
    'header' => 'no',
]);

3.1.6、导出user表有效用户,使用condition

theCsv::export([
    'table' => 'user',
    'condition' => ['status' => 1],
]);

condition请参考http://www.yiiframework.com/doc-2.0/yii-db-query.html#where()-detail

3.1.7、导出user表有效用户,使用orderby和limit

theCsv::export([
    'table' => 'user',
    'condition' => ['status' => 1],
    'orderby' => 'id DESC',
    'limit' => 10,
]);

3.1.8、自定义SQL

theCsv::export([
    'sql' => 'SELECT * FROM user',
]);

3.1.9、自定义SQL,绑定参数

theCsv::export([
    'sql' => 'SELECT * FROM user WHERE id = :id AND status = :status',
    'bind' => [':id' => 1, ':status' => 1],
]);

3.1.10、使用Query

theCsv::export([
    'query' => (new \yii\db\Query)->from('user'),
]);

3.1.11、使用reader

theCsv::export([
    'reader' => \Yii::$app->getDb()->createCommand('SELECT * FROM user')->query(),
]);

3.2、示例:导出数据

theCsv::export([
    'data' => [
        ['a', 'b', 'c'],
        ['A', 'B', 'C'],
    ],
]);

3.3、示例:其他

3.3.1

theCsv::export([
    'data' => [
        ['a', 'b', 'c'],
        ['A', 'B', 'C'],
    ],
    'name' => 'data.csv',
]);

3.3.2

theCsv::export([
    'data' => [
        ['a', 'b', 'c'],
        ['A', 'B', 'C'],
    ],
    'name' => 'data.csv',    // 自定义导出文件名称
    'target' => './',        // 如果指定导出目录,则默认行为从下载变为保存到指定目录
]);

3.3.3

$fp = fopen('./data.csv', 'w');
theCsv::export([
    'data' => [
        ['a', 'b', 'c'],
        ['A', 'B', 'C'],
    ],
    'fp' => $fp,    // 如果指定fp资源,则默认行为从下载变为直接写入该资源
]);
觉得很赞
  • 评论于 2015-07-12 16:07 举报

    经过测试表现不错

  • 评论于 2015-08-06 11:34 举报

    谢过分享,晚点去试试

  • 评论于 2015-10-22 14:33 举报

    我想问一下,导出的汉字乱码怎么解决呢?

    1 条回复
    评论于 2015-12-16 15:09 回复

    你用2010打开就不会了,2007就会

  • 评论于 2015-11-04 11:52 举报

    @楼主,请问该类,能定制csv表的样式么

  • 评论于 2015-12-14 17:39 举报

    导出乱码怎么解决

  • 评论于 2015-12-15 14:16 举报

    为什么导出没有分卷的导出??

  • 评论于 2016-09-26 19:41 举报

    3.1.2、导出user表的用户名和密码 这里我就运行不成功了,不知道什么原因。前面全表导出是可以的。

  • 评论于 2016-12-07 12:45 举报

    可以设置为文本格式吗,

  • 评论于 2017-08-23 22:40 举报

    导出整张表成功,sql语句导出失败,说什么没有$db这个变量,题主是我哪里没有做好么...

  • 评论于 2017-08-23 23:12 举报

    ![BV0{S]DQH84)I[6Q5D_5%6.png](/uploads/images/201708/23231136278_thumb.png "BV0{S]DQH84)I[6Q5D_5%6.png")

    最后根据提示把这两行代码提上去,继续照着教程练习,ok,都成功了

  • 评论于 2017-10-19 11:30 举报

    有问题的扩展

    这里只有$parameter是字符串的情况下才生成$tables

    if (is_string($parameter)) {
                $parameter = ['table' => $parameter];
                $db= \Yii::$app->getDb();
                $tables = $db->schema->getTableNames();
            }
    

    而在$parameter数组的时候并没有生成$tables所以除了导出全表不会报错有条件的都报错

    解决方法:将下面两条复制到判断是否字符串语句外

      $db= \Yii::$app->getDb();
      $tables = $db->schema->getTableNames();
    
    觉得很赞
您需要登录后才可以评论。登录 | 立即注册