codeigniter - How to import data from excel to mysql database using php -
i using phpexcel import xlsx file related database. while running function getting error. code looks 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 used microsoft excel 2007 $objreader= phpexcel_iofactory::createreader('excel2007'); //set read $objreader->setreaddataonly(true); //load excel file $objphpexcel=$objreader->load('data.xls'); // error in line $objworksheet=$objphpexcel->setactivesheetindex(0); //load model $this->load->model('user_model'); //loop 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 code:
fatal error: uncaught exception 'phpexcel_reader_exception' message 'could not open data.xls reading! file 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 error message , comment, looks using incorrect filepath.
$objphpexcel=$objreader->load('data.xls');
in codeigniter paths relative entry script, index.php.
use relative file path location or alternatively absolute path.
Comments
Post a Comment