php语言中Excel表格导入数据库的方法详解

2018-06-22 05:37:59来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

  在php编程语言中,对于如何在Excel表格中导入数据库的方法是很多编程者比较头疼的一个问题,有些技术人员可能在百度尝试过搜索很多不同的问题,但是给出的答案经过自己测试之后,发现还是行不通,那么对此,燚轩科技也尝试了一下如何在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();

          $smodel = M("Shopinfo");

          $gmodel = M("Goodscategory");

          foreach ($list as $key => $value) {

            $list[$key]['maoli']=floatval($value['price'])-floatval($value['jprice']);

            $list[$key]['maolilv']=floatval($list[$key]['maoli'])/floatval($value['price'])*100;

            $list[$key]["catename"]=$gmodel->where("id=%d",$value['goodscateid'])->getField("name");

          }

          $a =['普通商品','休闲专区','特价专区','套餐专区','酒水专区','新品专区','限购','普通商品'];

          $b = ['推荐','不推荐'];

          $c = ['',''];                

                      // 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['gid']}</td>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                      <td class='name'>{$c[$v['repertor']]}</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表格中导入数据库的具体方法了,希望有不懂的技术人员可以在评论中提出,我们会为您解答。

  本文由专业做app开发制作、微信小程序开发、网站建设的郑州燚轩软件科技发表,如需转载请注明原文作者及出处!

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Laravel Service Provider 开发时设置延迟加载时遇到的问题

下一篇:SourceTree Win10 安装不成功解决过程记录