charmer / yii2-dataexporter
将 Yii2 数据提供者模型导出到文件
dev-main
2023-01-14 12:43 UTC
Requires
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2024-09-14 16:29:32 UTC
README
使用批处理迭代和临时文件将 Yii2 数据提供者模型导出到文件,以减少内存使用
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
php composer.phar require --prefer-dist charmer/yii2-dataexporter "*"
或者
"charmer/yii2-dataexporter": "*"
将以下内容添加到您的 composer.json
文件的 require 部分。
使用
示例 Web 控制器及其返回下载文件的 action
<?php namespace app\controllers; use app\models\Clients; use charmer\dataexporter\Exporter; use charmer\dataexporter\Writers\CsvWriter; use yii\web\Controller; use yii\data\ActiveDataProvider; class TestController extends Controller { public function actionTest() { //Create Exporter object $exporter = new Exporter(); //Create temp file $exporter->initTmpFile(); //Create writer for CSV file format $writer = new CsvWriter(); //Headers for exporting columns, ['model attribute' => 'Label'] //you can simply use Model::attributeLabels() if you want to export all fields //or just type only fields you need for export $headers = [ 'id' => '№', 'first_name' => 'First Name', 'last_name' => 'Last Name', 'email' => 'E-Mail', 'created_at' => 'Created at' ]; //Set fields for export $writer->setFields($headers); //Set writer to exporter $exporter->setWriter($writer); //Create DataProvider for export $provider = new ActiveDataProvider([ 'query' => Clients::find(), 'pagination' => [ 'pageSize' => 5, ], ]); //Set created data provider to exporter $exporter->setDataProvider($provider); //Prepare for export $exporter->prepare(); //Export $exporter->export(); //Get temp file path $path = $exporter->getTmpFileMetaData()['uri']; //return file for download return Yii::$app->response->sendFile($path, 'export.csv'); } }
使用不同的文件类型写入器
此包中有 3 个默认写入器: CSV、JSON 和 XML。要更改导出文件格式,只需创建所需的写入器对象即可。
$csvWriter = new CsvWriter(); $jsonWriter = new JsonWriter(); $xmlWriter = new XmlWriter();
其他操作对于两种写入器都是相同的。
创建自己的写入器
要创建自己的写入器,您需要从 charmer\dataexporter\Writers\BaseWriter
扩展您的自定义写入器类,并实现 write(array $data)
方法。
<?php namespace charmer\dataexporter\Writers; class TsvWriter extends BaseWriter { public function write(array $data) { $this->exportedCount++; $row = implode('\t', $data) fputs($this->tmpFile, $row."\n"); } }
您可以使用 BaseWriter
类的属性
mixed $tmpFile
- 导出数据的临时文件int $exportedCount
- 当前导出项目的数量int $totalCount
- 导出项目的总数(DataProvider::getTotalCount() 的值)
您可以使用 BaseWriter
类的方法
public Writer::initWriter()
- 如果此方法存在于您的写入器中,它将在Writer::write()
调用之前被调用closeWriter()
- 如果此方法存在于您的写入器中,它将在Writer::write()
调用之后被调用public function writeHeader()
- 写入导出字段的头部行(例如,用于 CSV 导出)
JsonWriter
的示例
<?php namespace charmer\dataexporter\Writers; class JsonWriter extends BaseWriter { //Writes opening array symbol "[" in the beginning of the JSON file public function initWriter() { fputs($this->tmpFile, '['); } //Writes closing array symbol "]" in the end of the JSON file public function closeWriter() { fputs($this->tmpFile, ']'); } //Writes JSON-serialized data to file public function write(array $data) { fputs($this->tmpFile, json_encode($data)); $this->exportedCount++; if ($this->exportedCount < $this->totalCount) { fputs($this->tmpFile, ','); } } }
您可以使用其他写入器类作为使用其他 Writer
方法属性的示例