2
votes

I am using PHPExcel to import a XLSX file to my related database. But while running the function I am getting the error. My code looks like shown below.

Controller:

public function readExcel()
    {
      //load library phpExcel
      $this->load->library("PHPExcel");

      //here i used microsoft excel 2007
      $objReader = PHPExcel_IOFactory::createReader('Excel2007');
      //set to read only
      $objReader->setReadDataOnly(true);
      //load excel file
      $objPHPExcel = $objReader->load("a.xls");
      $objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
      //load model
      $this->load->model("User_model");
      //loop from first data until last data
      for($i=2; $i<=77; $i++)
      {
        $name = $objWorksheet->getCellByColumnAndRow(0,$i)->getValue();
        $address = $objWorksheet->getCellByColumnAndRow(1,$i)->getValue();
        $data_user = array(
        "name" => $name,
        "username" => $address );
        $this->User_model->add_data($data_user);
      }
}

Model:

function add_data($datauser)
{
  $this->db->insert('data',$datauser);
  return $this->db->insert_id();
} 

error ::

Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open a.xlsx for reading! File does not exist.' in C:\xampp\htdocs\htdocs\htdocs\school_erp\school\school\application\libraries\PHPExcel\Reader\Excel2007.php:343 Stack trace: #0 C:\xampp\htdocs\htdocs\htdocs\school_erp\school\school\application\controllers\trial_c.php(32): PHPExcel_Reader_Excel2007->load('a.xls') #1 [internal function]: Trial_c->readExcel() #2 C:\xampp\htdocs\htdocs\htdocs\school_erp\school\school\system\core\CodeIgniter.php(359): call_user_func_array(Array, Array) #3 C:\xampp\htdocs\htdocs\htdocs\school_erp\school\school\index.php(202): require_once('C:\xampp\htdocs...') #4 {main} thrown in C:\xampp\htdocs\htdocs\htdocs\school_erp\school\school\application\libraries\PHPExcel\Reader\Excel2007.php on line 343

1
my magic crystal ball says your error is caused by.... Aliens. - Marc B
Error please? it would help - Drewdin
i am updating my question with an error - user2325244
Well the message is pretty self-explanatory - either the file isn't in the directory you think, or you don't have permissions to access it from your running PHP script - Mark Baker
Load using the Excel2007 Reader, save using the HTML Writer - Mark Baker

1 Answers

2
votes

Use an absolute path to the xml file:

 /var/www/html/mysite/a.xsl

or

C:\inthepubs\mysite\a.xsl