我正在使用 PHPExcel 将 XLSX 文件导入我的相关数据库。但是在运行该功能时,我收到了错误。我的代码如下所示。
控制器:
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);
}
}
型号:
function add_data($datauser)
{
$this->db->insert('data',$datauser);
return $this->db->insert_id();
}
错误 ::
致命错误:未捕获的异常 'PHPExcel_Reader_Exception' 带有消息“无法打开 a.xlsx 进行阅读!文件不存在。' 在 C:\xampp\htdocs\htdocs\htdocs\school_erp\school\school\application\libraries\PHPExcel\Reader\Excel2007.php:343 堆栈跟踪:#0 C:\xampp\htdocs\htdocs\htdocs\school_erp\school \school\application\controllers\trial_c.php(32): PHPExcel_Reader_Excel2007->load('a.xls') #1 [内部函数]: 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} 在 C:\xampp\htdocs\htdocs\htdocs\school_erp\school\school\application\libraries\PHPExcel\Reader\Excel2007.php 中抛出第 343 行