<button id="g80sy"></button>
  • <abbr id="g80sy"></abbr>
  • <button id="g80sy"></button>
    <rt id="g80sy"><delect id="g80sy"></delect></rt>
  • PhpSpreadsheet導(dǎo)入導(dǎo)出

    程序猿 2022-04-27 23:13:24 2043瀏覽 加載中
    public function export () {
            $id = safeInput('id');
            $user = User::where("school_id",$this->sid)
                ->where('is_check',0)
                ->with(['school','subject'])
                ->select();
            $title = ['手機(jī)號','姓名','老師號','學(xué)校','科系'];
            $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('成功導(dǎo)入' + 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'));


    標(biāo)簽:
    最后修改:2025-03-23 19:21:46

    非特殊說明,本博所有文章均為博主原創(chuàng)。

    主站蜘蛛池模板: 寿宁县| 偏关县| 城口县| 汉源县| 淮安市| 郓城县| 什邡市| 溆浦县| 天等县| 临江市| 历史| 宜宾县| 西盟| 竹山县| 航空| 沧源| 南召县| 宜兰县| 南召县| 荥阳市| 咸阳市| 满城县| 龙江县| 永年县| 肇庆市| 怀远县| 北辰区| 三台县| 永宁县| 博罗县| 富川| 隆安县| 云和县| 上犹县| 阜康市| 广宗县| 克拉玛依市| 吴川市| 洪湖市| 余干县| 广河县|