您好,欢迎访问一九零五行业门户网

PHP实例:使用PHPExcel导入Excel2003文档和Excel2007文档到MySQL数据库中,_PHP教程

php实例:使用phpexcel导入excel2003文档和excel2007文档到mysql数据库中,如果要使用phpexcelreader将excel 数据导入到mysql 数据库,请读者点击这个文章查看。
使用phpexcelreader将excel 数据导入到mysql 数据库。
下面我们介绍另一种方法来实现导入excel到mysql数据库。
1、到官网http://phpexcel.codeplex.com/下载phpexcel类库,我目前使用的是1.7.9版本。如图:
2、直接上代码。
(1)、conn.php文件(这个不用介绍,你懂得):
php code复制内容到剪贴板$mysql=mysql_connect(localhost,root,root);   mysql_select_db(test,$mysql);   mysql_query(set names gbk);  (2)、html页面部分:index.php文件(表单提交页面):
xml/html code复制内容到剪贴板form name=form2 method=post enctype=multipart/form- data action=upload_excel.php>  input type=hidden name=leadexcel value=true>  table align=center width=90%  border=0>  tr>     td>      input type=file name=inputexcel>input type=submit name=import value=导入数据 >     td>  tr>  table>  form>  (3)、表单处理处理程序部分:upload_excel.php文件:
php code复制内容到剪贴板include(conn.php);   include(function.php);      if($_post ['import']==导入数据 ){         $leadexcel=$_post['leadexcel'];              if($leadexcel == true)       {           //echo ok;die();           //获取上传的文件名           $filename = $http_post_files['inputexcel'] ['name'];           //上传到服务器上的临时文件名           $tmp_name = $_files ['inputexcel']['tmp_name'];                      $msg = uploadfile($filename,$tmp_name);           echo $msg;       }   }  (4)、函数部分:function.php文件:
php code复制内容到剪贴板//导入excel文件   function uploadfile($file,$filetempname)    {       //自己设置的上传文件存放路径       $filepath = 'upfile/';       $str = ;       //下面的路径按照你 phpexcel的路径来修改       set_include_path('.'. path_separator .'e:\php\appserv\www\91ctcstudy\phpexcelimportsql2 \phpexcel' . path_separator .get_include_path());                 require_once 'phpexcel.php';       require_once 'phpexcel\iofactory.php';       //require_once 'phpexcel\reader\excel5.php';//excel 2003       require_once 'phpexcel\reader\excel2007.php';//excel 2007         $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 excel2003          $objreader = phpexcel_iofactory::createreader('excel2007');//use excel2003 和  2007 format          // $objphpexcel = $objreader->load($uploadfile); //这个容易造成httpd崩溃          $objphpexcel = phpexcel_iofactory::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 =& nbsp;explode(\\,$str);                            //var_dump ($strs);              //die();              $sql =&n bsp;insert into z_test_importexcel(duty_date,name_am,name_pm) values ('.$strs[0].','.$strs[1].','.$strs[2].');                   //echo $ sql;              mysql_query (set names gbk);//这就是指定数据库字 符集,一般放在连接数据库后面就系了               if(! mysql_query($sql)){                & nbsp; return false;              }              $str =&n bsp;;         }               unlink ($uploadfile); //删除上传的excel文件         $msg = 导入成 功!;      }else{         $msg = 导入失 败!;       }       return $msg;   }  关于这个函数,参考了http://blog.csdn.net/grassroots20 11/article/details/8104604该博友的文章,但是,该博友的写法,我认为有问题,至少 ,我用的
$objphpexcel = $objreader->load ($uploadfile);
这一句写法,在运行的时候,会出现:
或者是phpexcel类库官方后来升级了,调用方法需要改正一下吧,具体笔者就没考究了。
3、通过上面的几个步骤,读者再分别准备一个xls和xlsx文档就可以了,系统运行效果:
附件下载:完整demo下载(已含phpexcel类库)附件下载:完整demo下载(已含phpexcel类库)
http://www.bkjia.com/phpjc/1048745.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1048745.htmltecharticlephp实例:使用phpexcel导入excel2003文档和excel2007文档到mysql数据库中, 如果要使用phpexcelreader将excel 数据导入到mysql 数据库,请读者点击这个文...
其它类似信息

推荐信息