【Php】PhpSpreadsheet安装的坑怎么这么多!

4 篇文章 0 订阅

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


人是代码非

最近在自己的服务器上面汇总一些工作中的常用的功能,有一个功能是想把数据库的一张表导出到excel里面,于是我把自己两三年前用过的PhpExcel库引入到我的Php代码中,结果发现已经用不起来了……

我还是我,但服务器的Php版本已经是PHP-7.4.33了,引入PhpExcel库一运行,结果报了一个这样的错误:“Deprecated: Array and string offset access syntax with curly braces is deprecated in /PHPExcel/Shared/String.php on line 526”,大致的意思是说Php7已经不支持PhpExcel里面用到的语法规则了,搜索了一下,说是PhpExcel已经不再维护了,现在有一款替代的叫“PhpSpreadsheet”,就准备用一下试试,结果发现光安装就好多坑,一言难尽。


提示:以下是本篇文章正文内容,下面案例可供参考

一、PhpSpreadsheet不安装直接用行不行?

答案:不行。我在github上面把PhpSpreadsheet代码下载下来后上传到服务器上面,然后在代码里面包含了几个需要用到的文件,像Spreadsheet.php,但运行时会报找不到Spreadsheet类的错误。后来查了一下,说是必须通过composer安装后才可以使用,于是开始了自己的踩坑之旅。

二、安装的坑

1.缺少fileinfo扩展

在服务器上面按照官方给的安装命令“composer require phpoffice/phpspreadsheet”,执行后报如下错误:

  Problem 1
    - Root composer.json requires phpoffice/phpspreadsheet ^1.29 -> satisfiable by phpoffice/phpspreadsheet[1.29.0].
    - phpoffice/phpspreadsheet 1.29.0 requires ext-fileinfo * -> it is missing from your system. Install or enable PHP's fileinfo extension.

没装的小伙伴按照我下面的截图装一下吧:
宝塔面板安装fileinfo扩展

2.下载中断

报如下错误:

  [Composer\Downloader\TransportException]                                                                                                                        
  curl error 7 while downloading https://repo.packagist.org/p2/phpoffice/phpspreadsheet.json: Failed to connect to repo.packagist.org port 443: No route to host 

查了一下这种错误是由于从国外下载,网络并不是十分稳定,遇到这种错误只能是重新运行安装命令,希望下次能够稳定下载。

3.proc_open函数被禁用

报如下错误:

  [Symfony\Component\Process\Exception\RuntimeException]                                   
  The Process class relies on proc_open, which is not available on your PHP installation.  

查了一下这种错误的意思是安装过程中要用到proc_open()这个函数,可以按照下图来解禁:
在禁用函数里面找到proc_open,然后点击删除即可完成解禁

4.composer.json没有权限打开

报如下错误:

  [ErrorException]                                                              
  file_put_contents(./composer.json): failed to open stream: Permission denied 

这种的就直接改文件权限为“777”解决吧。

5.composer.json内容要调整一下

报如下错误:

Root package 'phpoffice/phpspreadsheet' cannot require itself in its composer.json

删掉composer.json这一行
可以通过删除composer.json里面的这一行解决。

6.composer.lock权限问题

报如下错误:

  [ErrorException]                                                              
  file_put_contents(./composer.lock): failed to open stream: Permission denied

我找了半天composer.lock这个文件都没有找到,索性把整个PhpSpreadsheet-master目录里面的文件的权限都改成“777”了

7.最后成功安装完成的打印

都安装完后打印如下:

  - Installing sebastian/code-unit-reverse-lookup (2.0.3): Extracting archive
  - Installing phpunit/php-code-coverage (9.2.27): Extracting archive
  - Installing phar-io/version (3.2.1): Extracting archive
  - Installing phar-io/manifest (2.0.3): Extracting archive
  - Installing doctrine/instantiator (1.5.0): Extracting archive
  - Installing phpunit/phpunit (9.6.10): Extracting archive
  - Installing tecnickcom/tcpdf (6.6.2): Extracting archive
15 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
54 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
PHP CodeSniffer Config installed_paths set to ../../phpcompatibility/php-compatibility
[lighthouse@VM-24-11-centos PhpSpreadsheet-master]$ ls -l
total 356
-rwxrwxrwx  1 www        www        114347 Jul 29 20:59 CHANGELOG.md
-rwxrwxrwx  1 www        www          3804 Jul 29 21:19 composer.json
-rw-rw-r--  1 lighthouse lighthouse 197505 Jul 29 21:20 composer.lock
-rwxrwxrwx  1 www        www          3136 Jul 29 20:59 CONTRIBUTING.md
-rwxrwxrwx  1 www        www          1079 Jul 29 20:59 LICENSE
-rwxrwxrwx  1 www        www          1076 Jul 29 20:59 phpstan-baseline.neon
-rwxrwxrwx  1 www        www          4101 Jul 29 20:59 phpstan-conditional.php
-rwxrwxrwx  1 www        www          1387 Jul 29 20:59 phpstan.neon.dist
-rwxrwxrwx  1 www        www           617 Jul 29 20:59 phpunit10.xml.dist
-rwxrwxrwx  1 www        www          6667 Jul 29 20:59 README.md
drwxrwxrwx  3 www        www          4096 Jul 29 20:59 src
drwxrwxr-x 31 lighthouse lighthouse   4096 Jul 29 21:36 vendor
[lighthouse@VM-24-11-centos PhpSpreadsheet-master]$ 

新生成的vendor目录就是我们的安装成果,这个就和网上给的一些PhpSpreadsheet用法示例能对得上了。


总结

以上就是今天要讲的内容,本文仅仅简单介绍了PhpSpreadsheet安装时遇到的问题及解决方案,Php版本是PHP-7.4.33,服务器用的是宝塔面板-腾讯云专享版本,大家环境如果相似,可以借鉴一下,后续我会发一些PhpSpreadsheet的示例,相信这个库能做的事情比PhpExcel多一些。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
安装PhpSpreadsheet,可以使用Composer来进行安装。首先,你需要安装Composer。无论是在Windows还是Linux上,安装Composer后都可以执行安装命令。 使用Composer将PhpSpreadsheet安装到项目中,可以执行以下命令:composer require phpoffice/phpspreadsheet。强烈推荐使用Composer来安装PhpSpreadsheet。 值得注意的是,由于PHPExcel已经停止更新和维护,官方发布了新的版本PhpSpreadsheet作为其后续版本。PhpSpreadsheet的最低要求是PHP 7.2以上。你可以根据自己的实际情况来安装和使用PhpSpreadsheet。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [phpspreadsheet的composer安装,导出Excel表格](https://blog.csdn.net/u013252962/article/details/95192351)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [PHP7.2 安装使用PhpSpreadsheet(原PHPExcel)](https://blog.csdn.net/qq_15758827/article/details/107658056)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值