PhpSpreadsheet导入导出

程序猿 2022-04-27 23:13:24 1418浏览 加载中
public function export () {
        $id = safeInput('id');
        $user = User::where("school_id",$this->sid)
            ->where('is_check',0)
            ->with(['school','subject'])
            ->select();
        $title = ['手机号','姓名','老师号','学校','科系'];
        $sp = new Spreadsheet();
        $sheet = $sp->getActiveSheet();
        foreach ($title as $key => $value) {
            $sheet->setCellValueByColumnAndRow($key + 1,1,$value);
        }
        $row = 2;
        foreach ($user as $key=>$value) {
            $sheet->setCellValueByColumnAndRow(1,$row,$value['username']);
            $sheet->setCellValueByColumnAndRow(2,$row,$value['nick']);
            $sheet->setCellValueByColumnAndRow(3,$row,$value['no']);
            $sheet->setCellValueByColumnAndRow(4,$row,$value["school"]['title']);
            $sheet->setCellValueByColumnAndRow(5,$row,$value['subject']['title']);
            $row ++;
        }
        ob_end_clean();
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="老师列表.xlsx"');
        header('Cache-Control: max-age=0');
        $write = new Xlsx($sp);
        $write->save('php://output');
        $sp->disconnectWorksheets();
        unset($sp);
        exit;
    }
            import2(e) {
                console.log(e);
                console.log(this.$refs.upload1.files);
                var data = new FormData();
                data.append('files',this.$refs.upload1.files[0]);
                $.ajax({
                    url:"{:url('Teacher/import')}",
                    processData: false,
                    contentType: false,
                    data:data,
                    cache: false,
                    type:'post',
                    success:function (res) {
                        if (res.code == 1) {
                            vs.showSuccessMsgCall('成功导入' + res.data.count + '条',function () {
                                location.reload();
                            })
                        }
                    }
                })
            },
public function import() {
        $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
        if (empty($_FILES['files']['tmp_name'])) {
            return  $this->error('文件不存在');
        }
        $file = $_FILES['files']['tmp_name'];
        $result = $reader->load($file);
        $result = $result->getActiveSheet()->toArray();
        $i = 0;
        foreach ($result as $key => $item) {
            if ($key == 0) {
                continue;
            }
            if (!empty($item[0])) {
                $exit = User::where("username",$item[0])->find();
                if (!$exit) {
                    $password = empty($item[3]) ? "ht123456" : $item[3];
                    User::create([
                        "school_id" => $this->sid,
                        "username" => $item[0],
                        "password" => password_hash($password,PASSWORD_DEFAULT),
                        "nick" => $item[1],
                        "no" => $item[2]
                    ]);
                    $i ++;
                }
            }
        }
        return $this->successData(['count'=>$i]);
    }
vs.$refs.upload1.dispatchEvent(new MouseEvent('click'));


标签:
最后修改:2024-04-24 23:14:08

非特殊说明,本博所有文章均为博主原创。