PHP method for importing Excel to MySQL _ PHP Tutorial

Source: Internet
Author: User
Tags php sample code
Use PHP to import Excel files to MySQL. I have studied it ~ Many methods ~ Finally, I decided to use Excel to import ~ I searched the internet for a lot of such information and found that the excel file was saved as a csv file, and then imported from the csv file. Here we will introduce the study ~ Many methods ~ Finally, I decided to use Excel to import ~ I searched the internet for a lot of such information and found that the excel file was saved as a csv file, and then imported from the csv file. Here is an example of importing an excel file to mysql. It took me one night to test the function. no garbled characters are displayed in simplified and traditional Chinese characters. it is very easy to use.
PHP-ExcelReader,: http://sourceforge.net/projects/phpexcelreader
Note:
PHP Imports EXCEL to the MYSQL test environment: the MYSQL database adopts utf8 encoding. the imported EXCEL file is in the xls format. after testing, the xlsx format [excel 2007] is also OK.
Replace it with the configured data, such as the database configuration. Run http: // localost/test. php to import data.
Below is the detailed code I posted, where test. php is the test file I wrote, reader. php and oleread. inc files are downloaded from the Web site provided above.
1. PHP sample code for importing EXCEL into MYSQL: test. php

The code is as follows:


<? Php
Require_once 'reader. php '; // ExcelFile ($ filename, $ encoding); $ data = new Spreadsheet_Excel_Reader (); // Set output Encoding. $ data-> setOutputEncoding ('gbk ');
// ”Data.xls refers to the excel file to be imported to mysql.
$ Data-> read('data.xls ');
@ $ Db = mysql_connect ('localhost', 'root', '123') or
Die ("cocould not connect to database."); // connect to the database
Mysql_query ("set names 'gbk'"); // output Chinese
Mysql_select_db ('mydb'); // select a database
Error_reporting (E_ALL ^ E_NOTICE );
For ($ I = 1; $ I <= $ data-> sheets [0] ['numrows ']; $ I ++ ){
// Print the excel table data in a for loop with the following comments
/*
For ($ j = 1; $ j <= $ data-> sheets [0] ['numcols']; $ j ++ ){
Echo "\" ". $ data-> sheets [0] ['cells '] [$ I] [$ j]." \ ",";
}
Echo "\ n ";
*/
// The following code inserts [3 fields] of the excel table data into mysql,
Modify the following code based on the number of fields in your excel table!
$ SQL = "INSERT INTO test VALUES ('".
$ Data-> sheets [0] ['cells '] [$ I] [1]. "', '".
$ Data-> sheets [0] ['cells '] [$ I] [2]. "', '".
$ Data-> sheets [0] ['cells '] [$ I] [3]. "')";
Echo $ SQL. '<br/> ';
$ Res = mysql_query ($ SQL );
}
?>


The above describes how PHP Imports EXCEL into MYSQL. I hope many users can help me.
However, the files in his class are found to be faulty. after modification, it will be normal ~ You can

Download phpexcelreader

Bytes ~ Many methods ~ Finally, I decided to use Excel to import ~ I searched the internet for a lot of such information and found that the excel file was saved as a csv file, and then imported from the csv file. Here we will introduce...

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.