yii2 csv export extension
Run php composer.phar require m35/thecsv
or
Add "m35/thecsv": "*"
in your composer.json file.
<?php
use m35\thecsv\theCsv;
theCsv::export('tableName'); // return true if success
You can specify a table name as the parameter, and it would export all the data of the table and automatically generate the table fields name as csv header.
-
table
:table name (string
)
-
fields
:the table fields you want to export, it would export all the fields if you not set (array
)
-
exceptFields
:except the table fields you set above if ture,default to false(bool
)
-
header
:the csv header(array
)
-
condition
:use the condition in where part with the table(mixed
). for details: http://www.yiiframework.com/doc-2.0/yii-db-query.html#where()-detail
-
limit
:limit with the table(int
)
-
offset
:offset with the table(int
)
-
orderby
:order by with the table(mixed
). for details: http://www.yiiframework.com/doc-2.0/yii-db-querytrait.html#orderBy()-detail
-
name
:the export file name. e.g.'data.csv'
, automatically generate if you not set(string
)
-
sql
:use the SQL syntax to export the data(string
)
-
bind
:bind values with the SQL syntax(array
)
-
target
:the directory name you want to save the file, it would turn the behavior to save the file on server other than downloading the file(string
)
-
fp
:you can directly specify the resource to put the data(resource
)
-
data
:directly set the export data other than selecting from table(array
)
-
query
:Yii2 Framework Query object(yii\db\Query
). for details: http://www.yiiframework.com/doc-2.0/yii-db-query.html
-
reader
:Yii2 Framework DataReader object(yii\db\DataReader
). for details: http://www.yiiframework.com/doc-2.0/yii-db-datareader.html
theCsv::export('user');
theCsv::export([
'table' => 'user',
'fields' => ['username', 'password'],
]);
theCsv::export([
'table' => 'user',
'fields' => ['status'],
'exceptFields' => true,
]);
theCsv::export([
'table' => 'user',
'fields' => ['username', 'password'],
'header' => ['Username', 'Password'],
]);
theCsv::export([
'table' => 'user',
'fields' => ['username', 'password'],
'header' => 'no',
]);
theCsv::export([
'table' => 'user',
'condition' => ['status' => 1],
]);
for details: http://www.yiiframework.com/doc-2.0/yii-db-query.html#where()-detail
theCsv::export([
'table' => 'user',
'condition' => ['status' => 1],
'orderby' => 'id DESC',
'limit' => 10,
]);
theCsv::export([
'sql' => 'SELECT * FROM user',
]);
theCsv::export([
'sql' => 'SELECT * FROM user WHERE id = :id AND status = :status',
'bind' => [':id' => 1, ':status' => 1],
]);
theCsv::export([
'query' => (new \yii\db\Query)->from('user'),
]);
for details: http://www.yiiframework.com/doc-2.0/yii-db-query.html
theCsv::export([
'reader' => \Yii::$app->getDb()->createCommand('SELECT * FROM user')->query(),
]);
for details: http://www.yiiframework.com/doc-2.0/yii-db-datareader.html
theCsv::export([
'data' => [
['a', 'b', 'c'],
['A', 'B', 'C'],
],
]);
theCsv::export([
'data' => [
['a', 'b', 'c'],
['A', 'B', 'C'],
],
'name' => 'data.csv',
]);
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
]);
$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
]);
运行 php composer.phar require m35/thecsv
或
添加 "m35/thecsv": "*"
<?php
use m35\thecsv\theCsv;
theCsv::export('tableName'); // return true if success
直接指定表名称,下载该表所有数据,自动生成表字段名称。
-
table
:数据表名称 (string
)
-
fields
:要导出的表字段 (array
)
-
exceptFields
:是否是排除字段模式,默认false(bool
)
-
header
:自定义表头(array
)
-
condition
:导出表条件(mixed
) 请参考http://www.yiiframework.com/doc-2.0/yii-db-query.html#where()-detail
-
limit
:限制数量(int
)
-
offset
:偏移(int
)
-
orderby
:排序(mixed
) 请参考http://www.yiiframework.com/doc-2.0/yii-db-querytrait.html#orderBy()-detail
-
name
:自定义文件名(string
)
-
sql
:自定义SQL语句(string
)
-
bind
:与sql结合绑定参数(array
)
-
target
:导出目录,如果设置target,则默认行为由下载变为保存文件到服务器(string
)
-
fp
:直接导出数据到指定的资源(resource
)
-
data
:自定义导出数据(array
)
-
query
:Yii2框架Query类型对象(yii\db\Query
) 请参考http://www.yiiframework.com/doc-2.0/yii-db-query.html
-
reader
:Yii2框架DataReader类型对象(yii\db\DataReader
) 请参考http://www.yiiframework.com/doc-2.0/yii-db-datareader.html
theCsv::export('user');
theCsv::export([
'table' => 'user',
'fields' => ['username', 'password'],
]);
theCsv::export([
'table' => 'user',
'fields' => ['status'],
'exceptFields' => true,
]);
theCsv::export([
'table' => 'user',
'fields' => ['username', 'password'],
'header' => ['账户', '密码'],
]);
theCsv::export([
'table' => 'user',
'fields' => ['username', 'password'],
'header' => 'no',
]);
theCsv::export([
'table' => 'user',
'condition' => ['status' => 1],
]);
condition请参考http://www.yiiframework.com/doc-2.0/yii-db-query.html#where()-detail
theCsv::export([
'table' => 'user',
'condition' => ['status' => 1],
'orderby' => 'id DESC',
'limit' => 10,
]);
theCsv::export([
'sql' => 'SELECT * FROM user',
]);
theCsv::export([
'sql' => 'SELECT * FROM user WHERE id = :id AND status = :status',
'bind' => [':id' => 1, ':status' => 1],
]);
theCsv::export([
'query' => (new \yii\db\Query)->from('user'),
]);
theCsv::export([
'reader' => \Yii::$app->getDb()->createCommand('SELECT * FROM user')->query(),
]);
theCsv::export([
'data' => [
['a', 'b', 'c'],
['A', 'B', 'C'],
],
]);
theCsv::export([
'data' => [
['a', 'b', 'c'],
['A', 'B', 'C'],
],
'name' => 'data.csv',
]);
theCsv::export([
'data' => [
['a', 'b', 'c'],
['A', 'B', 'C'],
],
'name' => 'data.csv', // 自定义导出文件名称
'target' => './', // 如果指定导出目录,则默认行为从下载变为保存到指定目录
]);
$fp = fopen('./data.csv', 'w');
theCsv::export([
'data' => [
['a', 'b', 'c'],
['A', 'B', 'C'],
],
'fp' => $fp, // 如果指定fp资源,则默认行为从下载变为直接写入该资源
]);