美文网首页
thinkphp3.2 mysql导入excel数据

thinkphp3.2 mysql导入excel数据

作者: 呦丶耍脾气 | 来源:发表于2017-09-21 11:45 被阅读33次

在tp3.2中使用phpexcelreader,实现mysql导入excel数据。
新建一个控制器随便,在调用方法中放入下面代码

              // ExcelFile($filename, $encoding);
                $data = new \Org\Util\Reader();//将需要使用的文件放入org/util
                // Set output Encoding.
                $data->setOutputEncoding('utf-8');
                //”$_FILES['files']['tmp_name']”是指要导入到mysql中的excel文件
                $data->read($_FILES['files']['tmp_name']);
                $datas =array();
                /** 事务开启*/
                $model = new \Think\Model();
                $model->startTrans();//开启事务
                /** 事务开启结束*/
                if($data->sheets[0]){
                    for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) {
                        //以下代码是将excel表数据【3个字段】插入到mysql中,根据你的excel表字段的多少,改写以下代码吧!
                        if($ids[0]==1){
                            $check = 0;
                        }elseif($ids[0]==2){
                            $check = 1;
                        }else{
                            $this->error( '参数错误');
                            die;
                        }
                        $datas['art_title'] = $data->sheets[0]['cells'][$i][1];
                        $datas['zhubian'] = $data->sheets[0]['cells'][$i][2];
                        $datas['chubanshe'] = $data->sheets[0]['cells'][$i][3];
                        $datas['shuhao'] = $data->sheets[0]['cells'][$i][4];
                        $datas['shijian'] = $data->sheets[0]['cells'][$i][5];
                        $datas['dingjia'] = $data->sheets[0]['cells'][$i][6];
                        $datas['art_time'] = $post['art_time'];
                        $datas['cate_pid'] = $ids[1];
                        $datas['check'] = $check;

//                        $sql = "INSERT INTO cms_zhishi(art_title,zhubian,chubanshe,shuhao,shijian,dingjia,check,art_time) VALUES('" .
//                            $data->sheets[0]['cells'][$i][2] . "','" . $data->sheets[0]['cells'][$i][3] . "','" . $data->sheets[0]['cells'][$i][4] ."','" . $data->sheets[0]['cells'][$i][5] ."','" . $data->sheets[0]['cells'][$i][6] ."','" . $data->sheets[0]['cells'][$i][7] ."','".$check. "','".$post['art_time']."')";
//                        $res = $this->zhishiObj->execute($sql);
                        $res = $this->zhishiObj->storeData($datas);//插入数据库
                        if(!$res){
                            $model->rollback();//判断插入失败整个回滚
                            $this->error('数据导入失败!');
                            die;
                        }
                    }
                    $model->commit();//执行事务
                    $this->success( '添加成功',U( 'Phpexcel/lists' ) );//成功跳转的路径

                }else{
                    $this->error( 'excel文件问题!请检查sheet1(第一个)是否有内容,如无问题请联系管理员');
                }

  • 前台html代码
<form action="" method="post" enctype="multipart/form-data">

        <table class="add_tab">
            <tbody>
            <tr>
                <th>excel导入数据:</th>
                <td>
                    <div id='mainpre'style=""></div>
                    <span id="up-btn" class="btn btn-success fileinput-button btn-sm up-btn-style" style="">
                        <input  type="file" name="files">
                    </span>
                    <span><i class="fa fa-exclamation-circle yellow"></i>选择excel文件</span>
                    <!-- 上传成功文件显示区 -->
                </td>
            </tr>
            <tr>
                <th></th>
                <td>
                    <input type="submit"  value="提交">
                    <input type="button" class="back" onclick="history.go(-1)" value="返回">
                </td>
            </tr>
            </tbody>
        </table>
    </form>

相关文章

网友评论

      本文标题:thinkphp3.2 mysql导入excel数据

      本文链接:https://www.haomeiwen.com/subject/jsszsxtx.html