0
votes

I'm trying to open an excel sheet and insert few records into it. But, it's not working. Here is my code

            $objReader = PHPExcel_IOFactory::createReader('Excel2007');
            //we load the file that we want to read
            $objPHPExcel = PHPExcel_IOFactory::load("/home/bigc/Desktop/test.xlsx");
            $objPHPExcel->setActiveSheetIndex(0);

            // Change the file
            $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A1', 'Hello')
                        ->setCellValue('B1', 'World!');

            // Write the file               
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            error_reporting(E_ALL);
            ini_set('display_errors', 1);
            $objWriter->save("/home/bigc/Documents/test.xlsx");

            //we save
            $objWriter->save();

The error which it throws is

Fatal error: Uncaught exception 'PHPExcel_Writer_Exception' with message 'Could not close zip file /home/bigc/Documents/test.xlsx.' in /home/bigc/Development/Development/Classes/PHPExcel/Writer/Excel2007.php:399 Stack trace: #0 /home/bigc/Development/Development/Upload.php(39): PHPExcel_Writer_Excel2007->save('/home/bigc/Docu...') #1 {main} thrown in /home/bigc/Development/Development/Classes/PHPExcel/Writer/Excel2007.php on line 399

I've already set read & write permissions. Any help would be greatly appreciated.

1
The only three causes for this error are invalid filepath, permissions, or that the file is already open/locked by a different process.... PHP safe_mode or base_dir can restrict access to a filepath with otherwise valid permissions, as can the suhosin patch - Mark Baker
Hi @MarkBaker, I've checked file permissions. I've set them to read and write, and the file is not already opened & when I click on the path from aptana studio, it takes me to the file. So, it means the file exists & moreover why does it show me "Could not close zip file"?? - Nizam Ali
I have absolutely no idea whatsoever, you've clearly found a fourth circumstance that can cause this problem; but unless that cause can be isolated then I can't do anything at all to help - Mark Baker
@MarkBaker, Thanks anyways. :) - Nizam Ali

1 Answers

0
votes

But that is the issue, you are trying to write over the top of the file on your desk top. It is not an issue with your code or phpexcel. It is about permissions. Try saving the file to a different location, then to the same location but with a different name. Is the current file read only by chance. Keep chasing the permission issue.