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

图片:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值