yii2 export .csv format table

1. Add     "m35/thecsv": "*" to the require item of composer.json

Then execute     php require m35/thecsv



2. Parameter list

( 1 ) string type: directly specify the table name, download all the data of the table, and automatically generate the table field name.

( 2 ) array type:

table : data table name ( string )

fields : table fields to export ( array )

exceptFields : whether it is an exclude field pattern, default false ( bool )

header : custom header ( array )

condition : export table condition ( mixed ) please refer to http://www.yiiframework.com/doc-2.0/yii-db-query.html#where () -detail

limit : limit amount ( int )

offset : offset ( int )

orderby : sort ( mixed ) please refer to http://www.yiiframework.com/doc-2.0/yii-db-querytrait.html#orderBy () -detail

name : custom file name ( string )

sql : custom SQL statement ( string )

bind : bind parameters ( array ) in combination with sql

target : export directory, if target is set , the default behavior changes from downloading to saving files to the server ( string )

fp : Export data directly to the specified resource ( resource )

data : custom export data ( array )

query : Yii2 framework Query type object ( yii\db\Query ) please refer to http://www.yiiframework.com/doc-2.0/yii-db-query.html

reader : Yii2 framework DataReader type object ( yii\db\DataReader ) please refer to http://www.yiiframework.com/doc-2.0/yii-db-datareader.html

                       


3. Example of use

( 1 ) string type, the subsequent use is not written out, only the first one is written, the same below

use m35\thecsv\theCsv;
theCsv::export('tableName');

( 2 ) array type

Export data table complete data

theCsv::export('user');
Export the username and password of the user table

theCsv::export([
    'table' => 'user',
    'fields' => ['username', 'password'],
]);
Export all data in the user table except the status field

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


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


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

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

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', // custom export file name
    'target' => './', // If you specify an export directory, the default behavior changes from downloading to saving to the specified directory
]);

$fp = fopen('./data.csv', 'w');
theCsv::export([
    'data' => [
        ['a', 'b', 'c'],
        ['A', 'B', 'C'],
    ],
    'fp' => $fp, // If an fp resource is specified, the default behavior changes from downloading to writing directly to the resource
]);


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324862446&siteId=291194637