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: