0

I am uploading an excel file from a folder in my computer to a folder in the server, after the upload i am loading the uploaded file so that i can protect certain cell, the first method i used below does not work at all

  function LockCertainCells(){
         $labref=  $this->uri->segment(3);
          $objReader = new PHPExcel_Reader_Excel2007();
          $path = "analyst_uploads/" . date('Y') . '/' . date('M') . '/'. $labref .'/'. $labref . ".xlsx";
          $objPHPExcel = $objReader->load($path);
          $objPHPExcel->setActiveSheetIndexbyName('Sample Summary');
          $objPHPExcel->getActiveSheet()->protectCells('A17:G85','PHPExcel');
          $objPHPExcel ->getActiveSheet()->getProtection()->setSheet(true);                         
      }

This second one

  function LockCertainCells(){
     $labref=  $this->uri->segment(3);
      $objPHPExcel = new PHPExcel;
      $path = "analyst_uploads/" . date('Y') . '/' . date('M') . '/'. $labref .'/'. $labref . ".xlsx";
      $objSheet = $objPHPExcel->load($path);
      $objSheet->setActiveSheetIndexbyName('Sample Summary');
      $objSheet->protectCells('A17:G85', 'PHP');
      $objSheet->getProtection()->setSheet(true);                        

}

Throws me this error:

Fatal error: Call to undefined method PHPExcel::load() in C:\127.0.0.1\htdocs\NQCL\.....

suggestions!

alphy
  • 931
  • 4
  • 13
  • 23

1 Answers1

0

The PHPExcel class doesn't have a load method, which is precisely why you get that error.... the first method is the method provided by the PHPExcel library for loading a file into a PHPExcel object, the second is not. Use the method that works (and is the method described in all the documentation); not the one that doesn't exist.

The first method for locking the cells isn't doing anything with the PHPExcel object (such as saving it) after you have set cell protection; so as soon as the LockCertainCells function terminates, it will be out of scope and discarded by the PHP script. If you want to make the changes in the file, you need to save the file again. I believe that MS Excel itself requires you to save a file if you want to make any changes to that file permanent.

EDIT

You also miss setting the password for the protected cells:

$objSheet->getProtection()->setPassword('mypassword');
Mark Baker
  • 209,507
  • 32
  • 346
  • 385