2016-11-16 33 views
-2

嗨,我想獲取excel工作表的記錄並存儲在我的數據庫中。我成功完全添加了Excel的第一張表格,但無法獲得第二張表格及其記錄。
我正在使用這段代碼,並得到第一張`$ this - > load - > library('Excel');如何計算並訪問codeigniter中的Excel工作表?

$objPHPExcel = PHPExcel_IOFactory::load($load_file); 
$cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection(); 

`
在圖像假設其工作表3在一個Excel中。我可以訪問第二個和第三個工作表嗎?請給我一些幫助。謝謝

how to count excel worksheets in PHP codeigniter

回答

1

嘗試使用PHPExcel閱讀您的Excel文件。

function read_excel(){ 
     //Load library plugin 
     $this->load->library('excel'); 

     /** Error reporting */ 
     error_reporting(E_ALL); 
     ini_set('display_errors', TRUE); 
     ini_set('display_startup_errors', TRUE); 
     define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />'); 

     //Open excel file 
     $objPHPExcel = PHPExcel_IOFactory::load(FCPATH."file.xls"); 

     // go through your worksheet if you have multiple worksheet inside the excel file 
     foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) { 
      //read each row of the worksheet 
      foreach ($worksheet->getRowIterator() as $row) { 
       //get cells 
       $cellIterator = $row->getCellIterator(); 
       $cellIterator->setIterateOnlyExistingCells(false); 
       foreach ($cellIterator as $cell) { 
        //get cell values 
        $cell->getCalculatedValue(); 
       } 
      } 
     } 
} 

在您的需要調整此代碼。

+0

感謝您的回覆。我已經在使用這個。我正在尋找如何訪問我的第二個和第三個工作表?
我使用這個代碼'$ this - > load - > library('Excel'); \t $ objPHPExcel = PHPExcel_IOFactory :: load($ load_file); \t $ cell_collection = $ objPHPExcel-> getActiveSheet() - > getCellCollection(); ' –

+0

@ user3548569試試上面的示例函數我在我的程序中使用它讀取excel文件中的多個工作表。並感謝您的反饋:P –

+0

感謝您的回覆。但我得到我自己的答案,它的超級快,我也在我的答案中顯示。感謝你的支持 :) –

1

當我探索PHPExcel.php我得到的解決方案,我將與其他人分享。

$target_file = $path . basename($_FILES["files"]["name"]);// storing the excel file to folder 
$file_id  = basename($_FILES["files"]["name"]); 
$FileType = pathinfo($target_file, PATHINFO_EXTENSION); 

move_uploaded_file($_FILES["files"]["tmp_name"], $target_file); 

$load_file = $target_file; 

$update4  = array('file_id' => $file_id,); 

$this -> load -> library('Excel'); 

$objPHPExcel = PHPExcel_IOFactory::load($load_file); 

$sheet_count = $objPHPExcel->getSheetCount();// this function give me worksheets count. 
for($s=0; $s<$sheet_count; $s++){// using for lop to get specific worksheet data 
    $cell_collection= $objPHPExcel->getSheet($s)->getCellCollection(); 

    foreach ($cell_collection as $cell) { 
     $column  = $objPHPExcel -> getSheet() -> getCell($cell) -> getColumn(); 
     $row  = $objPHPExcel -> getSheet() -> getCell($cell) -> getRow(); 
     $data_value = $objPHPExcel -> getSheet() -> getCell($cell) -> getValue(); 

     if ($row == 1) {// if you have header in your excel or want to store then store in header. 
      $header[$s][$row][$column] = $data_value; 
     } else {// i get my meaningful data from here :) 
      $arr_data[$s][$row][$column] = $data_value; 
     } 
    } 
}// end of for loop for getting the work sheet