Import data from Excel into a database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.