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.
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. PHP sample code for importing EXCEL into MYSQL: test. php
- <? 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.