.连接数据库的connection.php文件
<?php
//修改下面代码来联接数据库
// mysql_connect打开一个到 MySQL 服务器的连接,如果成功则返回一个 MySQL 连接标识,失败则返回 FALSE。
$mysql=mysql_connect("localhost","root","root"); //integer mysql_connect(主机,用户名,口令);
mysql_select_db("test",$mysql); //boolean mysql_select_db(数据库名,连接号);
mysql_query("set names GBK"); //这就是指定数据库字符集,一般放在连接数据库后面就系了
?>

  

 .页面调用 
<?php

require("connection.php"); 

require("UpLoad_Excel.php"); 

if($leadExcel == "true")
{   //获取上传的文件名
  $filename = $HTTP_POST_FILES['inputExcel']['name'];   //上传到服务器上的临时文件名
  $tmp_name = $_FILES['inputExcel']['tmp_name'];
  $msg = uploadFile($filename,$tmp_name);
} ?> <form method="post" enctype="multipart/form-data">
  <input type="hidden" name="leadExcel" value="true">
  <table align="center" width="90%" border="0">
  <tr>
    <td>
      <input type="file" name="inputExcel"><input type="submit" value="导入数据">
    </td>
  </tr>
  </table>
</form>

  

.导入的UpLoad_Excel.php文件
<?php

require("connection.php");

//导入Excel文件
function uploadFile($file,$filetempname)
{   //自己设置的上传文件存放路径
  $filePath = 'upFile/';
  $str = "";   //下面的路径按照你PHPExcel的路径来修改
  set_include_path('.'. PATH_SEPARATOR . 'D:\xampp\htdocs\XM1\PHPExcel' . PATH_SEPARATOR .get_include_path());   require_once 'PHPExcel.php';
  require_once 'PHPExcel\IOFactory.php';
  require_once 'PHPExcel\Reader\Excel5.php';   $filename=explode(".",$file);//把上传的文件名以“.”好为准做一个数组。
  $time=date("y-m-d-H-i-s");//去当前上传的时间
  $filename[0]=$time;//取文件名t替换
  $name=implode(".",$filename); //上传后的文件名
  $uploadfile=$filePath.$name;//上传后的文件名地址   //move_uploaded_file() 函数将上传的文件移动到新位置。若成功,则返回 true,否则返回 false。
  $result=move_uploaded_file($filetempname,$uploadfile);//假如上传到当前目录下
  if($result) //如果上传文件成功,就执行导入excel操作
  {
    $objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
    $objPHPExcel = $objReader->load($uploadfile);
    $sheet = $objPHPExcel->getSheet(0);
    $highestRow = $sheet->getHighestRow(); // 取得总行数
    $highestColumn = $sheet->getHighestColumn(); // 取得总列数     //循环读取excel文件,读取一条,插入一条
    for($j=2;$j<=$highestRow;$j++)
    {
      for($k='A';$k<=$highestColumn;$k++)
      {
        $str .= iconv('utf-8','gbk',$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue()).'\\';//读取单元格
      }
      //explode:函数把字符串分割为数组。
      $strs = explode("\\",$str);
      $sql = "INSERT INTO tab_zgjx_award (jx_name,jx_degree,jx_item_name,jx_unit,dy_originator,de_originator,xm_intro,hj_item_id)       VALUES('".
        $strs[0]."','". //奖项名称
        $strs[1]."','". //奖项届次
        $strs[2]."','". //获奖项目名
        $strs[3]."','". //获奖单位
        $strs[4]."','". //第一发明人
        $strs[5]."','". //第二发明人
        $strs[6]."','". //项目简介
        $strs[7]."')"; //获奖项目编号       mysql_query("set names GBK");//这就是指定数据库字符集,一般放在连接数据库后面就系了
      if(!mysql_query($sql))
      {
        return false;
      }
      $str = "";
    }     unlink($uploadfile); //删除上传的excel文件
    $msg = "导入成功!";
  } else {
    $msg = "导入失败!";
  } return $msg;
}
?>

  


