1
我正在一個項目中,我需要將數據從Excel(.xlsx
)文件
導入到數據庫中的表中。
我已經寫代碼的控制器upload.php
PHPExcel Codeigniter:致命錯誤:未捕獲的異常'PHPExcel_Reader_Exception'
$this->load->library('excel');
ini_set('memory_limit', '-1');
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$path = $_SERVER['DOCUMENT_ROOT'] . '/bitcoinreports/uploads/data.xlsx';
$objPHPExcel=$objReader->load($path);
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
//loop from first data until last data
for($i=2; $i<=77; $i++){
$name = $objWorksheet->getCellByColumnAndRow(0,$i)->getValue();
$email = $objWorksheet->getCellByColumnAndRow(1,$i)->getValue();
$phone = $objWorksheet->getCellByColumnAndRow(2,$i)->getValue();
$details = $objWorksheet->getCellByColumnAndRow(3,$i)->getValue();
$location = $objWorksheet->getCellByColumnAndRow(4,$i)->getValue();
$data_user = array(
"name" => $name,
"email" => $email ,
"phone" => $phone ,
"details" => $details ,
"location" => $location ,
);
$this->dashboard_model->add_data($data_user);
}
每當我把這個控制器我有以下錯誤
Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open C:/xampp/htdocs/bitcoinreports/uploads/data.xlsx for reading! File does not exist.' in C:\xampp\htdocs\bitcoinreports\application\third_party\PHPExcel\Reader\Excel2007.php:347
Stack trace:
#0 C:\xampp\htdocs\bitcoinreports\application\controllers\admin\upload.php(40): PHPExcel_Reader_Excel2007->load('C:/xampp/htdocs...')
#1 [internal function]: Upload->index()
#2 C:\xampp\htdocs\bitcoinreports\system\core\CodeIgniter.php(359): call_user_func_array(Array, Array)
#3 C:\xampp\htdocs\bitcoinreports\index.php(202): require_once('C:\\xampp\\htdocs...')
#4 {main}
thrown in C:\xampp\htdocs\bitcoinreports\application\third_party\PHPExcel\Reader\Excel2007.php on line 347
如何解決這個問題呢?
如此清晰的錯誤..你的文件數據。 xlsx不存在於您設置的路徑或其不可訪問的路徑中。兩者都是poss因爲我看到它在你的窗戶C裏面:.. – Svetoslav