我正在使用PHPExcel將XLSX文件導入到我的相關數據庫。但是,當運行該功能時,我收到錯誤。我的代碼如下所示。如何使用php將數據從excel導入到mysql數據庫
控制器:
<?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);
}
}
}
?>
型號:
<?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();
}
}
?>
錯誤在我的代碼:
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
在哪裏存儲你的文件? 你能爲你提供文件夾和文件結構嗎? – Sanoob
路徑爲:application/third_party/Excel5/data.xlsx – bhanu