ronrademaker/
exporter
库,用于将各种输入格式的数据导出为各种输出格式。
0.1.0
2016-01-11 12:22 UTC
Requires
- league/csv: ^8.0
Requires (Dev)
- phpunit/phpunit: ^4.8
- satooshi/php-coveralls: ^1.0
This package is auto-updated.
Last update: 2024-08-29 04:09:09 UTC
README
库,用于将各种输入格式的数据导出为各种输出格式。
安装
composer require ronrademaker/exporter
用法
将数组输出到文件中的CSV
$someArray = [ ['foo' => 'foo', 'bar' => 'bar'], ['foo' => 'foobar', 'bar' => 'baz'], ['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], ['foo' => 'bar', 'baz' => 'bar'], ['foo' => 'baz', 'bar' => 'foobar'], ]; $data = new ArrayData($someArray); $fileOption = new FileOption($outputFile); $exporter = new CSVExporter(); $exporter->export($data, [$fileOption]);
或
$someArray = [ ['foo' => 'foo', 'bar' => 'bar'], ['foo' => 'foobar', 'bar' => 'baz'], ['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], ['foo' => 'bar', 'baz' => 'bar'], ['foo' => 'baz', 'bar' => 'foobar'], ]; $data = new ArrayData($someArray); $fileOption = new FileOption($outputFile); $exporter = new CSVExporter(); $exporter->setOption($fileOption); $exporter->setInput($data); $exporter->export();
添加标题以控制输出CSV
只有baz
和foo
将出现在结果CSV中,列的顺序将按照HeadersOption中的设置排列。
$someArray = [ ['foo' => 'foo', 'bar' => 'bar'], ['foo' => 'foobar', 'bar' => 'baz'], ['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], ['foo' => 'bar', 'baz' => 'bar'], ['foo' => 'baz', 'bar' => 'foobar'], ]; $outputFile = 'output.csv'; $headers = new HeadersOption(['baz', 'foo']); $data = new ArrayData($someArray); $fileOption = new FileOption($outputFile); $exporter = new CSVExporter(); $exporter->export($data, [$headersOption, $fileOption]);
输出到多个文件
$someArray = [ ['foo' => 'foo', 'bar' => 'bar'], ['foo' => 'foobar', 'bar' => 'baz'], ['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], ['foo' => 'bar', 'baz' => 'bar'], ['foo' => 'baz', 'bar' => 'foobar'], ]; $outputFile = 'output.csv'; $otherFile = sprintf('/data/dropbox/output-%s.csv', date('Ymd')); $headers = new HeadersOption(['baz', 'foo']); $data = new ArrayData($someArray); $options = [ $headers, new FileOption($outputFile), new FileOption($otherFile), ]; $exporter = new CSVExporter(); $exporter->export($data, $options);
错误处理
$someArray = [ ['foo' => 'foo', 'bar' => 'bar', ['impossible' => 'input']], ['foo' => 'foobar', 'bar' => 'baz'], ['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], ['foo' => 'bar', 'baz' => 'bar'], ['foo' => 'baz', 'bar' => 'foobar'], ]; $outputFile = 'output.csv'; $otherFile = sprintf('/data/dropbox/output-%s.csv', date('Ymd')); $headers = new HeadersOption(['baz', 'foo']); $data = new ArrayData($someArray); $options = [ $headers, new FileOption($outputFile), new FileOption($otherFile), ]; try { $exporter = new CSVExporter(); $exporter->export($data, $options); } catch (UnsupportedDataException $e) { echo sprintf('The given input is not valid: %s', $e->getMessage()); }
导出多个类似输入
$someArray = [ ['foo' => 'foo', 'bar' => 'bar'], ['foo' => 'foobar', 'bar' => 'baz'], ['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], ['foo' => 'bar', 'baz' => 'bar'], ['foo' => 'baz', 'bar' => 'foobar'], ]; $somePluralArray = [ ['foo' => 'foos', 'bar' => 'bars'], ['foo' => 'foobars', 'bar' => 'bazs'], ['foo' => 'foos', 'bar' => 'bars', 'baz' => 'bazs'], ['foo' => 'bars', 'baz' => 'bars'], ['foo' => 'bazs', 'bar' => 'foobars'], ]; $headers = new HeadersOption(['baz', 'foo']); $exporter = new CSVExporter([$headers]); $outputFileOption = new FileOption('output.csv'); $outputPluralFileOption = new FileOption('outputs.csv'); $inputData = new ArrayData($someArray); $inputPluralData = new ArrayData($somePlurarData); $exporter->setOption($outputFileOption, true); $exporter->export($inputData); $exporter->setOption($outputPluralFileOption, true); $exporter->export($inputPluralData);