2016-11-16 91 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