欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

thinkcmf5 Excel文件导入

程序员文章站 2024-02-23 15:14:16
...

以后台Excel导入来添加用户为例:

Controller

//Excel导入 添加用户
public function excelAddUser()
{
    if($this->request->isPost()){
        $excel_name = $this->request->param('excel_name');
        $excel_url = $this->request->param('excel_url');
        if(!empty($excel_name) && !empty($excel_url)){
            Loader::import('PHPExcel.Classes.PHPExcel');
            Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
            Loader::import('PHPExcel.Classes.PHPExcel.Reader.Excel5');
            $filename = ROOT_PATH.'public/upload/'.$excel_url;  //文件路径
            $extension = cmf_get_file_extension($excel_url);    //文件扩展名
            if($extension == 'xlsx'){
                $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
                $objPHPExcel = $objReader->load($filename, $encode = 'utf-8');  //加载文件内容,编码utf-8
            }else if($extension == 'xls'){
                $objReader =\PHPExcel_IOFactory::createReader('Excel5');
                $objPHPExcel = $objReader->load($filename, $encode = 'utf-8');  //加载文件内容,编码utf-8
            }else{
                $this->error('请上传Excel格式的文件!','');
            }
            $excel_array=$objPHPExcel->getsheet(0)->toArray();   //转换为数组格式
            array_shift($excel_array);  //删除第一个数组(标题);

            $data = [];
            foreach($excel_array as $k=>$v) {
                $data[$k]['user_login'] = $v[0];
                $data[$k]['user_pass'] = $v[1];
                $data[$k]['user_email'] = $v[2];
                $data[$k]['create_time'] = time();
                $data[$k]['user_type'] = 3;
                $validate = Loader::validate('Excel');
                if(!$validate->check($data[$k])){
                    $this->error('第'.($k+2).'行'.' '.$validate->getError(),'');
                }
                $data[$k]['user_pass'] = cmf_password($v[1]);
            }
            if(Db::name('user')->insertAll($data)){
                $this->success('导入数据成功!',url('user/AdminIndex/index'));
            } else {
                $this->error('导入数据失败!','');
            }
        }else{
            $this->error('文件导入失败!','');
        }
    }
}

Html

<form method="post" class="form-horizontal js-ajax-form margin-top-20" action="{:url('user/AdminIndex/excelAddUser')}">
   <div class="form-group">
      <label for="input-user_login" class="col-sm-2 control-label"><span class="form-required">*</span>Excel导入用户</label>
      <div class="col-md-6 col-sm-10">
         <!--<input  type="file" name="excel">-->
         <input id="excel-1" type="hidden" name="excel_url" >
         <input  id="excel-1-name" type="text" name="excel_name" title="Excel名称">
         <a href="javascript:uploadOne('Excel上传','#excel-1','file');" class="btn btn-sm btn-default">Excel文件上传</a>
      </div>
   </div>
   <div class="form-group">
      <div class="col-sm-offset-2 col-sm-10">
         <button type="submit" class="btn btn-primary js-ajax-submit">导入</button>
      </div>
   </div>
</form>

Validate

class ExcelValidate extends Validate
{
    protected $rule = [
        'user_login' => 'require|unique:user,user_login',
        'user_pass'  => 'require',
        'user_email' => 'require|email|unique:user,user_email',
    ];
    protected $message = [
        'user_login.require' => '用户不能为空',
        'user_login.unique'  => '用户名已存在',
        'user_pass.require'  => '密码不能为空',
        'user_email.require' => '邮箱不能为空',
        'user_email.email'   => '邮箱不正确',
        'user_email.unique'  => '邮箱已经存在',
    ];
}

应该还有很多可以优化的地方