项目作者: jqhph

项目描述 :
🚀 快速读写Excel文件,简单高效
高级语言: PHP
项目地址: git://github.com/jqhph/easy-excel.git
创建时间: 2019-10-17T08:14:49Z
项目社区:https://github.com/jqhph/easy-excel

开源协议:MIT License

下载




# EASY EXCEL




StyleCI






Easy Excel是一个基于 box/spout 封装的Excel读写工具,可以帮助开发者更快速更轻松地读写Excel文件,
并且无论读取多大的文件只需占用极少的内存。

由于box/spout只支持读写xlsxcsvods等类型文件,所以本项目目前也仅支持读写这三种类型的文件。

文档

文档

环境

  • PHP >= 7.1
  • PHP extension php_zip
  • PHP extension php_xmlreader
  • box/spout >= 3.0
  • league/flysystem >= 1.0

安装

  1. composer require dcat/easy-excel

快速开始

导出

下载

  1. use Dcat\EasyExcel\Excel;
  2. $array = [
  3. ['id' => 1, 'name' => 'Brakus', 'email' => 'treutel@eg.com', 'created_at' => '...'],
  4. ...
  5. ];
  6. $headings = ['id' => 'ID', 'name' => '名称', 'email' => '邮箱'];
  7. // xlsx
  8. Excel::export($array)->headings($headings)->download('users.xlsx');
  9. // csv
  10. Excel::export($array)->headings($headings)->download('users.csv');
  11. // ods
  12. Excel::export($array)->headings($headings)->download('users.ods');

保存

  1. use Dcat\EasyExcel\Excel;
  2. use League\Flysystem\Adapter\Local;
  3. use League\Flysystem\Filesystem;
  4. $array = [...];
  5. // 保存到当前服务器
  6. Excel::export($array)->store('/tmp/users.xlsx');
  7. // 使用 filesystem
  8. $adapter = new Local(__DIR__);
  9. $filesystem = new Filesystem($adapter);
  10. Excel::export($array)->disk($filesystem)->store('users.xlsx');

获取文件内容

  1. use Dcat\EasyExcel\Excel;
  2. $array = [...];
  3. $xlsxContents = Excel::xlsx($array)->raw();
  4. $csvContents = Excel::csv($array)->raw();
  5. $odsContents = Excel::ods($array)->raw();

更多导出功能请参考文档

导入

读取所有表格数据

  1. use Dcat\EasyExcel\Excel;
  2. use League\Flysystem\Adapter\Local;
  3. use League\Flysystem\Filesystem;
  4. $headings = ['id', 'name', 'email'];
  5. // 导入xlsx
  6. $allSheets = Excel::import('/tmp/users.xlsx')->headings($headings)->toArray();
  7. // 使用filesystem
  8. $adapter = new Local(__DIR__);
  9. $filesystem = new Filesystem($adapter);
  10. $allSheets = Excel::import('users.xlsx')->disk($filesystem)->headings($headings)->toArray();
  11. print_r($allSheets); // ['Sheet1' => [['id' => 1, 'name' => 'Brakus', 'email' => 'treutel@eg.com', 'created_at' => '...']]]

遍历表格

  1. use Dcat\EasyExcel\Excel;
  2. use Dcat\EasyExcel\Contracts\Sheet as SheetInterface;
  3. use Dcat\EasyExcel\Support\SheetCollection;
  4. // 导入xlsx
  5. Excel::import('/tmp/users.xlsx')->each(function (SheetInterface $sheet) {
  6. // 获取表格名称,如果是csv文件,则此方法返回空字符串
  7. $sheetName = $sheet->getName();
  8. // 表格序号,从 0 开始
  9. $sheetIndex = $sheet->getIndex();
  10. // 是否是最后一次保存前打开的表格
  11. $isActive = $sheet->isActive();
  12. // 分块处理表格数据
  13. $sheet->chunk(100, function (SheetCollection $collection) {
  14. $chunkArray = $collection->toArray();
  15. print_r($chunkArray); // [['id' => 1, 'name' => 'Brakus', 'email' => 'treutel@eg.com', 'created_at' => '...']]
  16. });
  17. });

获取指定表格内容

  1. use Dcat\EasyExcel\Excel;
  2. use Dcat\EasyExcel\Support\SheetCollection;
  3. // 获取第一个表格内容
  4. $firstSheet = Excel::import('/tmp/users.xlsx')->first()->toArray();
  5. // 获取最后一次保存前打开的表格内容
  6. $activeSheet = Excel::import('/tmp/users.xlsx')->active()->toArray();
  7. // 获取指定名称或序号的表格内容
  8. $sheet = Excel::import('/tmp/users.xlsx')->sheet('Sheet1')->toArray();
  9. $sheet = Excel::import('/tmp/users.xlsx')->sheet(0)->toArray();
  10. // 分块处理表格内容
  11. Excel::import('/tmp/users.xlsx')
  12. ->first()
  13. ->chunk(1000, function (SheetCollection $collection) {
  14. $collection = $collection->keyBy('id');
  15. });

更多导入功能请参考文档

License

The MIT License (MIT).