11

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

我学习了许多教程,但我总是被困在一个点:“ZipArchive::getFromName(): Invalid or unialized Zip object”。我添加了扩展程序、加载程序等,但似乎没有任何效果。有没有办法解决?还是我需要另一个图书馆?这是我的控制器中的代码。

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:\wamp\www\example\protected\vendors\PHPExcel\PHPExcel\Reader\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->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->_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->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";
4

2 回答 2

39

看起来您将 PHPExcel 设置为明确使用 2007 格式,但您正在尝试打开 XLS 文件。虽然我不能 100% 确定,但我猜测 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
于 2013-01-09T21:32:51.270 回答
5

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

Excel文件快速chmod 644修复它。

于 2015-04-22T14:23:10.440 回答