/* Navicat MySQL Data Transfer Source Server : 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(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=latin1; -- ---------------------------- -- Records of execl -- ---------------------------- INSERT INTO `execl` VALUES ('14', 'jim'); INSERT INTO `execl` VALUES ('15', 'taurus');
<?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 '成功'; }else{ die('失败'); } } } } ?> <head> </head> <body> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="execl" /> <input type="submit" value="导入数据" /> </form> </body>
希望本文所述对大家的php程序设计有所帮助。