定制 bluehe/yii2-phpexcel 二次开发

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

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

bluehe/yii2-phpexcel

Composer 安装命令:

composer require bluehe/yii2-phpexcel

包简介

Exporting PHP to Excel or Importing Excel to PHP

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License

Exporting PHP to Excel or Importing Excel to PHP. Excel Widget for generate Excel File or for load Excel File.

Property

string $mode is an export mode or import mode. valid value are 'export' and 'import'

boolean $isMultipleSheet for set the export excel with multiple sheet.

array $properties for set property on the excel object.

array $models Model object or DataProvider object with much data.

array $columns to get the attributes from the model, this valid value only the exist attribute on the model. If this is not set, then all attribute of the model will be set as columns.

array $headers to set the header column on first line. Set this if want to custom header. If not set, the header will get attributes label of model attributes.

string|array $fileName is a name for file name to export or import. Multiple file name only use for import mode, not work if you use the export mode.

string $savePath is a directory to save the file or you can blank this to set the file as attachment.

string $format for excel to export. Valid value are 'Excel5', 'Excel2007', 'Excel2003XML', '00Calc', 'Gnumeric'.

array $style for all excel.

boolean|array $headerTitle to set the header title row on the fitst line.

boolean|array $firstTitle to set the first title row on the fitst line.

boolean $setFirstTitle to set the title column on the first line. The columns will have a header on the first line.

boolean $asAttachment to set the file excel to download mode.

boolean $setFirstRecordAsKeys to set the first record on excel file to a keys of array per line. If you want to set the keys of record column with first record, if it not set, the header with use the alphabet column on excel.

boolean $setIndexSheetByName to set the sheet index by sheet name or array result if the sheet not only one

string $getOnlySheet is a sheet name to getting the data. This is only get the sheet with same name.

array|Formatter $formatter the formatter used to format model attribute values into displayable texts. This can be either an instance of [[Formatter]] or an configuration array for creating the [[Formatter]] instance. If this property is not set, the "formatter" application component will be used.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist bluehe/yii2-phpexcel "*"

or add

"bluehe/yii2-phpexcel": "*"

to the require section of your composer.json file.

Usage

Exporting Data

Exporting data into an excel file.

<?php

// export data only one worksheet.

\bluehe\phpexcel\Excel::widget([
	'models' => $allModels,
	'mode' => 'export', //default value as 'export'
	'columns' => ['column1','column2','column3'], //without header working, because the header will be get label from attribute label.
	'header' => ['column1' => 'Header Column 1','column2' => 'Header Column 2', 'column3' => 'Header Column 3'],
]);

\bluehe\phpexcel\Excel::export([
	'models' => $allModels,
	'columns' => ['column1','column2','column3'], //without header working, because the header will be get label from attribute label.
	'header' => ['column1' => 'Header Column 1','column2' => 'Header Column 2', 'column3' => 'Header Column 3'],
]);

// export data with multiple worksheet.

\bluehe\phpexcel\Excel::widget([
	'isMultipleSheet' => true,
	'models' => [
		'sheet1' => $allModels1,
		'sheet2' => $allModels2,
		'sheet3' => $allModels3
	],
	'mode' => 'export', //default value as 'export'
	'columns' => [
		'sheet1' => ['column1','column2','column3'],
		'sheet2' => ['column1','column2','column3'],
		'sheet3' => ['column1','column2','column3']
	],
	//without header working, because the header will be get label from attribute label.
	'header' => [
		'sheet1' => ['column1' => 'Header Column 1','column2' => 'Header Column 2', 'column3' => 'Header Column 3'],
		'sheet2' => ['column1' => 'Header Column 1','column2' => 'Header Column 2', 'column3' => 'Header Column 3'],
		'sheet3' => ['column1' => 'Header Column 1','column2' => 'Header Column 2', 'column3' => 'Header Column 3']
	],
]);

\bluehe\phpexcel\Excel::export([
	'isMultipleSheet' => true,
	'models' => [
		'sheet1' => $allModels1,
		'sheet2' => $allModels2,
		'sheet3' => $allModels3
	], 'columns' => [
		'sheet1' => ['column1','column2','column3'],
		'sheet2' => ['column1','column2','column3'],
		'sheet3' => ['column1','column2','column3']
	],
	//without header working, because the header will be get label from attribute label.
	'header' => [
		'sheet1' => ['column1' => 'Header Column 1','column2' => 'Header Column 2', 'column3' => 'Header Column 3'],
		'sheet2' => ['column1' => 'Header Column 1','column2' => 'Header Column 2', 'column3' => 'Header Column 3'],
		'sheet3' => ['column1' => 'Header Column 1','column2' => 'Header Column 2', 'column3' => 'Header Column 3']
	],
]);

New Feature for exporting data, you can use this if you familiar yii gridview. That is same with gridview data column. Columns in array mode valid params are 'attribute', 'header', 'format', 'value', and footer (TODO). Columns in string mode valid layout are 'attribute:format:header:footer(TODO)'.

<?php

\bluehe\phpexcel\Excel::export([
   	'models' => Post::find()->all(),
      	'columns' => [
      		'author.name:text:Author Name',
      		[
      				'attribute' => 'content',
      				'header' => 'Content Post',
      				'format' => 'text',
      				'value' => function($model) {
      					return ExampleClass::removeText('example', $model->content);
      				},
      		],
      		'like_it:text:Reader like this content',
      		'created_at:datetime',
      		[
      				'attribute' => 'updated_at',
      				'format' => 'date',
      		],
      	],
      	'headers' => [
     		'created_at' => 'Date Created Content',
		],
]);

