php匯入excel檔案入mysql資料庫例子

來源:互聯網
上載者:User

匯入前我們需要先準備一個資料庫,sql語句代碼

 代碼如下 複製代碼


/*
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(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['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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.