您的位置:首页 > 编程语言 > PHP开发

yii2.0中使用Goodby/csv插件,实现数据的导入导出

2016-09-29 16:04 721 查看
插件下载,功能介绍

Goodby/csv的功能

可以实现导入csv文件中的数据,导出数据到csv文件中

在yii2.0框架中引入Goodby/csv插件(两种方法)

第一种:在项目根目录下直接输入: composer require goodby/csv

第二种:在项目根目录下找到composer.json文件,在‘require‘项下加入 : "goodby/csv": "^1.3" ,然后在项目根目录下运行composer update

使用实例解析

1.导入csv文件

a.通过PDO向数据库导入数据

user.csv文件中的内容:

1,alice,alice@example.com
2,bob,bob@example.com
3,carol,carol@eample.com

use Goodby\CSV\Import\Standard\Lexer;
use Goodby\CSV\Import\Standard\Interpreter;
use Goodby\CSV\Import\Standard\LexerConfig;

$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'root');
$pdo->query('CREATE TABLE IF NOT EXISTS user (id INT, `name` VARCHAR(255), email VARCHAR(255))');

$config = new LexerConfig();
$lexer = new Lexer($config);

$interpreter = new Interpreter();

$interpreter->addObserver(function(array $columns) use ($pdo) {
$stmt = $pdo->prepare('INSERT INTO user (id, name, email) VALUES (?, ?, ?)');
$stmt->execute($columns);
});

$lexer->parse('user.csv', $interpreter); //user.csv中有数据,每一行作为一条记录写入数据库
2.导出数据到csv文件中

a.将数组中的值导出到csv文件中

use Goodby\CSV\Export\Standard\Exporter;
use Goodby\CSV\Export\Standard\ExporterConfig;

$config = new ExporterConfig();
$exporter = new Exporter($config);

$exporter->export('/tmp/abc.csv', array(
array('1', 'alice', 'alice@example.com'),
array('2', 'bob', 'bob@example.com'),
array('3', 'carol', 'carol@example.com'),
));//将数组中的值导入到/tmp/abc.csv文件中,文件不存在则创建

b.将数据库中的值通过PDO导出到csv文件中

use Goodby\CSV\Export\Standard\Exporter;

use Goodby\CSV\Export\Standard\ExporterConfig;
use Goodby\CSV\Export\Standard\CsvFileObject;
use Goodby\CSV\Export\Standard\Collection\PdoCollection;

$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'root');
/*******/
$pdo->query('CREATE TABLE IF NOT EXISTS user (id INT, `name` VARCHAR(255), email VARCHAR(255))');
$pdo->query("INSERT INTO user VALUES(1, 'alice', 'alice@example.com')");
$pdo->query("INSERT INTO user VALUES(2, 'bob', 'bob@example.com')");
$pdo->query("INSERT INTO user VALUES(3, 'carol', 'carol@example.com')");
//以上是创建实验用的数据表,并在数据表中填入数据
$exporter = new Exporter($config);

$stmt = $pdo->prepare("SELECT * FROM user");
$stmt->execute();

$exporter->export('/tmp/data.csv', new PdoCollection($stmt));//将数据库中的数据导入到/tmp/data.csv文件中
c.通过回调函数,将数组按要求处理后,导入到csv文件中

use Goodby\CSV\Export\Standard\Exporter;
use Goodby\CSV\Export\Standard\ExporterConfig;

use Goodby\CSV\Export\Standard\Collection\CallbackCollection;
/******/
$data = array();
$data[] = array('user', 'name1');
$data[] = array('user', 'name2');
$data[] = array('user', 'name3');
//以上为准备实验用的数组,此数组可以通过查询数据库得到
$collection = new CallbackCollection($data, function($row) {
// apply custom format to the row
$row[1] = $row[1] . '!';//在每条记录的索引为1的数据上,都连接一个感叹号

return $row;
});

$config = new ExporterConfig();
$exporter = new Exporter($config);

$exporter->export('/tmp/deal.csv', $collection);<span style="font-family: Arial, Helvetica, sans-serif;">//将数据库中的数据导入到/tmp/deal.csv文件中</span>


以上是goodby/csv常用的功能介绍,更多功能使用请移步:插件下载,功能介绍
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息