简体   繁体   中英

How to import data from excel to mysql database using php

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM