m35/thecsv

yii2 csv导出扩展

安装数量: 45,800

依赖项: 0

建议者: 0

安全: 0

星标: 11

关注者: 1

分支: 12

类型:yii2-extension

v1.0-stable 2015-07-08 03:17 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:15:58 UTC


README

yii2 csv导出扩展

1. 安装

运行 php composer.phar require m35/thecsv

在您的composer.json文件中添加 "m35/thecsv": "*"

2. 使用

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

3. 参数 & 示例

3.0 参数

3.0.1 字符串类型

您可以将表名指定为参数,它将导出表的所有数据,并自动生成作为csv头部的表字段名称。

3.0.2 数组类型

    1. table:表名(string
    1. fields:您想要导出的表字段,如果没有设置,将导出所有字段(array
    1. exceptFields:如果为true,将排除您设置的表字段,默认为false(bool
    1. header:csv头部(array
    1. condition:使用where部分的条件与表一起使用(mixed)。详细信息:[https://yiiframework.cn/doc-2.0/yii-db-query.html#where()-detail](https://yiiframework.cn/doc-2.0/yii-db-query.html#where()-detail)
    1. limit:与表一起限制(int
    1. offset:与表一起偏移(int
    1. orderby:与表一起排序(mixed)。详细信息:[https://yiiframework.cn/doc-2.0/yii-db-querytrait.html#orderBy()-detail](https://yiiframework.cn/doc-2.0/yii-db-querytrait.html#orderBy()-detail)
    1. name:导出文件名。例如'data.csv',如果没有设置,将自动生成(string
    1. sql:使用SQL语法导出数据(string
    1. bind:使用SQL语法绑定值(array
    1. target:您想要保存文件的目录名,如果设置为字符串,它将使行为转换为在服务器上保存文件而不是下载文件(string
    1. fp:您可以直接指定放置数据的资源(resource
    1. data:直接设置导出数据,而不是从表中选择(array
    1. query:Yii2框架查询对象(yii\db\Query)。详细信息:[https://yiiframework.cn/doc-2.0/yii-db-query.html](https://yiiframework.cn/doc-2.0/yii-db-query.html)
    1. reader:Yii2框架DataReader对象(yii\db\DataReader)。详细信息:[https://yiiframework.cn/doc-2.0/yii-db-datareader.html](https://yiiframework.cn/doc-2.0/yii-db-datareader.html)

3.1 示例:导出表数据(假设我们有一个名为'user'的表)

3.1.1 从'user'表导出所有数据

theCsv::export('user');

3.1.2 仅导出'user'表的'username'和'password'字段

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

3.1.3 不包含'status'字段导出数据

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

3.1.4 仅导出'user'表的'username'和'password'字段并指定csv头部

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

3.1.5 不包含头部导出数据

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

3.1.6 使用条件导出活跃用户数据

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

详细信息:[https://yiiframework.cn/doc-2.0/yii-db-query.html#where()-detail](https://yiiframework.cn/doc-2.0/yii-db-query.html#where()-detail)

3.1.7 使用条件、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 使用查询对象

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

详细信息:[https://yiiframework.cn/doc-2.0/yii-db-query.html](https://yiiframework.cn/doc-2.0/yii-db-query.html)

3.1.11 使用DataReader对象

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

详细信息:[https://yiiframework.cn/doc-2.0/yii-db-datareader.html](https://yiiframework.cn/doc-2.0/yii-db-datareader.html)

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',    // file name
    'target' => './',        // the directory you want to save the file other than downloading it
]);

3.3.3

$fp = fopen('./data.csv', 'w');
theCsv::export([
    'data' => [
        ['a', 'b', 'c'],
        ['A', 'B', 'C'],
    ],
    'fp' => $fp,    // fp resource you want to put the data in
]);

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

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、字符串类型

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

3.0.2、数组类型参数

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请参考https://yiiframework.cn/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资源,则默认行为从下载变为直接写入该资源
]);