Home >php教程 >php手册 >php导入excel文件入mysql数据库例子

php导入excel文件入mysql数据库例子

WBOY
WBOYOriginal
2016-05-25 16:38:211149browse

php导入excel文件入mysql数据库我们是需一借助一个phpexcel类文件了,有了这个类文件我们就可以快速简单的导入excel到mysql数据库中,下面来给大家整理一个例子,希望能对大家有帮助.

导入前我们需要先准备一个数据库,sql语句代码如下:

/* 
	Navicat MySQL Data Transfer 
	 
	Source Server         : www.phprm.com 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处理程序,在这里我们需要使用一个phpexcel类文件了,这个可以百度搜索下载,代码如下:

<?php 
	if($_FILES[&#39;execl&#39;][&#39;name&#39;]){ 
	 $db = mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;); 
	  mysql_select_db(&#39;test&#39;); 
	  mysql_query(&#39;set names gbk&#39;); 
	  require_once &#39;reader.php&#39;; 
	  $data = new Spreadsheet_Excel_Reader(); 
	  $data->setOutputEncoding(&#39;CP936&#39;); 
	  $data->read($_FILES[&#39;execl&#39;][&#39;name&#39;]); 
	 error_reporting(E_ALL ^ E_NOTICE); 
	 $sql   = ""; 
	 for($i=1;$i<=$data->sheets[0][&#39;numRows&#39;];$i++) 
	 {//开源代码phprm.com 
	  if($data->sheets[0][&#39;cells&#39;][$i][1]!=""){ 
	   $sql = "INSERT INTO `execl`(`name`)values(&#39;".$data->sheets[0][&#39;cells&#39;][$i][2]."&#39;);"; 
	   if(mysql_query($sql)){ 
	     
	    echo &#39;成功&#39;; 
	   }else{ 
	    die(&#39;失败&#39;); 
	   } 
	  }  
	 } 
	} 
	 
	<head> 
	</head> 
	<body> 
	  <form action="" method="post" enctype="multipart/form-data"> 
	      <input type="file" name="execl" /> 
	      <input type="submit" value="导入数据" /> 
	  </form> 
	</body>

       
               
               

本文链接:

收藏随意^^请保留教程地址.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn