2015-11-16 135 views
1

我正在一個項目中,我需要將數據從Excel(.xlsx)文件
導入到數據庫中的表中。
我已經寫代碼的控制器upload.phpPHPExcel 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 

如何解決這個問題呢?

+2

如此清晰的錯誤..你的文件數據。 xlsx不存在於您設置的路徑或其不可訪問的路徑中。兩者都是poss因爲我看到它在你的窗戶C裏面:.. – Svetoslav

回答

0

在打開文件之前,檢查它是否存在,並且運行PHP的進程可讀(在這種情況下是xampp,不知道用戶是如何在xampp中命名的,也許是apache)。

http://php.net/manual/en/function.is-readable.php

is_readable - 判斷給一個文件是否存在,是否可讀

所以替換此:

$objPHPExcel=$objReader->load($path); 

if (is_readable($path)){ 
    $objPHPExcel=$objReader->load($path); 
} else { 
    // handle problem, for example 
    echo "cannot read $path"; 
    exit; 
} 
相關問題