• [moka同学摘录]Yii2 csv数据导出扩展


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

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

    1、安装

    运行 php composer.phar require m35/thecsv

    添加 "m35/thecsv": "*"

    2、使用

    <?php
    use m35thecsvtheCsv;
    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 yiidbQuery)->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资源,则默认行为从下载变为直接写入该资源
    ]);


    很全面哦原文地址:http://www.yiichina.com/extension/464
    我生活的地方,我为何要生活。
  • 相关阅读:
    ArcGIS中的VBA修复
    ArcSDE解除图层锁定
    Sanboxie 5.14安装图解
    获取WIFI密码
    ArcGIS安装错误1402
    地图中插入表格——ArcMap篇
    mysql 5.6 datetime 保存精确到秒
    JAVA8 ARRAY、LIST操作 汇【5】)- JAVA8 LAMBDA LIST统计(求和、最大、最小、平均)
    cloud配置中心遇到的坑
    解决 MySQL 比如我要拉取一个消息表中用户id为1的前10条最新数据
  • 原文地址:https://www.cnblogs.com/hsd1727728211/p/YII2.html
Copyright © 2020-2023  润新知