thinkphp5+PHPexcel导入导出

网上很多资料都是php源码的,因为项目是tp5的,所以整合了下导入导出的tp5版本,做下笔记以备用。

后台样式:

thinkphp5+PHPexcel导入导出_第1张图片

导出样式:

材料准备:PHPExcel类库:https://pan.baidu.com/s/1oTMU2qj6OWE4Svdf_b0i7g 下载解压至tp5 vendor目录下即可

头部use:

use PHPExcel\PHPExcel;

导出:

getActiveSheet();
	        $PHPSheet->setTitle(date("Ymd",time()));
	        $PHPSheet->
	        setCellValue("A1","微信识别符")->
	        setCellValue("B1","宝贝姓名")->
	        setCellValue("C1","电话")->
	        setCellValue("D1","出生日期")->
	        setCellValue("E1","是否可以通过电话号码加微信")->
	        setCellValue("F1","期待预约时间(星期)")->
	        setCellValue("G1","创建时间");

	        $baoming_list = Db::name("guestbook")->order('create_time desc')->select();
	        $baoming_count = Db::name("guestbook")->count();
	        if(isset($baoming_list) && !empty($baoming_list)){
	            for($i=0;$i<$baoming_count;$i++){
	                $PHPSheet->
	                setCellValue("A".($i+2),$baoming_list[$i]['openid'])->
	                setCellValue("B".($i+2),$baoming_list[$i]['baby_name'])->
	                setCellValue("C".($i+2),$baoming_list[$i]['mobile'])->
	                setCellValue("D".($i+2),$baoming_list[$i]['birthday'])->
	                setCellValue("E".($i+2),$baoming_list[$i]['allow_add'])->
	                setCellValue("F".($i+2),$baoming_list[$i]['yuyue_time'])->
	                setCellValue("G".($i+2),date("Y-m-d",$baoming_list[$i]['create_time']));
	            }
	        }
	        $PHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(25);//设置宽度
	        $PHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
	        $PHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
	        $PHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
	        $PHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(30);
	        $PHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(30);
	        $PHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);

	        $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,"Excel2007");
	        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器输出07Excel文件
	        //header('Content-Type:application/vnd.ms-excel');//告诉浏览器将要输出Excel03版本文件
	        header('Content-Disposition: attachment;filename="01simple.xlsx"');//告诉浏览器输出浏览器名称
	        header('Cache-Control: max-age=0');//禁止缓存
	        $PHPWriter->save("php://output");
	    }
?>

导入:

file('student_excel');
        $info = $file->validate(['ext' => 'xlsx,xls'])->move(ROOT_PATH . 'excel');
        if(empty($info)){
            return $this->success('导入数据失败', url('admin/user/input_student'));
        }
        //文件名
        $exclePath = $info->getSaveName();
        //上传文件的地址
        $filename = ROOT_PATH . 'excel' . DS . $exclePath;
        //判断截取文件
        $extension = strtolower( pathinfo($filename, PATHINFO_EXTENSION) );
        //区分上传文件格式
        if($extension == 'xlsx') {
            $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
            $objPHPExcel = $objReader->load($filename, $encode = 'utf-8');
        }else if($extension == 'xls'){
            $objReader =\PHPExcel_IOFactory::createReader('Excel5');
            $objPHPExcel = $objReader->load($filename, $encode = 'utf-8');
        }

        $excel_array = $objPHPExcel->getsheet(0)->toArray();   //转换为数组格式
        array_shift($excel_array);  //删除第一个数组(标题);

        $data = [];
        $num = 0;
        foreach($excel_array as $k=>$value) {
            $data[$k]['stu_name'] = $value[0];
            $data[$k]['stu_sex'] = $value[1];
            $data[$k]['stu_age'] = $value[2];
            $data[$k]['stu_username'] = $value[3];
            $data[$k]['stu_school'] = $value[4];
            $data[$k]['stu_grade'] = $value[5];
            $data[$k]['stu_major'] = $value[6];
            $data[$k]['stu_future'] = $value[7]; 
            $data[$k]['stu_tel'] = $value[8];
            $data[$k]['stu_address'] = $value[9];
            $data[$k]['stu_type'] = $value[10];
            $data[$k]['stu_work'] = $value[11];
            $data[$k]['stu_pwd'] = md5(md5('123456')); 
            $data[$k]['create_time'] = time();

            $check_user = Db::name('stud_member')->where('stu_username',$value[3])->find();
            if(isset($check_user) && !empty($check_user)){
                //数据重复,剔除
                $num++;
            }else{
                Db::name('stud_member')->insert($data[$k]); 
            }
        }
        return $this->success('批量导入成功,其中剔除重复数据'.$num.'条', url('admin/user/student_index'));
    }
?>

这是我两个项目的导入导出合并代码片段,导入支持去重,可以直接反馈是否有重复导入数据。

你可能感兴趣的:(thinkphp5开发总结)