Home  >  Article  >  Backend Development  >  TP5 import database

TP5 import database

不言
不言Original
2018-05-08 10:35:082318browse

This article mainly introduces the TP5 import database, which has certain reference value. Now I share it with everyone. Friends in need can refer to the

html page code theme:

<form method="post" action="{:url(&#39;Index/savestudentImport&#39;)}" class="form-signin" enctype="multipart/form-data" >  
    <p>
        <select name="themeid">
            {volist name="theme" id="v"}
                <option value="{$v.id}">{$v.title}</option>
            {/volist}
        </select>
    </p><br />
    <input name="excel" type="file" class="form-control">  
    <br /><br />
    <button class="btn btn-lg btn-primary btn-block">导入</button>  
</form>

Controller receiving code:

public function savestudentImport(){  
        //import(&#39;phpexcel.PHPExcel&#39;, EXTEND_PATH);//方法二  
        vendor("PHPExcel.PHPExcel"); //方法一  
        $objPHPExcel = new \PHPExcel();  
          $themeid=input(&#39;post.themeid&#39;);//导入数据类型
        //获取表单上传文件  
        $file = request()->file(&#39;excel&#39;);  
        $info = $file->validate([&#39;size&#39;=>1567800,&#39;ext&#39;=>&#39;xlsx,xls,csv&#39;])->move(ROOT_PATH . &#39;public&#39; . DS . &#39;excel&#39;);  
        if($info){  
            $exclePath = $info->getSaveName();  //获取文件名  
            $file_name = ROOT_PATH . &#39;public&#39; . DS . &#39;excel&#39; . DS . $exclePath;   //上传文件的地址  
            $objReader =\PHPExcel_IOFactory::createReader(&#39;Excel5&#39;);  
            $obj_PHPExcel =$objReader->load($file_name, $encode = &#39;utf-8&#39;);  //加载文件内容,编码utf-8  
            echo "<pre class="brush:php;toolbar:false">";  
            $excel_array=$obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式  
            array_shift($excel_array);  //删除第一个数组(标题);  
            
            $data = [];  
            $i=0;  
            foreach($excel_array as $k=>$v) {  
                
                $data[$k][&#39;title&#39;] = $v[1]; 
                $data[$k][&#39;optiona&#39;] = $v[2];   
                $data[$k][&#39;optionb&#39;] = $v[3];   
                $data[$k][&#39;optionc&#39;] = $v[4];   
                $data[$k][&#39;optiond&#39;] = $v[5];   
                $data[$k][&#39;optione&#39;] = $v[6];   
                $data[$k][&#39;optionf&#39;] = $v[7];   
                $data[$k][&#39;time&#39;]    = date(&#39;Y-m-d H:i:s&#39;,time());
                $data[$k][&#39;themeid&#39;] = $themeid;
                $i++;  
            }  
           $success=Db::name(&#39;sleeptopic&#39;)->insertAll($data); //批量插入数据  
           //$i=  
           $error=$i-$success;  
            echo "总{$i}条,成功{$success}条,失败{$error}条。";  
           // Db::name(&#39;t_station&#39;)->insertAll($city); //批量插入数据  
        }else{  
            // 上传失败获取错误信息  
            echo $file->getError();  
        }  
  
    }

Related recommendations:

Tp5 project modification database

Sharing examples of table generation in phpexcel in tp5

The above is the detailed content of TP5 import database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn