How to import Excel to MySQL using PHP

Source: Internet
Author: User

How to import Excel to MySQL using PHP

 

Note:

Modify the following line in reader. php
1. Change require_once 'spreadsheet/Excel/Reader/OLERead. php' to require_once 'oleread. inc ';

2. require_once 'oleread. inc' can also be merged into a file before the file copied to reader.

3. $ data-> setOutputEncoding ('utf-8') can also be used in this way. My projects are all UTF-8 encoded, the preceding Code $ data-> setOutputEncoding ('cp936') is used at the beginning. The result is that the encoding of the imported database is incorrect. Finally, set it to UTF-8.

Upload Excel Code:

UpExcel. php

 

  
<Script> function import_check () {var f_content = form1.file. value; var fileext = f_content.substring (f_content.lastIndexOf (.), f_content.length) fileext = fileext. toLowerCase () if (fileext! Else '.xls ') {alert (Sorry, the imported data format must be xls files. Please adjust the format and upload it again. Thank you !); Return false ;}}</script>
Select the data table you want to import:

 

 

Insert. php code for uploading and writing data to the database

 

 SetOutputEncoding ('utf-8'); $ data-> read ($ Import_TmpFile); $ count = 0; for ($ I = 1; $ I <= $ data-> sheets [0] ['numrows ']; $ I ++) {$ SQL = INSERT INTO test_xls (id, tm, name) VALUES ('. $ data-> sheets [0] ['cells '] [$ I] [1]. ','. $ data-> sheets [0] ['cells '] [$ I] [2]. ','. $ data-> sheets [0] ['cells '] [$ I] [3]. '); echo $ SQL .; if (mysql_query ($ SQL) {$ count ++ ;}} echo <script> alert ('imported successfully. $ count. data entries '); </script >;}?>

 

Mysql table:

 

Excel:

 

 

 

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.