使用PHPExcel导入Excel到MySql的更多相关文章

  1. 使用phpExcel导入Excel进入Mysql数据库

    1.http://phpexcel.codeplex.com/下载phpExcel 2. <? require_once 'PHPExcel.php'; require_once 'PHPExc ...

  2. Thinkphp 用PHPExcel 导入Excel

    搞了个简单的Excel导入, 用的是PHPExcel(百科:用来操作Office Excel文档的一个PHP类库, 基于微软的OpenXML标准和PHP语言) 好, 不说了, 开始吧... 首先得有P ...

  3. phpexcel导入excel文件报the filename xxx is not recognised as an OLE file错误。

    工作中频繁会用phpexcel类导入excel文件的数据到数据库,目前常用的excel文件格式有:xls.csv.xlsx. 刚开始,针对xls文件,使用如下程序,能正常运行: $objReader ...

  4. YII使用PHPExcel导入Excel文件的方法

    1.下载phpexcel,将压缩包中的classes复制到protected/extensions下并修改为PHPExcel. 2.修改YII配置文件config/main.php 'import'= ...

  5. thinkphp整合系列之phpexcel导入excel数据

    一:导入phpexcel /ThinkPHP/Library/Vendor/PHPExcel 二:导入excel的函数 /** * 导入excel文件 * @param string $file ex ...

  6. 使用phpexcel导入excel文件种的时期数据时数据导入格式

    在使用phpexcel导入类似于 YYYY-MM-DD HH:ii:ss格式的数据时,导入成功以后会发现导入的数据其实是类似于42085.746516204格式的数据( excel在存储时间类型的数据 ...

  7. phpexcel导入excel表格

    html代码 <form action="{:U('Mall/updExcel')}" method="POST" enctype="multi ...

  8. thinkphp5使用PHPExcel导入Excel数据

    安装PHPExcel扩展类 地址:https://github.com/PHPOffice/PHPExcel ①通过composer安装 ②手动下载, 放在项目的extend目录下 代码中引入 由于P ...

  9. PHPExcel 导入Excel数据 (导出下一篇我们继续讲解)

    一:使用composer下载 phpoffice/phpexcel 或者直接下载安装包 composer require phpoffice/phpexcel 二 1:导入数据 原理:读取文件,获取文 ...

随机推荐

  1. Python 2.x闭包(enclosure)中的变量访问&修改

    http://stackoverflow.com/questions/3190706/nonlocal-keyword-in-python-2-x ---answer---- Python can r ...

  2. jquery学习——选择器

    一.基础选择 1.$("*") 选择所有元素 2.$(".class") 选择某个类 3.$("#id") 选择某个id 4.$(" ...

  3. phpcms导航中添加内部链接

    phpcms中栏目有3中类型 1.普通栏目 2.单网页 3.外部链接 其中如果想添加本站的内部链接,可以使用3,然后在添加链接的地方填入剩下的地址即可(需要以/开头) 如: /index.php?m= ...

  4. Group Anagrams

    Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...

  5. POJ 2479

    ---恢复内容开始--- http://poj.org/problem?id=2479 #include <stdio.h> #include <iostream> using ...

  6. selenium处理rich text(富文本框)

    WordPress 的 rich  text 采用js,先让selenium切换到iframe中 driver.switchTo().frame("content_ifr"); 然 ...

  7. 初识Python(一)

    Python简介 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解 ...

  8. 【Android】如何将eclipse工程导入studio

    1.eclipse工程右键->export->Generate Gradle build files 2.studio->New->import project->选择工 ...

  9. FileUpload控件使用初步

    FileUpload控件使用初步   FileUpload控件使用初步: 1.实现文件上传 protected void btnSubmit_click(object sender, EventArg ...

  10. LightOJ1336 Sigma Function(约数和为偶数的个数)

    Sigma Function Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit ...