克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Excel | 数据表格处理库

介绍

基于OpenSpout封装的轻量级高性能表格处理类库,仅占用3M内存,即可轻松实现上百万行数据的高效导入与导出。

你还在为 PhpSpreadsheet 封装的类库占用内存过大,无法满足业务需求时,而苦恼吗? 那么,Xin-Excel 就是你的救星,它基于OpenSpout封装的轻量级高性能表格处理类库,仅占用3M内存,即可轻松实现上百万行数据的高效导入与导出。 从此你就可以轻松实现Excel表格数据的导出与导入,轻松解决Excel表格数据导出与导入的问题,专注于业务逻辑,不再为表格数据导出与导入而烦恼。

安装教程

composer require xin/excel

使用说明

<?php

use Xin\Excel\Column;
use Xin\Excel\Dataset;
use Xin\Excel\Export;
use Xin\Excel\Import;

require __DIR__ . '/vendor/autoload.php';

echo "初始化内存: " . (memory_get_usage() / 1024 / 1024) . " MB\n";

$excelFilepath = 'aa.xlsx';

//======================数据导出====================================
$dataset = new Dataset(new MakeBigData(), [
	Column::integer('id', 'ID'),
	Column::title('goods_title', '商品名称'),
	Column::integer('goods_num', '数量')->setWidth(10),
	Column::price('goods_price', '单价'),
	Column::RMB('total_price', '合计'),
	Column::custom(function ($row) {
		return "{$row['master_order']['user_nickname']}(用户ID:{$row['master_order']['user_id']})";
	}, '买家信息')->setWidth(20),
	Column::create('master_order.receiver_name', '收货人姓名')->setWidth(15),
	Column::phone('master_order.receiver_phone', '收货人手机号'),
	Column::custom(function ($row) {
		return "{$row['master_order']['receiver_province']}{$row['master_order']['receiver_city']}{$row['master_order']['receiver_city']}{$row['master_order']['receiver_address']}";
	}, '收货人地址'),
	Column::create('master_order.pay_type_text', '支付方式')->setWidth(10),
	Column::create('master_order.order_no', '订单编号'),
	Column::create('master_order.order_status_text', '订单状态'),
	Column::datetime('create_time', '下单时间'),
], '订单数据');

// 快速导出数据
Export::data(new MakeBigData(), $dataset->columns(), $excelFilepath, '订单数据');

// 快速导出多个数据源
$dataset2 = clone $dataset;
$dataset2->setTitle('订单数据2');
Export::make([$dataset])->addDataset($dataset2)->save($excelFilepath);

//======================数据导入====================================

// 列映射,支持索引和首行标题两种方式进行映射
$columnsMapping = [
	'id' => 'ID',
	'goods_time' => 1,
	'create_time' => '下单时间',
];

// 加载单个工作表
$data = Import::load(
	$excelFilepath, // 加载的文件
	$columnsMapping, // 列的映射关系
	0, // 读取的起始工作表索引
	[
		'max_count' => 10,
	], // 读取的参数
);
var_dump($data);

// 加载多个工作表
$data = Import::loadMultiple(
	$excelFilepath, // 加载的文件
	[
		$columnsMapping,
		$columnsMapping,
	],// 每个工作表列的映射关系
	[0, 2], // 要读取的工作表索引或名称
	[
		0 => [
			'max_count' => 100,
		],
	],// 读取的参数
);
var_dump($data);

// 遍历工作表数据
Import::make($excelFilepath)->each(
	0,
	function ($row) {
		var_dump($row);
	},
	$columnsMapping,
	[
		'max_count' => 100,
	]
);

Import::make($excelFilepath)->chunk(
	0,
	function ($rows) {
		var_dump($rows);
	},
	$columnsMapping,
	10,
	[
		'max_count' => 100,
	]
);

// 遍历所有工作表数据
Import::make($excelFilepath)->eachAll(
	function ($row, $count) {
		var_dump($row, $count);
		return $count < 50;
	},
	[$columnsMapping],
	[],
	[
		[Import::OPTION_MAX_COUNT => 100],
	]
);

// 使用分块遍历所有工作表数据
Import::make($excelFilepath)->chunkAll(
	function ($row) {
		var_dump($row);
	},
	[$columnsMapping],
	[],
	1000,
	[
		[Import::OPTION_MAX_COUNT => 1000],
	]
);

echo "处理后内存: " . (memory_get_usage() / 1024 / 1024) . " MB\n";
echo "内存峰值: " . (memory_get_peak_usage() / 1024 / 1024) . " MB\n";

