import.php
<?PHPHeader("Content-type:text/html;charset=utf-8");Echo‘‘;if(@$_files["File"]){ Var_dump(@$_files["File"]); $path= "upload/".$_files["File"] ["Name"];//put the uploaded file 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("No Excel file found! "); }//Get extension functionGetExtension ($filename) { $myext=substr($filename,Strrpos($filename, ‘.‘)); return Str_replace(‘.‘, ‘‘,$myext); } $excel _ext= GetExtension ($dir); if($excel _ext= = "Xlsx") { $reader= Phpexcel_iofactory::createreader (' Excel2007 ');//set in EXCEL7 format (excel97-2007 workbook)}Else { $reader= Phpexcel_iofactory::createreader (' Excel5 ');//set in EXCEL5 format (excel97-2003 workbook)}$PHPExcel=$reader->load ("$dir");//Loading Excel Files$sheet=$PHPExcel->getsheet (0);//read the first worksheet$highestRow=$sheet->gethighestrow ();//total number of rows obtained$highestColumm=$sheet->gethighestcolumn ();//gets the total number of columns//1. Import configuration file//require ("conf/dbconfig.php");Define(' HOST ', ' localhost ');Define(' USER ', ' root ');Define(' PASS ', ');Define(' DBNAME ', ' MSSC ');//2. Connect to MySQL and select a database$link= @mysql_connect(HOST, USER, PASS) or die("Database connection failed! ");mysql_select_db(DBNAME,$link); for($j= 2;$j<=$highestRow;$j++) { $telephone=$PHPExcel->getactivesheet ()->getcell ("B").$j)->getvalue ();//get the value of column B//filter the incorrect phone number if(Preg_match("/^1[34578]\d{9}$/",$telephone)) { $tel[] =$telephone; } Else { $filter[] =$telephone; }}//split Array As string//assemble into string$mess= ' ('.implode("),(",$tel).‘)‘;//Success Statistics$cgsum=Count($tel);//Failure Statistics$sbsum=Count($filter);mysql_query(INSERT into Onethink_member (tel) VALUES.)$mess);////Release Sheet Objectunset($sheet);//releasing an Excel file Objectunset($PHPExcel);//return data//delete fileunlink($dir);Exit(' Update database succeeded ');}?>
:
Import data from Excel into a database