Before importing we need to prepare a database, SQL statement code
The code is as follows |
Copy Code |
/* Navicat MySQL Data Transfer Source server:www.111cn.net localhost Source Server version:50133 Source host:localhost:3306 Source Database:test Target Server Type:mysql Target Server version:50133 File encoding:65001 Date:2011-10-11 14:11:38 */ SET foreign_key_checks=0; -- ---------------------------- --Table structure for ' execl ' -- ---------------------------- DROP TABLE IF EXISTS ' execl '; CREATE TABLE ' execl ' ( ' id ' int (one) not NULL auto_increment, ' Name ' varchar DEFAULT NULL, PRIMARY KEY (' id ') ) Engine=myisam auto_increment=16 DEFAULT charset=latin1; -- ---------------------------- --Records of EXECL -- ---------------------------- INSERT into ' execl ' VALUES (' n ', ' Jim '); INSERT into ' execl ' VALUES (', ' Taurus '); |
PHP processing program, where we need to use a Phpexcel class file, this can be Baidu search download.
The code is as follows |
Copy Code |
<?php if ($_files[' execl '] [' name ']) { $db = mysql_connect (' localhost ', ' root ', '); mysql_select_db (' Test '); mysql_query (' Set names GBK '); Require_once ' reader.php '; $data = new Spreadsheet_excel_reader (); $data->setoutputencoding (' CP936 '); $data->read ($_files[' execl '] [' name ']); Error_reporting (e_all ^ e_notice); $sql = ""; For ($i =1 $i <= $data->sheets[0][' numrows '); $i + +) { if ($data->sheets[0][' cells '] [$i][1]!= "") { $sql = INSERT into ' execl ' (' name ') VALUES (' ". $data->sheets[0][' cells '] [$i][2]."; if (mysql_query ($sql)) {
echo ' success '; }else{ Die (' failure '); } } } } ?> <body> <form action= "" method= "Post" enctype= "Multipart/form-data" > <input type= "File" name= "Execl"/> <input type= "Submit" value= "Import data"/> </form> </body> |