当尝试通过PHP读取excel文件时,为什么会出现“无效或单元化的Zip对象”错误?

时间:2013-01-09 06:18:37

标签: phpexcel

我想了解如何使用PHP 读取 excel文件。我的具体用例是使用Yii中的PHPExcel

我已经遵循了很多教程而且我总是陷入困境:“ZipArchive :: getFromName():无效或整体化的Zip对象”。我已经添加了扩展,加载器等,但似乎没有任何工作。有没有办法解决?还是我需要另一个图书馆?这是我控制器中的代码。

Yii::import('application.vendors.PHPExcel.PHPExcel',true);
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load('c:\cctv.xls'); //$file --> your filepath and filename
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); // e.g. 10
$highestColumn = $objWorksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // e.g. 5
echo '<table>' . "\n";
for ($row = 2; $row <= $highestRow; ++$row) {
  echo '<tr>' . "\n";
  for ($col = 0; $col <= $highestColumnIndex; ++$col) {
    echo '<td>' . $objWorksheet->getCellByColumnAndRow($col, $row)->getValue() . '</td>' . "\n";
  }
  echo '</tr>' . "\n";
}
echo '</table>' . "\n";

这是详细的错误:

C:\瓦帕\ WWW \示例\保护\厂商\ PHPExcel \ PHPExcel \读卡器\ Excel2007.php(272)

}
 public function _getFromZipArchive(ZipArchive $archive, $fileName = '')
 {
     // Root-relative paths
     if (strpos($fileName, '//') !== false)
     {
         $fileName = substr($fileName, strpos($fileName, '//') + 1);
     }
     $fileName = PHPExcel_Shared_File::realpath($fileName);

     // Apache POI fixes
     $contents = $archive->getFromName($fileName);
     if ($contents === false)
     {
         $contents = $archive->getFromName(substr($fileName, 1));
     }

     /*
     if (strpos($contents, '<?xml') !== false && strpos($contents, '<?xml') !== 0)
     {
         $contents = substr($contents, strpos($contents, '<?xml'));
     }
     var_dump($fileName);
     var_dump($contents);

堆栈跟踪C:\ wamp \ www \ trunk \ protected \ vendors \ PHPExcel \ PHPExcel \ Reader \ Excel2007.php(272):ZipArchive-&gt; getFromName(“_ rels / .rels”)

$fileName = substr($fileName, strpos($fileName, '//') + 1);
}
$fileName = PHPExcel_Shared_File::realpath($fileName);
// Apache POI fixes
$contents = $archive->getFromName($fileName);
if ($contents === false)
{
    $contents = $archive->getFromName(substr($fileName, 1));
}

C:\ wamp \ www \ example \ protected \ vendors \ PHPExcel \ PHPExcel \ Reader \ Excel2007.php(312):PHPExcel_Reader_Excel2007-&gt; _getFromZipArchive(ZipArchive,“_rels / .rels”)

$excel->removeCellXfByIndex(0); // remove the default style
     }
     $zip = new ZipArchive;
     $zip->open($pFilename);

     $rels = simplexml_load_string($this->_getFromZipArchive($zip, "_rels/.rels")); //~http://schemas.openxmlformats.org/package/2006/relationships");
     foreach ($rels->Relationship as $rel) {
         switch ($rel["Type"]) {
             case "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties":
                 $xmlCore = simplexml_load_string($this->_getFromZipArchive($zip, "{$rel['Target']}"));
                 if (is_object($xmlCore)) {

C:\ wamp \ www \ example \ protected \ controllers \ AdminController.php(58):PHPExcel_Reader_Excel2007-&gt; load(“c:\ cctv.xls”)

public function actionCreateSource() {
Yii::import('application.vendors.PHPExcel.PHPExcel',true);
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load('c:\cctv.xls'); //$file --> your filepath and filename
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); // e.g. 10
$highestColumn = $objWorksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // e.g. 5
echo '<table>' . "\n";

2 个答案:

答案 0 :(得分:34)

看起来您将PHPExcel设置为显式使用2007格式,但您尝试打开XLS文件。虽然我不是百分百肯定,但我猜这个zip错误是因为它试图解压缩XLS文件,而且它会因为没有压缩而失败。

php zip扩展似乎正在工作,因为错误来自扩展程序 - 无效或单元化的Zip对象。我的猜测是你得到一个无效的Zip对象,因为你没有处理一个zip文件。

如果您尝试打开XLS文件,可能需要:

$objReader = PHPExcel_IOFactory::createReader('Excel5');

或者,您可以删除显式模式,只需依赖自动文件类型解析,例如:

$objPHPExcel = PHPExcel_IOFactory::load("c:\cctv.xls");  // Remove the createReader line before this

答案 1 :(得分:5)

我收到了相同的错误消息,但结果是文件权限问题(如此处所示:PHPExcel Warning: ZipArchive::getFromName(): Invalid or unitialized Zip object in)。

Excel文件上的快速chmod 644修复了它。