首先:php
//包含excel的類庫
require APPPATH . 'third_party/PHPExcel.php';
require APPPATH . 'third_party/PHPExcel/Reader/Excel2007.php';
導入:數據庫
$excel_reader = new PHPExcel_Reader_Excel2007(); try { if (!$excel_reader->canRead($file_name)) { return $this->output("沒法讀取文件",1); } $obj = $excel_reader->load($file_name); // 讀取excel文件中的第一個工做表 $currentSheet = $obj->getSheet(0); // 列 $allColumn = $currentSheet->getHighestColumn(); // 行 $allRow = $currentSheet->getHighestRow(); $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($allColumn);//總列數 $headtitle =array(); for($cols =0 ;$cols<=$highestColumnIndex;$cols++){ $headtitle[$cols] =(string)$currentSheet->getCellByColumnAndRow($cols, 1)->getValue(); } if(empty($headtitle[0])){ for($cols =0 ;$cols<=$highestColumnIndex;$cols++){ $headtitle[$cols] =(string)$currentSheet->getCellByColumnAndRow($cols, 2)->getValue(); } } for($currentRow = 2;$currentRow <= $allRow;$currentRow++){ /**從第A列開始輸出*/ for($cols =0 ;$cols<=$highestColumnIndex;$cols++){ $data[$currentRow - 2][$cols] =(string)$currentSheet->getCellByColumnAndRow($cols, $currentRow)->getValue(); } } } catch (Exception $e) { @unlink($file_name); } //刪除臨時文件 @unlink($file_name);
再將導入數據保存到數據庫便可。ui