首页

Excel表格导入数据库的方法详解

PHPExcel表格数据库 2018-05-02 6478

在php编程语言中,对于如何在Excel表格中导入数据库的方法是很多编程者比较头疼的一个问题,有些技术人员可能在百度尝试过搜索很多不同的问题,但是给出的答案经过自己测试之后,发现还是行不通,那么对此,专业的app开发报价燚轩科技也尝试了一下如何在Excel表格中导入数据库,现在将源代码展示给各位技术编程者,大家可以借鉴参考一下。

public function saveexcel(){

       require_once('./Thinkphp/Extend/Vendor/PHPExcel-1.8/Classes/PHPExcel.php');

       require_once('./Thinkphp/Extend/Vendor/PHPExcel-1.8/Classes/PHPExcel/IOFactory.php');

       require_once('./Thinkphp/Extend/Vendor/PHPExcel-1.8/Classes/PHPExcel/Reader/Excel5.php');

        //获取表格的大小,限制上传表格的大小5M

        // $file_size = $_FILES['myfile']['size'];

        // if ($file_size>5*1024*1024) {

        // echo "<script>alert('上传失败,上传的表格不能超过5M的大小');history.go(-1);</script>";

        //     exit();

        // }

 

        //限制上传表格类型

        $file_type = $_FILES['myfile']['type'];

        if ($file_type!='application/vnd.ms-excel') {

        echo "<script>alert('上传失败,只能上传excel200397-2003xls格式!');history.go(-1)</script>";

        exit();

        }

 

        //判断表格是否上传成功

        if (is_uploaded_file($_FILES['myfile']['tmp_name'])) {

            

            //以上三步加载phpExcel的类

 

        $objReader = \PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format

            //接收存在缓存中的excel表格

        $filename = $_FILES['myfile']['tmp_name'];

        $objPHPExcel = $objReader->load($filename); //$filename可以是上传的表格,或者是指定的表格

        $sheet = $objPHPExcel->getSheet(0);

        $highestRow = $sheet->getHighestRow(); // 取得总行数

     // $highestColumn = $sheet->getHighestColumn(); // 取得总列数

            

            //循环读取excel表格,读取一条,插入一条

            //j表示从哪一行开始读取  从第二行开始读取,因为第一行是标题不保存

            //$a表示列号

        $model=M('user');

        $rechargemodel=M('recharge');

        $joinmodel=M('joinplan');

 

        for($j=2;$j<=$highestRow;$j++)  

        {

            $name = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//获取B(姓名)列的值

            $tel = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();//获取C(手机号)列的值

             $arr['name']=$name;

             $arr['tel']=$tel;

              

             $arr['addtime']=time();

             $arr['geti']=0;

             $res=$model->add($arr);

              

                

            }

            if($res){

                success('添加成功',U('userlist'));

            }else{

                error('操作失败,请稍后再试');

            }

        }

}

 

Php excel 文件

 

代码导出(不需要引入 任何接口)

     public function  exportDayInner(){

          $model = M("Goodsinfo");

          // $innerdata = $mod->select();

          $list = $model->order('gid desc')->select();


                      // echo $mod->getLastSql();exit;

                      // dump($innerdata);die;

          $table = '';

          $table .= "<table align='center' border='1' cellpadding'0' cellspacing='0'>

                      <thead>

                      <tr>

                      <th class='name'>名称</th>

                      <th class='name'>名称</th>

                      <th class='name'>名称</th>

                      <th class='name'>名称</th>

                      <th class='name'>名称</th>

                      <th class='name'>名称</th>

                      <th class='name'>名称</th>

                      <th class='name'>名称</th>

                      <th class='name'>名称</th>

                      <th class='name'>名称</th>

                      <th class='name'>名称</th>

                      <th class='name'>名称</th>

                      <th class='name'>名称</th>

                      <th class='name'>名称</th>

                      <th class='name'>名称</th>

                      <th class='name'>名称</th>

                      </tr>

                      </thead>

                      <tbody>";

                      foreach ($list as $v) {

                      $table .= "<tr align='center'>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$a[$v['gname']]}</td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$b[$v['gname']]}</td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$v['gname']}/{$v['gname']} </td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$v['gname']}%</td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$v['gname']}</td>

                      <td class='name'>{$c[$v['gname']]}</td>                

                      </tr>";

                      }

                      $table .= "</tbody>

                      </table>";

  //通过header头控制输出excel表格

                      header("Pragma: public");  

                      header("Expires: 0");  

                      header("Cache-Control:must-revalidate, post-check=0, pre-check=0");  

                      header("Content-Type:application/force-download");  

                      header("Content-Type:application/vnd.ms-execl");  

                      header("Content-Type:application/octet-stream");  

                      header("Content-Type:application/download");

                      header('Content-Disposition:attachment;filename="商品列表.xls"');  

                      header("Content-Transfer-Encoding:binary");  

                      echo $table;

      }

以上就是为各位讲解的如何在Excel表格中导入数据库的具体方法了,希望有不懂的技术人员可以在评论中提出,我们会为您解答。

分享到微信朋友圈 +
打开微信,点击底部的“发现”,使用 “扫一扫” 即可将网页分享到我的朋友圈。 如何使用?
推荐文章

热贴More +

服务范围More +

联系我们

请扫二维码联系客服

854221200@qq.com

185-3825-9583

QQ客服

关于  ·  招聘  ·  案例中心  ·  网站地图

©@2018 燚轩科技版权所有 豫ICP备16015002号-4

百度提供搜索支持