提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
人是代码非
最近在自己的服务器上面汇总一些工作中的常用的功能,有一个功能是想把数据库的一张表导出到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.
没装的小伙伴按照我下面的截图装一下吧:
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()这个函数,可以按照下图来解禁:
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里面的这一行解决。
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多一些。