PHPExcel在Windows下出错 修复方案

环境

Steps

  1. 安装好PHPExcel,可以使用composer安装;

  2. 第一个错误是phpzip库没有找到,ZipArchive and lib-zip.dll
    PHPExcel 依赖 lib_zip库,需要在PHP的php.conf文件中打开。PHP5.3+默认有这个库,但是XAMPP TMD居然没有。要看有没有可以打开 XAMPP_ROOT/php/ext/ (in Windows) 文件夹看有没有 lib_zip.dll
    如果没有可以从 官网 下载,拖到 lib_zip.dll 上面的文件夹中。but,好像不行,需要重新编译PHP。

  3. PHPExcel内置的zip库 PCLZIP
    phpexcel 有一个 Pclzip 类,作为 lib_zip 缺失的情况下的备胎,具体可以看( from stackoverflow )
    需要在加载IOFactory方法前 添加一句PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);来切换到内置的zip库。

  4. 路径有问题 Classes/Shared/ZipArchive.php
    但是内置的 Classes/Shared/ZipArchive.php 类有个不兼容Windows路径的错误需要更正一下,在方法 public function getFromName($fileName)(141行)。
    找到的错误是传进来的 $fileName 包含的路径是这样的の xl/_rels/workbook.xml.rels(即使在windows下也是斜线模式,*nix)但是实际在 $list 数组中的却是Windows的反斜线风格 xl\_relsworkbook.xml.rels,斜线和反斜线不匹配,So 问题来了。
    需要在匹配的if判断中添加反斜线的匹配!!!!!!!!!!!!!

# before
if (strtolower($list[$i]["filename"]) == strtolower($fileName) || strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
# after
if (strtolower($list[$i]["filename"]) == strtolower($fileName) || str_replace("/", "u{5c}", strtolower($fileName)) == strtolower($list[$i]["filename"]) || strtolower($list[$i]["stored_filename"]) == strtolower($fileName) || str_replace("/", "u{5c}", strtolower($fileName)) == strtolower($list[$i]["stored_filename"])) {
# u{5c} is backslash

还会有一个问题就是 "**Variable contents is not defined", so 再小改一下

# before
        if ( is_array($extracted) && $extracted > 0 ) {
            $contents = $extracted[0]["content"];
        }
	return $contents;
#after
        $contents = "";
        if ( is_array($extracted) && $extracted > 0 ) {
            $contents = $extracted[0]["content"];
	    return $contents;
        }
	return false;

现在可以在PHP7(Windows)中使用了

原文地址:https://www.cnblogs.com/raybiolee/p/5956890.html