class MakeBigData implements \Iterator
{
	/**
	 * @var int
	 */
	protected $index = 0;

	/**
	 * @var int
	 */
	protected $max = 10000;

	const content = <<<TEXT
《追日者与移山人》
在古老的华夏大地上,有两个相邻的部落。一个部落居住在巍峨的太行山脚下,另一个部落则生活在广袤的平原上。太行山部落的首领是一位名叫愚公的长者,而平原部落的首领则是一位名叫追日的勇士。
愚公年事已高,但精神矍铄。他所在的部落被太行山和王屋山阻隔,交通不便,生活困苦。愚公深知,这两座大山是部落发展的最大障碍。于是,他召集族人,提出了一个大胆的计划——移山。族人们虽然对愚公的计划感到震惊,但都被他的决心所打动,纷纷响应。他们用简陋的工具,开始挖掘山石,将土石运往渤海之滨。尽管困难重重,但愚公坚信,只要子孙后代坚持不懈,终有一天会移走这两座大山。
与此同时,平原部落的追日是一位力大无穷的勇士。他的部落曾因十个太阳同时出现而陷入灾难,庄稼枯萎,土地干裂,百姓流离失所。后来,追日的祖先后羿射落九个太阳,拯救了部落。追日从小就听着后羿的故事长大,他渴望像祖先一样,为部落做出伟大的贡献。
有一天,追日得知愚公移山的壮举,深受感动。他决定前往太行山,帮助愚公的部落。当他来到愚公的部落时,看到愚公和族人们正在辛勤地劳作,尽管疲惫不堪,但无人退缩。追日被他们的精神所打动,决定加入他们。
追日的加入为移山的工程带来了巨大的帮助。他力大无穷,能轻松地举起巨石,还能用神力驱散山间的迷雾。在他的帮助下,工程进展得更快了。然而,移山并非易事,愚公的部落面临着诸多困难。智叟曾嘲笑愚公不自量力,但愚公毫不理会,依然坚持自己的信念。
随着时间的推移,愚公和追日的友谊也日益深厚。他们互相鼓励,共同面对困难。愚公告诉追日,移山的意义不仅在于改变地理环境,更在于传承一种不屈不挠的精神。追日则用他的勇气和力量,激励着族人们继续前行。
终于,在愚公和追日的共同努力下,太行山和王屋山被逐渐移平。虽然工程尚未完全结束,但道路已不再阻塞,部落的生活也逐渐改善。愚公的子孙们继承了他的遗志,继续挖掘,而追日也成为了部落的英雄。
故事传遍了大地,人们被愚公的坚持和追日的勇气所感动。他们明白,无论面对多么巨大的困难,只要心中有信念,坚持不懈,就一定能创造出奇迹。正如夸父追日时所展现的勇气和决心,虽然他最终倒在了追日的途中,但他留下的精神却激励了后人。
TEXT;

	/**
	 * @param int $max
	 */
	public function __construct(int $max = 10000)
	{
		$this->max = $max;
	}


	public function current(): mixed
	{
		return [
			'id' => $this->index + 1,
			'goods_title' => '商品名称',
			'goods_num' => random_int(0, 9999),
			'content' => self::content,
			'goods_price' => floatval(random_int(0, 99999) . "." . random_int(0, 99)),
			'create_time' => date('Y-m-d H:i:s'),
			'master_order' => [
				'user_id' => '1',
				'user_nickname' => 'xxxx',
				'receiver_phone' => '13653975075',
				'receiver_province' => '',
				'receiver_city' => '',
				'receiver_address' => '',
			],
		];
	}

	public function next(): void
	{
		$this->index++;
	}

	public function key(): mixed
	{
		return $this->index;
	}

	public function valid(): bool
	{
		return $this->index < $this->max;
	}

	public function rewind(): void
	{
		$this->index = 0;
	}
}
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [2022] [jinyangzi] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

基于OpenSpout封装的轻量级高性能表格处理类库,仅占用3M内存,即可轻松实现上百万行数据的高效导入与导出。 你还在为 PhpSpreadsheet 封装的类库占用内存过大,无法满足业务需求时,而苦恼吗? 那么,Xin-Excel 就是你的救星,它基于OpenSpout封装的轻量级高性能表格处理类库,仅占用3M内存,即可轻松实现上百万行数据的高效导入与导出。 从此你就可以轻松实现Excel表 展开 收起
PHP
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化