php通过PHPExcel导入Excel表格到MySQL数据库(转载)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
$PHPExcel = new PHPExcel();// 实例化PHPExcel工具类 //分析文件获取后缀判断是2007版本还是2003 $extend = pathinfo("./" . $_POST['files']); $extend = strtolower($extend["extension"]); // 判断xlsx版本,如果是xlsx的就是2007版本的,否则就是2003 if ($extend=="xlsx") { $PHPReader = new PHPExcel_Reader_Excel2007(); $PHPExcel = $PHPReader->load("./" . $_POST['files']); }else{ $PHPReader = new PHPExcel_Reader_Excel5(); $PHPExcel = $PHPReader->load("./" . $_POST['files']); } // 获取哪一页的数据,0就是第一页 $currentSheet = $PHPExcel->getSheet(0); // 获取行数 $allColumn = $currentSheet->getHighestColumn(); // 获取列数 $allRow = $currentSheet->getHighestRow(); $arr = array(); //遍历获取数据装到$arr数组中 for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) { $arrs = (str_split($allColumn)); if (strlen($allColumn) >= 2) { foreach (range('A', 'Z') as $letter) { $address = $letter . $currentRow; $arr[$currentRow][] = $currentSheet->getCell($address)->getFormattedValue(); } for ($s1 = $arrs[0]; $s1 <= $arrs[1]; $s1++) { $address = $arrs[0] . $s1 . $currentRow; $arr[$currentRow][] = $currentSheet->getCell($address)->getFormattedValue(); } } else { //遍历获取数据存到数组,从A列开始获取到AB列 for ($currentColumn = 'A'; $currentColumn <= "AB"; $currentColumn++) { $address = $currentColumn . $currentRow; $arr[$currentRow][] = $currentSheet->getCell($address)->getFormattedValue(); } } } |
上面已经可以把表格的数据存到数组里面了,然后数组各位只需要遍历就可以处理了,使用框架的可以循环单独写入也行,要求效率的可以循环拼接成一条sql语句直接执行,如何写入数据库就不用我说了吧,各位都懂的。
原文地址:https://sunnyos.com/article-show-18.html
转载请注明:PHP笔记 » php通过PHPExcel导入Excel表格到MySQL数据库