Search the Internet a lot of this information, found that the Excel file is saved as a CSV file, and then imported from the CSV file. Here is an example of importing an Excel file directly into MySQL. I spent the whole night testing, no matter the import of simple and traditional will not appear garbled, very useful.
Php-excelreader, download Address: Http://sourceforge.net/projects/phpexcelreader
Description
PHP imports Excel into the MySQL test environment: The MySQL database is UTF8 encoded. The import Excel document is in the XLS format, tested, and the XLSX format [Excel 2007] is OK.
If you want to be aware of the red label, replace it with your configured data, such as database configuration. Run http://localost/test.php to implement the import.
Here is the detailed code I posted, where test.php wrote the test files for me, reader.php and oleread.inc files are downloaded from the URLs provided above.
1. PHP import Excel into MySQL code example test.php
- < ? PHP
- Require_once ' reader.php ';
- Excelfile ($filename, $encoding);
- $ Data = New Spreadsheet_excel_reader ();
- Set output Encoding.
- $data- > setoutputencoding (' GBK ');
- "Data.xls" means an Excel file to import into MySQL
- $data- > read (' Data.xls ');
- @ $ DB = mysql_connect (' localhost ', ' root ', ' 123456 ') or
- Die ("Could not connect to database."); /Connection Database
- mysql_query ("Set names ' GBK '");//Output Chinese
- mysql_select_db (' mydb '); Select Database
- Error_reporting (e_all ^ e_notice);
- For ($i = 1; $i <= $data->sheets[0 [' NumRows ']; $i + +) {
- The For loop of the following comments prints the Excel table data
- /*
- For ($J = 1; $j < = $data-> sheets[0][' numcols '); $j + +) {
- echo "". $data- > sheets[0][' cells ' [$i] [$j]. "", ";
- }
- echo "n";
- */
- The following code inserts the Excel table data "3 fields" into MySQL,
Depending on how much of your Excel table field, rewrite the following code!
- $ 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 is the introduction of the PHP Excel import MySQL method, I hope that many friends need help.
http://www.bkjia.com/PHPjc/446270.html www.bkjia.com true http://www.bkjia.com/PHPjc/446270.html techarticle Search the Internet a lot of this information, found that the Excel file is saved as a CSV file, and then imported from the CSV file. Here is an example of importing an Excel file directly into MySQL. I spend ...