3

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:

   <?php    if (!defined ('BASEPATH')) exit ('No direct access allowed');
      class ExcelController extends CI_Controller
      {


      public function index()
      {
          //load library excel
          $this->load->library('excel');

          //Here i used microsoft excel 2007
          $objReader= PHPExcel_IOFactory::createReader('Excel2007');

          //Set to read only
          $objReader->setReadDataOnly(true);


          //Load excel file
          $objPHPExcel=$objReader->load('data.xls'); // error in this line
          $objWorksheet=$objPHPExcel->setActiveSheetIndex(0);

          //load model

          $this->load->model('user_model');

          //loop from first data untill 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:

 <?php
if (!defined ('BASEPATH')) exit ('No direct access allowed');
    class User_model extends CI_Controller
{
    public function __construct() {
        parent::__construct();
    }

    public function add_data($data_user)
    {
        $this->load->database();

        $this->db->insert('data',$data_user);
        return $this->db->insert_id();

    }
}

?>

Error in my code:

Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open data.xls for reading! File does not exist.' in C:\xampp\htdocs\ci_excel\application\third_party\PHPExcel\Reader\Excel2007.php:347 Stack trace: #0 C:\xampp\htdocs\ci_excel\application\controllers\excelcontroller.php(19): PHPExcel_Reader_Excel2007->load('data.xls') #1 [internal function]: ExcelController->index() #2 C:\xampp\htdocs\ci_excel\system\core\CodeIgniter.php(359): call_user_func_array(Array, Array) #3 C:\xampp\htdocs\ci_excel\index.php(202): require_once('C:\xampp\htdocs...') #4 {main} thrown in C:\xampp\htdocs\ci_excel\application\third_party\PHPExcel\Reader\Excel2007.php on line 347
2
  • Where is your file stored ? Can you provide you folder and file Structure ? Commented Mar 6, 2014 at 7:53
  • path is : application/third_party/Excel5/data.xlsx Commented Mar 6, 2014 at 7:56

2 Answers 2

2

Judging by the error message and your comment, it looks like you are using an incorrect filepath.

$objPHPExcel=$objReader->load('data.xls');

In CodeIgniter paths are relative to the entry script, usually index.php.

Use a relative file path to this location or alternatively an absolute path.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the reply. I am beginner. can you suggest me where i save my file?
Either move it to the same folder as the CodeIgniter entry script or reference the file path as Shanoop described ('./application/third_party/Excel5/data.xlsx')
i got error again. could i need some configuration??
0

I think you used incorrect path , change this

   $objPHPExcel=$objReader->load('data.xls'); // error in this line

To

   $objPHPExcel=$objReader->load('./application/third_party/Excel5/data.xlsx');

2 Comments

thank you!! but i am receive error Fatal error: Uncaught exception 'PHPExcel_Reader_Exception'....
@bhanu I not familiar with PHPEXcel check this link phpexcel.codeplex.com/discussions/61890

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.