如下所示:
<?php define('BASE_URL', realpath(dirname(__FILE__))); require_once BASE_URL . '/PHPExcel/PHPExcel.php';//引入PHPExcel類文件 //excel文件的地址 $excel_fiel_path = './phpexcel.xls'; $PHPExcel = new PHPExcel();// 實例化PHPExcel工具類 //分析文件獲取後綴判斷是2007版本還是2003 $extend = pathinfo("./" . $excel_fiel_path); $extend = strtolower($extend["extension"]); // 判斷xlsx版本,如果是xlsx的就是2007版本的,否則就是2003 if ($extend=="xlsx") { $PHPReader = new PHPExcel_Reader_Excel2007(); $PHPExcel = $PHPReader->load("./" . $excel_fiel_path); }else{ $PHPReader = new PHPExcel_Reader_Excel5(); $PHPExcel = $PHPReader->load("./" . $excel_fiel_path); } /* 第二種方法*/ $objWorksheet = $PHPExcel->getActiveSheet(); $highestRow = $objWorksheet->getHighestRow(); echo 'highestRow='.$highestRow; echo "<br>"; $highestColumn = $objWorksheet->getHighestColumn(); $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//總列數 echo 'highestColumnIndex='.$highestColumnIndex; echo "<br>"; $headtitle=array(); for ($row = 2;$row <= $highestRow;$row++) { $strs=array(); //注意highestColumnIndex的列數索引從0開始 for ($col = 0;$col < $highestColumnIndex;$col++) { $strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue(); } //todo //連接mysql ,一條條寫入 }
以上就是小編為大家帶來的php通過PHPExcel導入Excel表格到MySQL數據庫的簡單實例全部內容了,希望大家多多支持~