定制 ronrademaker/exporter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ronrademaker/exporter

Composer 安装命令:

composer require ronrademaker/exporter

包简介

Library to export data in various input formats to various output formats.

README 文档

README

Library to export data in various input formats to various output formats.

Coverage Status Build Status SensioLabsInsight

Installation

composer require ronrademaker/exporter

Usage

Output an array to CSV in a file

$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]);

or

$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();

Add headers to control output CSV

Only baz and foo will be in the resulting CSV and the order of the columns will be as set in the 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]);

Output to multiple files

$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);

Error Handling

$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());
}

Exporting multiple, similar input

$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);

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-01-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固