标签:
import.php
<?php header("Content-Type:text/html;charset=utf-8"); echo ‘<html> <body> <form action="import.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html>‘; if(@$_FILES["file"]){ var_dump(@$_FILES["file"]); $path="upload/" . $_FILES["file"]["name"]; //把上传的文件 move_uploaded_file($_FILES["file"]["tmp_name"],$path); $dir = $path; // $dir =‘1.xlsx‘; /** PHPExcel_IOFactory */ require_once ‘PHPExcel_1.8.0_doc/Classes/PHPExcel.php‘; if (!file_exists("$dir")) { exit("没有发现excel文件!"); } //获取扩展名 function getExtension($filename) { $myext = substr($filename, strrpos($filename, ‘.‘)); return str_replace(‘.‘, ‘‘, $myext); } $excel_ext = getExtension($dir); if ($excel_ext == "xlsx") { $reader = PHPExcel_IOFactory::createReader(‘Excel2007‘); //设置以Excel7格式(Excel97-2007工作簿) } else { $reader = PHPExcel_IOFactory::createReader(‘Excel5‘); //设置以Excel5格式(Excel97-2003工作簿) } $PHPExcel = $reader->load("$dir"); // 载入excel文件 $sheet = $PHPExcel->getSheet(0); // 读取第一個工作表 $highestRow = $sheet->getHighestRow(); // 取得总行数 $highestColumm = $sheet->getHighestColumn(); // 取得总列数 //1.导入配置文件 //require("conf/dbconfig.php"); define(‘HOST‘, ‘localhost‘); define(‘USER‘, ‘root‘); define(‘PASS‘, ‘‘); define(‘DBNAME‘, ‘mssc‘); //2.连接MySQL、并选择数据库 $link = @mysql_connect(HOST, USER, PASS) or die("数据库连接失败!"); mysql_select_db(DBNAME, $link); for ($j = 2; $j <= $highestRow; $j++) { $telephone = $PHPExcel->getActiveSheet()->getCell("B" . $j)->getValue();//获取B列的值 //过滤不正确的手机号 if (preg_match("/^1[34578]\d{9}$/", $telephone)) { $tel[] = $telephone; } else { $filter[] = $telephone; } } //分割数组为字符串 //组装成字符串 $mess = ‘(‘.implode("),(", $tel).‘)‘; //成功统计 $cgsum = count($tel); //失败统计 $sbsum = count($filter); mysql_query("INSERT INTO onethink_member (tel) VALUES ".$mess); // //释放工作表对象 unset($sheet); //释放Excel文件对象 unset($PHPExcel); //返回数据 //删除文件 unlink($dir); exit(‘更新数据库成功‘); } ?>
效果图:
标签:
原文地址:http://www.cnblogs.com/hltswd/p/5657670.html