PHPExcel Zip扩展错误 - 无法生成Excel文件

时间:2016-03-08 17:24:41

标签: php phpexcel

概述

我试图直接下载使用PHPExcel创建的Excel电子表格。我没有服务器级访问权限,因此我无法安装或启用mod(例如Zip模块)。

数据是活动的嘉宾列表。

代码

<?php
if(isset($_GET["event_id"])&&
    !empty($_GET["event_id"])){

    //Include PHPExcel, Excel2007, classes
    require_once("inc/PHPExcel/PHPExcel.php");
    require_once("inc/PHPExcel/PHPExcel/Writer/Excel2007.php");
    require_once("inc/classes.php");

    //Zip not installed - change settings to use local compression
    PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);

    //Get event data
    $event_id = intval($_GET["event_id"]);
    $event = new Event($event_id);
    $guests = $event->getGuests();

    //Create new PHPExcel object
    $spreadsheet = new PHPExcel();

    //Add data
    $spreadsheet->setActiveSheetIndex(0);
    $spreadsheet->getActiveSheet()->SetCellValue("B2", "TMC Gateway");
    $spreadsheet->getActiveSheet()->SetCellValue("B3", "Event register");

    $spreadsheet->getActiveSheet()->SetCellValue("B5", "Name");
    $spreadsheet->getActiveSheet()->SetCellValue("C5", "Member/Guest");
    $spreadsheet->getActiveSheet()->SetCellValue("D5", "Checkin Time");

    foreach($guests as $guest){
        if($guest["degree"]=="guest"){
            $arr[] = [$guest["name1"]." ".$guest["name2"], "Guest", $guest["checkintime"]];
        } else {
            $arr[] = [trim($guest["name2"]), "Member", $guest["checkintime"]];
        }
    }

    $currentCell = 6;

    foreach($arr as $a){
        $spreadsheet->getActiveSheet()->SetCellValue("B$currentCell",$a[0]);
        $spreadsheet->getActiveSheet()->SetCellValue("C$currentCell",$a[1]);
        $spreadsheet->getActiveSheet()->SetCellValue("D$currentCell",$a[2]);
        $currentCell++;
    }

    //Rename sheet
    $spreadsheet->getActiveSheet()->setTitle("TMC Gateway");

    //Open writer
    $writer = new PHPExcel_Writer_Excel2007($spreadsheet);

    //Set headers and force download
    header("Content-type: application/vnd.ms-excel");
    header("Content-Disposition: attachment;filename=\"TMC_Gateway_Attendees-".$event_id.".xls\"");
    $writer->save("php://output");

    //Kill script
    exit;
}

问题

最初处理并打开文件时,我看到了这个错误:

  

致命错误:Class&#39; ZipArchive&#39; /home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Writer/Excel2007.php 227

行中找不到

我意识到这可能是因为Zip模块未安装或未启用,所以我在Class 'ZipArchive' not found error while using PHPExcel处遵循了这些说明:

  

如果您没有为PHP安装/启用ZipArchive,并且无法自行启用,那么您可以使用

PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);

但是,现在打开文件时会出现此错误:

  

致命错误:未捕获的异常&#39; PHPExcel_Writer_Exception&#39;消息&#39;错误压缩文件:PCLZIP_ERR_READ_OPEN_FAIL(-2):无法打开临时文件&#39; /tmppclzip-56df08ee0384c.tmp'在二进制写入模式&#39;在/home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Shared/ZipArchive.php:108
  堆栈跟踪:
  #0 /home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Writer/Excel2007.php(278):PHPExcel_Shared_ZipArchive-&gt; addFromString(&#39; _rels / .rels&#39;,&#39;&lt; ?xml version =&#34; ...&#39;)
  #1 /home/loqui/public_html/doorapp/xls.php(66):PHPExcel_Writer_Excel2007-&gt;保存(&#39; php://输出&#39;)
  #2 {main}
    抛出 /home/loqui/public_html/doorapp/inc/PHPExcel/PHPExcel/Shared/ZipArchive.php 108

问题

由于我没有启用Zip模块,并且工作文件夹中的权限看似有限,如何让此脚本下载正确创建的Excel文件?

1 个答案:

答案 0 :(得分:0)

如果您要继续使用PCLZIP,我建议您检查它尝试写入的tmp目录,并查看该目录分配给哪个用户。 Apache很可能没有对该tmp目录的写访问权,因此无法在其中写入文件。我正在努力解决ZipArchive前端的类似问题,但是如果你有足够的权限chown -R user:user foldername可能会减轻你的写入问题。