How do I import data from an Excel table into a database?

Source: Internet
Author: User

The first method:

Ideas: It is not so easy to import data from Excel tables into a database directly, you can save the Excel table as a. csv-formatted document (features: content is separated by commas), and then processed into a two-dimensional array through a series of document manipulation functions, and then further processed to import the database

The idea is correct but sometimes the problem (explode here, because the data may also contain the "," number, so generally do not use this method, see the second method)

1: Prepare an Excel table to import the data:

2: Document saved as. csv suffix (feature: content separated by commas)

3: Handling of document manipulation functions

 

1    $filepath=Str_replace("\\", "/",dirname(__file__).‘ /score.csv '); 2     $TP=fopen($filepath, ' RB ');3      while(!feof($TP)){4       $con=fgets($TP);5       if($con!=NULL){6         $row[]=Explode(",",$con);//mark with "," as a split7 }8 }9 Ten  One     fclose($TP); A     Echo"<table border=1>"; -      for($i= 0;$i<Count($row);$i++){ -       Echo"<tr><td>{$row[$i][0]}</td><td>{$row[$i][1]}</td></tr> "; the } -     Echo"</table>";

Operation Result:

The second method:

PHP comes with a function to process the CSV file, you can get the contents directly, and return an array;

1 $filepath=Str_replace("\\", "/",dirname(__file__).‘ /score.csv ');2 $TP=fopen($filepath, ' RB ');3  while(!feof($TP)){4     $con=Fgetcsv($TP);//PHP comes with a function to get the contents of a CSV document;5     Echo"<pre>";6     Print_r($con);7     Echo"</pre>";8 }9 fclose($TP);

Operation Result:

Delete operation of extended file: Unlink ()

<?PHPHeader("Content-type:text/html;charset=utf-8");$filename=Str_replace("\\", "/",dirname(__file__)." /ceshi/");$arr=Array("A.txt", "B.txt", "C.txt", "D.txt");foreach($arr  as $v){    if(filesize($filename.$v) <10){        unlink($filename.$v); Echo"The number of bytes is less than 10."$filename.$v." The file was deleted <br/> "; Continue; }Else{        $con=file_get_contents($filename.$v); if(Stripos($con, "fuck")!==false){            unlink($filename.$v); Echo"Containing uncivilized words."$filename.$v." The file was deleted <br/> "; }    }}?>

How do I import data from an Excel table 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.