Import an EXCEL file to MYSQL using PHP

Source: Internet
Author: User

  PHP tutorial for helper House (www.bkjia.com)Recently, due to project requirements, you need to import an excel file to the mysql database through the php page. 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:

Test environment: the MYSQL database adopts utf8 encoding. The imported EXCEL file is in xls format. After testing, the xlsx format [excel 2007] is also OK.

In this text, the red mark is worth attention. Replace it with your configured data, such as 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. test. php

Reference content is as follows:

<? Php

Require_onCe 'reader. php ';

// ExcelFile ($ filename, $ encoding );

$ DaTa = new Spreadsheet_Excel_Reader ();

// Set output Encoding.

$ DaTa-> setOutputEncoding ('gbk ');

// "DaTa.xls is an 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. Rewrite 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 );

}

?>

Related Article

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.