11

PHPを使用してExcelファイルを読み取る方法を知りたいです。私の特定のユースケースは、 Yii内からPHPExcelを使用することです。

私は多くのチュートリアルを実行しましたが、常に1つのポイントで立ち往生しています:「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:\ 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

2007形式を明示的に使用するようにPHPExcelを設定したようですが、XLSファイルを開こうとしています。100%確信はありませんが、zipエラーはXLSファイルを解凍しようとしているためだと推測します。zipされていないため失敗します。

The php zip extension appears to be working, as the error is from the extension - Invalid or unitialized Zip object. My guess is that you're getting an invalid Zip object since you're not dealing with a zip file.

If you're trying to open an XLS file, you probably want:

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

Alternatively, you could remove the explicit mode, and just rely upon the automatic file type resolution, e.g.:

$objPHPExcel = PHPExcel_IOFactory::load("c:\cctv.xls");  // Remove the createReader line before this
于 2013-01-09T21:32:51.270 に答える
5

I had the same error message, but turned out to be an issue with file permissions (as suggested here: PHPExcel Warning: ZipArchive::getFromName(): Invalid or unitialized Zip object in).

A quick chmod 644 on the Excel file fixed it.

于 2015-04-22T14:23:10.440 に答える