MacOS 10.15 Laravel框架 使用 Box/Spout 导入导出Excel

  • 准备工作
    • 环境条件
    • 安装Composer
    • 使用Composer安装 Box/Spout
  • 导入Excel
    • 官方描述
    • 个人分享
  • 导出Excel
    • 官方描述
    • 个人分享

准备工作

环境条件

根据官方文档显示,使用Box/Spout组件需要满足:

  • PHP version 7.1 or higher
  • PHP extension ext-zip enabled
  • PHP extension ext-xmlreader enabled

即PHP需要7.1及以上、开启zip拓展、开启xmlreader拓展

MacOS开启拓展的详细教程可点击此处查看。

安装Composer

安装前务必保证已经正确安装了PHP,MacOS已经自带PHP,目前最新版本为7.3.9

  • 打开终端并依次执行下列命令安装最新版本的 Composer:
php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
  • 执行后将会在当前目录下生成一个composer.phar的文件,我们需要全局安装composer,执行下列命令:
sudo mv composer.phar /usr/local/bin/composer
  • 现在就可以全局运行Composer了,但是由于众所周知的原因,我们需要使用Composer的中国镜像,在终端中执行:
composer config -g repo.packagist composer https://packagist.phpcomposer.com

使用Composer安装 Box/Spout

  • 在你的Laravel工程根目录下执行命令:
composer require box/spout

本操作需要提前开启zip拓展和xmlreader拓展,否则会安装失败

导入Excel

目前在网络上难以搜索到真正可以使用的最新版教程,所以我将会分为官方教程和我个人的代码来进行分享,以达到准确性和实用性

官方描述

Regardless of the file type, the interface to read a file is always the same:

use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;$reader = ReaderEntityFactory::createReaderFromFile('/path/to/file.ext');$reader->open($filePath);foreach ($reader->getSheetIterator() as $sheet) {foreach ($sheet->getRowIterator() as $row) {// do stuff with the row$cells = $row->getCells();...}
}$reader->close();

If there are multiple sheets in the file, the reader will read all of them sequentially.

Note that Spout guesses the reader type based on the file extension. If the extension is not standard (.csv, .ods, .xlsx - lower/uppercase), a specific reader can be created directly:

use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;$reader = ReaderEntityFactory::createXLSXReader();
// $reader = ReaderEntityFactory::createODSReader();
// $reader = ReaderEntityFactory::createCSVReader();

个人分享

  • 以一个简单的网络应用——上传Excel表格,返回表格内数据——为例,此块代码可以将所有sheet中的所有行全部输出,建议新手在学习时使用dd();函数调试,以便于清晰的掌握其中原理。
$filePath = "/path/".$File_name;
$reader = ReaderEntityFactory::createReaderFromFile($filePath);
$reader->open($filePath);
foreach ($reader->getSheetIterator() as $sheet) {foreach ($sheet->getRowIterator() as $RowKey => $row) {$cell = $row->getCells();foreach($cell as $CellKey => $value){$data[$RowKey][$CellKey] = $value->getValue();}}
}
$reader->close();
return ['code' => 200, 'data' => $data];

导出Excel

目前在网络上难以搜索到真正可以使用的最新版教程,所以我将会分为官方教程和我个人的代码来进行分享,以达到准确性和实用性

官方描述

As with the reader, there is one common interface to write data to a file:

use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Box\Spout\Common\Entity\Row;$writer = WriterEntityFactory::createXLSXWriter();
// $writer = WriterEntityFactory::createODSWriter();
// $writer = WriterEntityFactory::createCSVWriter();$writer->openToFile($filePath); // write data to a file or to a PHP stream
//$writer->openToBrowser($fileName); // stream data directly to the browser$cells = [WriterEntityFactory::createCell('Carl'),WriterEntityFactory::createCell('is'),WriterEntityFactory::createCell('great!'),
];/** add a row at a time */
$singleRow = WriterEntityFactory::createRow($cells);
$writer->addRow($singleRow);/** add multiple rows at a time */
$multipleRows = [WriterEntityFactory::createRow($cells),WriterEntityFactory::createRow($cells),
];
$writer->addRows($multipleRows); /** Shortcut: add a row from an array of values */
$values = ['Carl', 'is', 'great!'];
$rowFromValues = WriterEntityFactory::createRowFromArray($values);
$writer->addRow($rowFromValues);$writer->close();

Similar to the reader, if the file extension of the file to be written is not standard, specific writers can be created this way:

use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Box\Spout\Common\Entity\Row;$writer = WriterEntityFactory::createXLSXWriter();
// $writer = WriterEntityFactory::createODSWriter();
// $writer = WriterEntityFactory::createCSVWriter();

For XLSX and ODS files, the number of rows per sheet is limited to 1,048,576. By default, once this limit is reached, the writer will automatically create a new sheet and continue writing data into it.

个人分享

  • 以最简单的浏览器输出Excel为例,新版的Box/Spout不允许直接使用数组,必须转换为Cell,直接上代码,若有其他更加复杂的代码需求请参阅官方文档。
