php通过PHPExcel导入Excel表格到MySQL数据库的简单实例
如下所示:
<?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 ,一条条写入 }
相关推荐
yifangs 2020-10-13
zyyjay 2020-11-09
xuebingnan 2020-11-05
samtrue 2020-11-22
stefan0 2020-11-22
songshijiazuaa 2020-09-24
hebiwtc 2020-09-18
天步 2020-09-17
83911535 2020-11-13
whatsyourname 2020-11-13
zhouyuqi 2020-11-10
Noneyes 2020-11-10
mathchao 2020-10-28
王志龙 2020-10-28
wwwsurfphpseocom 2020-10-28
diskingchuan 2020-10-23
savorTheFlavor 2020-10-23