11

I would like to find out how to read an excel file using via PHP. My specific use case is using PHPExcel from within Yii.

I have followed numerous tutorials and I am always stuck at one point: "ZipArchive::getFromName(): Invalid or unitialized Zip object". I have added the extensions, loader, etc. but nothing seems to be working. is there any way around this? or do I need to get another library? Here is the code in my controller.

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";

this is the detailed error:

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);

Stack Trace 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";
robsch
  • 9,358
  • 9
  • 63
  • 104
rodrigue
  • 125
  • 1
  • 1
  • 6
  • Please show the whole error message what you get – Sergey Jan 09 '13 at 06:24
  • You need the PHP zip extension enabled in your PHP: this is a prerequisite for handling zipped files such as .xlsx – Mark Baker Jan 09 '13 at 07:26
  • 2
    I have already enabled the zip extension Mark – rodrigue Jan 09 '13 at 07:38
  • ZipArchive is the standard class for the zip extension: if it was enabled, then PHP should be able to load it succesfully.... have you rebooted the server since you enabled it? Have you checked phpinfo to see that it is enabled? – Mark Baker Jan 09 '13 at 09:16
  • check some libraries suggested here too http://stackoverflow.com/a/5806036/724913 – arkoak Jan 09 '13 at 09:56
  • I did restart and check the php info Mark, everything was correct and the zip extension was enabled. – rodrigue Jan 09 '13 at 10:23
  • arkoak, I will check the other libraries – rodrigue Jan 09 '13 at 10:24
  • Then I really can't understand why PHP can't find the ZipArchive class.... it's a standard PHP class, not a PHPExcel class.... and it's something that needs to be there for any of the PHP libraries that work with .xlsx files – Mark Baker Jan 09 '13 at 10:49
  • @MarkBaker I don't think the error is that it can't find the class. Looks like he's trying to open an XLS file, which is not a zip format, and then getting errors about that . . . – ernie Jan 09 '13 at 21:35
  • 1
    @ernie - true enough, I missed that. A simple load() call would have picked the correct reader, or using identify() would have identified which reader to instantiate - all I'd seen was the explicit instantiation of the Excel2007 Reader... +1 for reading the question without being blinkered – Mark Baker Jan 09 '13 at 21:39
  • note: the spelling mistake in the error was fixed the same day as the question (Jan 9, 2013): https://github.com/php/php-src/commit/c3acefd9ea9f1c0b0900f75d05c14a1e6ab2b671 – Cœur Sep 04 '17 at 12:41

2 Answers2

39

It looks like you set PHPExcel to explicitly use the 2007 format, but you're trying to open an XLS file. While I'm not 100% sure, I'm going to guess the zip error is because it's trying to unzip the XLS file, and that's going to fail as it's not zipped.

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
ernie
  • 6,356
  • 23
  • 28
  • 1
    I suspect that I probably need to put in some error handling for this: throw an exception if $zip->open fails – Mark Baker Jan 09 '13 at 21:47
  • WOW, thanks a million ernie!!!!!!! I actually just replaced Excel2007 with Excel5, how could I not see this :(, thank you very much for pointing that out. – rodrigue Jan 10 '13 at 06:23
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.

Community
  • 1
  • 1
Paddy Mann
  • 1,169
  • 12
  • 18