Importing Data

Import file excel and return into an array.

<?php

$data = \bluehe\phpexcel\Excel::import($fileName, $config); // $config is an optional

$data = \bluehe\phpexcel\Excel::widget([
		'mode' => 'import',
		'fileName' => $fileName,
		'setFirstRecordAsKeys' => true, // if you want to set the keys of record column with first record, if it not set, the header with use the alphabet column on excel.
		'setIndexSheetByName' => true, // set this if your excel data with multiple worksheet, the index of array will be set with the sheet name. If this not set, the index will use numeric.
		'getOnlySheet' => 'sheet1', // you can set this property if you want to get the specified sheet from the excel data with multiple worksheet.
	]);

$data = \bluehe\phpexcel\Excel::import($fileName, [
		'setFirstRecordAsKeys' => true, // if you want to set the keys of record column with first record, if it not set, the header with use the alphabet column on excel.
		'setIndexSheetByName' => true, // set this if your excel data with multiple worksheet, the index of array will be set with the sheet name. If this not set, the index will use numeric.
		'getOnlySheet' => 'sheet1', // you can set this property if you want to get the specified sheet from the excel data with multiple worksheet.
	]);

// import data with multiple file.

$data = \bluehe\phpexcel\Excel::widget([
	'mode' => 'import',
	'fileName' => [
		'file1' => $fileName1,
		'file2' => $fileName2,
		'file3' => $fileName3,
	],
		'setFirstRecordAsKeys' => true, // if you want to set the keys of record column with first record, if it not set, the header with use the alphabet column on excel.
		'setIndexSheetByName' => true, // set this if your excel data with multiple worksheet, the index of array will be set with the sheet name. If this not set, the index will use numeric.
		'getOnlySheet' => 'sheet1', // you can set this property if you want to get the specified sheet from the excel data with multiple worksheet.
	]);

$data = \bluehe\phpexcel\Excel::import([
	'file1' => $fileName1,
	'file2' => $fileName2,
	'file3' => $fileName3,
	], [
		'setFirstRecordAsKeys' => true, // if you want to set the keys of record column with first record, if it not set, the header with use the alphabet column on excel.
		'setIndexSheetByName' => true, // set this if your excel data with multiple worksheet, the index of array will be set with the sheet name. If this not set, the index will use numeric.
		'getOnlySheet' => 'sheet1', // you can set this property if you want to get the specified sheet from the excel data with multiple worksheet.
	]);

Result example from the code on the top :


// only one sheet or specified sheet.
Array([0] => Array([name] => Anam, [email] => moh.khoirul.anaam@gmail.com, [framework interest] => Yii2), [1] => Array([name] => Example, [email] => example@moonlandsoft.com, [framework interest] => Yii2));

// data with multiple worksheet
Array([Sheet1] => Array([0] => Array([name] => Anam, [email] => moh.khoirul.anaam@gmail.com, [framework interest] => Yii2), [1] => Array([name] => Example, [email] => example@moonlandsoft.com, [framework interest] => Yii2)), [Sheet2] => Array([0] => Array([name] => Anam, [email] => moh.khoirul.anaam@gmail.com, [framework interest] => Yii2), [1] => Array([name] => Example, [email] => example@moonlandsoft.com, [framework interest] => Yii2)));

// data with multiple file and specified sheet or only one worksheet
Array([file1] => Array([0] => Array([name] => Anam, [email] => moh.khoirul.anaam@gmail.com, [framework interest] => Yii2), [1] => Array([name] => Example, [email] => example@moonlandsoft.com, [framework interest] => Yii2)), [file2] => Array([0] => Array([name] => Anam, [email] => moh.khoirul.anaam@gmail.com, [framework interest] => Yii2), [1] => Array([name] => Example, [email] => example@moonlandsoft.com, [framework interest] => Yii2)));

// data with multiple file and multiple worksheet
Array([file1] => Array([Sheet1] => Array([0] => Array([name] => Anam, [email] => moh.khoirul.anaam@gmail.com, [framework interest] => Yii2), [1] => Array([name] => Example, [email] => example@moonlandsoft.com, [framework interest] => Yii2)), [Sheet2] => Array([0] => Array([name] => Anam, [email] => moh.khoirul.anaam@gmail.com, [framework interest] => Yii2), [1] => Array([name] => Example, [email] => example@moonlandsoft.com, [framework interest] => Yii2))), [file2] => Array([Sheet1] => Array([0] => Array([name] => Anam, [email] => moh.khoirul.anaam@gmail.com, [framework interest] => Yii2), [1] => Array([name] => Example, [email] => example@moonlandsoft.com, [framework interest] => Yii2)), [Sheet2] => Array([0] => Array([name] => Anam, [email] => moh.khoirul.anaam@gmail.com, [framework interest] => Yii2), [1] => Array([name] => Example, [email] => example@moonlandsoft.com, [framework interest] => Yii2))));

TODO

  • Adding footer params for columns in exporting data.

bluehe/yii2-phpexcel 适用场景与选型建议

bluehe/yii2-phpexcel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 150 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 05 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「excel」 「xls」 「import」 「extension」 「export」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 bluehe/yii2-phpexcel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bluehe/yii2-phpexcel 我们能提供哪些服务?
定制开发 / 二次开发

基于 bluehe/yii2-phpexcel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-05-04