tp5-批量导入数据

添加页面

<head>
<style>
 a[class="button-selectimg"],input[type='submit']{text-decoration:none;color:#FAFAFB;padding:8px 16px;border:1px solid #fff;background:#4c95f7; font-size: 16px; }
        input[type='submit']{
          padding: 5px 16px;
        }
        input[id="avatval"]{padding:3px 6px;padding-left:10px;border:1px solid #888;width:300px;height:30px;line-height:30px;background:#FAFAFB;border-radius:2px;}
        input[type='file']{border:0px;display:none;}
    </style>
    <script src="http://localhost/attence/public/static/admin/js/jquery-1.10.2.js"></script>
    <script src="http://localhost/attence/public/static/admin/js/bootstrap.js"></script>
        <script type="text/javascript">
        $(function(){
            $("#avatsel").click(function(){
                $("input[type='file']").trigger('click');
            });
            $("#avatval").click(function(){
                $("input[type='file']").trigger('click');
            });
            $("input[type='file']").change(function(){
                $("#avatval").val($(this).val());
            });
        });
    </script>
</head>
<body> 
<div class="tab-pane fade" id="profile">
                  <div class="form-group" style="width: 60%;padding:10px">
                      <label>请选择导入的Excel文件:</label>
                      <form action="{:url('addstudents')}" method="post" enctype="multipart/form-data">
                        <input type="text" id="avatval" readonly="readonly" style="vertical-align: middle;"/>
                        <input type="file" name="excel" id="avatar"/>
                        <a href="javascript:void(0);" class="button-selectimg" id="avatsel">浏览</a>
                        <input type="submit" name="" id="" value="提交" />
                    </form>
                </div>                    
                </div>

</body>

controller

use PHPExcel_IOFactory;
use PHPExcel;
class Student{

public function addstudents() 
   {
        $request = \think\Request::instance();
        $file = $request->file('excel');

        $save_path = ROOT_PATH . 'uploads/';
        $info = $file->move($save_path);

        $filename=$save_path . DIRECTORY_SEPARATOR . $info->getSaveName();
	    if(file_exists($filename)) 
	    {
          	 	Vendor('phpexcel.PHPExcel');
				$phpExcel = PHPExcel_IOFactory::load($filename);
				 
				$phpExcel->setActiveSheetIndex(0);
				// 获取行数
				$row = $phpExcel->getActiveSheet()->getHighestRow();

	           $sheet = $phpExcel->getActiveSheet(0);//获得sheet

	           $total = 0;

			 	for($j=2;$j<$row;$j++){
			        //从A列读取数据
			        $data = array();
			        $data['sno'] = $sheet->getCell("A$j")->getValue();
			        $data['name'] = $sheet->getCell("B$j")->getValue();
			        $data['phone'] = $sheet->getCell("C$j")->getValue();
			        $data['class'] = $sheet->getCell("D$j")->getValue();
			        $data['academy'] = $sheet->getCell("E$j")->getValue();
			        $data['mac_address'] = $sheet->getCell("F$j")->getValue();
			        $num = db('student')->insert($data);
			        $total += $num;
			    }
				$this->success("成功导入{$total}条数据!",url('studentlist'));				
		}
   
   }
}

在vendor 引入 PHPExcel 即 PHPExcel 官网下载后的classes 文件可更名为phpexcel 复制到 vendor

图片:

猜你喜欢

转载自blog.csdn.net/Tjhfsghbjknjdy/article/details/89053400
今日推荐