$cells = [WriterEntityFactory::createCell('序号'),WriterEntityFactory::createCell('姓名'),WriterEntityFactory::createCell('工号'),
];
$writer = WriterEntityFactory::createXLSXWriter()->openToBrowser('Example.xlsx');
$singleRow = WriterEntityFactory::createRow($cells);
$writer->addRow($singleRow);
$values = ['1', 'Mar0ew', 'G-19030001'];
$rowFromValues = WriterEntityFactory::createRowFromArray($values);
$writer->addRow($rowFromValues);$writer->close();

MacOS 10.15 Laravel框架 使用 Box/Spout 导入导出Excel相关推荐

  1. php之box/spout 导入导出功能

    官方文档Spout execl导入功能 /*** Common import file* @param $filePath string* @author : patti*/public static ...

  2. 【译】A quick list of new enterprise features in iOS 13, iPadOS, and macOS 10.15 Catalina

    苹果WWDC 2019主题演讲就在几个小时前结束,事实证明,正如我们希望的那样,企业有很多新闻,特别是关于身份管理和BYOD的新闻! 现在,这是我们的企业功能初步列表.可能会有更新-我们仍然必须等待完 ...

  3. 如何干净的删除vm_如何在macOS 10.15 Catalina绕过XProtect?

    在macOS 10.15 Catalina中,Apple进行了许多安全性能地改进,包括通过使所有可执行文件都受XProtect扫描来加固系统,而不管文件是否带有com.apple.quarantine ...

  4. Acer 4750 安装黑苹果_黑苹果 MacOS 10.15 Catalina 最新安装教程

    原创教程地址:黑苹果 MacOS 10.15 Catalina安装教程(店铺远程安装收集+定期维护更新) B站视频教程:https://www.bilibili.com/video/av5824875 ...

  5. 最详细AMD Ryzen CPU,VMware 15安装macOS 10.15.x Catalina 记录(第一篇)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 安装介绍 安装方式 成功案例 前期准备 工具准备 VMware Workstation 15. ...

  6. AMD CPU VMware 16 Pro安装macOS 10.15

    AMD CPU VMware 16 Pro安装macOS 10.15 原文参考https://www.calonye.com/24169.html 安装主要过程截图参考https://www.mac- ...

  7. MacOS 10.15 Catalina 更新系统后无法访问根目录下的/dashu/log

    MacOS 10.15 Catalina 更新系统后无法访问根目录下的/dashu/log..这个目录直接就没了..简直想哭 临时解决方案: 1.关闭SIP 2.终端下执行 sudo mount -u ...

  8. mac android工具下载,适用于Mac OS的安卓数据恢复软件Android Toolkit,支持macOS 10.15?...

    原标题:适用于Mac OS的安卓数据恢复软件Android Toolkit,支持macOS 10.15? 适用于Mac OS的安卓数据恢复软件你觉得哪款好用? 安卓数据恢复软件Android Tool ...

  9. 【黑苹果】微星MSI Mortar Z370M+i7 9700k+macOS 10.15.x efi文件下载

    主板:微星MSI Mortar Z370M 处理器:Intel Core i7 9700k 硬盘:PLEXTORPX M9peg 1t 显卡: Intel UHD Graphics 630 & ...

最新文章

  1. kill linux 信号量,kill用法详细解释(特别是信号量9的使用以及理解)
  2. c语言消字母游戏实验报告,C语言编程实验报告格式示例
  3. python中socket模块常用吗_python网络学习笔记——socket模块使用记录
  4. tomcat安装与项目部署
  5. .net runtime占用cpu_.net 中的StringBuilder和TextWriter区别
  6. java 签名 ecdsa_Java实现ECDSA签名算法
  7. There is 和 There are的使用_28
  8. tf 矩阵行和列交换_tf.transpose函数的用法讲解
  9. eclipse快捷键备忘
  10. 每日一题/014/三角函数/和差化积/求证:cos 2+cos 4+cos 6+...+cos 2n=sin ncos(n+1)/sin1
  11. 软件测试和初级会计哪个简单,2018年初级会计app刷题哪个好?
  12. python爬虫做毕业论文_基于Python的网络爬虫(智联招聘)开发与实现毕业论文+作品源码+演示视频...
  13. win10卓越性能模式开启方法
  14. 高德地图两点间距离计算函数
  15. iis下屏蔽php notice,apache、iis规则屏蔽拦截蜘蛛抓取
  16. 互联网软件开发和传统软件开发的不同(转型互联网开发需要适应的那些事)
  17. 最大公约数与最小公倍数( 初学Java 类与对象 )
  18. KEIL5 AC6 出现printf告警
  19. 开源软件负载均衡方案比较
  20. Html5-------合并单元格

热门文章

  1. html js页面加载前执行,Javascript代码在页面加载时的执行顺序介绍
  2. 192.168.0.1/27 表示什么
  3. 制片人、出品人、监制、特别出演分别指的是什么?
  4. Easy App Locker - 给你的 mac 应用加锁保护你的隐私
  5. WGCLOUD默认登录账号密码是什么
  6. trifecta_ui安装
  7. 2019上海ICPC H.Tree Partition
  8. 织梦插件-站长必备免费织梦插件快速收录
  9. Virgin Voyages将提供按需香槟配送服务“Shake for Champagne™”
  10. CentOS7 安装过程中的多盘分区功能探讨(二)