php導(dǎo)入excel文件入mysql數(shù)據(jù)庫我們是需一借助一個(gè)phpexcel類文件了,有了這個(gè)類文件我們就可以快速簡單的導(dǎo)入excel到mysql數(shù)據(jù)庫中,下面來給大家整理一個(gè)例子,希望能對大家有幫助.
導(dǎo)入前我們需要先準(zhǔn)備一個(gè)數(shù)據(jù)庫,sql語句代碼如下:
- /*
- Navicat MySQL Data Transfer
- Source Server : m.survivalescaperooms.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處理程序,在這里我們需要使用一個(gè)phpexcel類文件了,這個(gè)可以百度搜索下載,代碼如下:
- <?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++)
- {//開源代碼Vevb.com
- 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="導(dǎo)入數(shù)據(jù)" />
- </form>
- </body>
新聞熱點(diǎn)
疑難解答