当前位置: 首页>>代码示例>>PHP>>正文


PHP Excel::readSimpleUploadFile2方法代码示例

本文整理汇总了PHP中Excel::readSimpleUploadFile2方法的典型用法代码示例。如果您正苦于以下问题:PHP Excel::readSimpleUploadFile2方法的具体用法?PHP Excel::readSimpleUploadFile2怎么用?PHP Excel::readSimpleUploadFile2使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Excel的用法示例。


在下文中一共展示了Excel::readSimpleUploadFile2方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: import

    /**
     * 导入
     */
    public function import($exam_pid = 0)
    {
        if ($_GET['dl'] == '1') {
            Func::dumpFile('application/vnd.ms-excel', 'file/import_teacher_stundent_template.xlsx', '师生关联模板.xlsx');
            exit;
        }
        if (!$exam_pid) {
            message('参数错误');
        }
        $data = array();
        while (isset($_FILES['file'])) {
            $param = $_POST;
            $col_char = array();
            $rows = Excel::readSimpleUploadFile2($_FILES['file']);
            if (!is_array($rows)) {
                $data['error'] = $rows;
                break;
            }
            $subject_map = array_flip(C('subject'));
            $db = Fn::db();
            $exam_ticket_maprule = ExamModel::get_exam($exam_pid, 'exam_ticket_maprule');
            $sql = "SELECT subject_id, exam_id FROM rd_exam\n                    WHERE exam_pid = {$exam_pid}";
            $subject_exam = $db->fetchPairs($sql);
            if (!is_array($subject_exam)) {
                $data['error'] = '考试期次没有考试学科';
                break;
            }
            $exam_subjectid = array_keys($subject_exam);
            $list = array();
            $subject_key = array();
            foreach ($rows as $k => $row) {
                if ($k == 0) {
                    for ($i = 2; $i <= count($row); $i++) {
                        $subject_id = $subject_map[str_replace("'", "", trim($row[$i]))];
                        if ($subject_id && in_array($subject_id, $exam_subjectid)) {
                            $subject_key[$i] = $subject_id;
                        }
                    }
                } else {
                    $student = array();
                    for ($i = 1; $i <= count($row); $i++) {
                        if ($i == 1) {
                            $exam_ticket = trim($row[$i]);
                            if (!$exam_ticket) {
                                break;
                            }
                            $exam_ticket = exam_ticket_maprule_encode($exam_ticket, $exam_ticket_maprule);
                            $sql = "SELECT uid, school_id FROM rd_student\n                                    WHERE exam_ticket = '{$exam_ticket}'";
                            $student = $db->fetchRow($sql);
                            if (!$student) {
                                break;
                            }
                            $list[$student['uid']]['uid'] = $student['uid'];
                        } else {
                            $ct_name = str_replace("'", "", trim($row[$i]));
                            if (!$ct_name) {
                                continue;
                            }
                            $sql = "SELECT ct_id FROM t_cteacher \n                                    LEFT JOIN t_cteacher_school ON scht_ctid = ct_id\n                                    WHERE scht_schid = {$student['school_id']} \n                                    AND ct_name = '{$ct_name}'";
                            $ct_id = $db->fetchOne($sql);
                            if (!$ct_id) {
                                continue;
                            }
                            $list[$student['uid']]['teacher'][$subject_key[$i]] = $ct_id;
                        }
                    }
                }
            }
            try {
                if (!$db->beginTransaction()) {
                    throw new Exception('开始导入事务处理失败');
                }
                $insert = 0;
                // 导入教师
                foreach ($list as $uid => $row) {
                    foreach ($row['teacher'] as $subject_id => $ct_id) {
                        $bind = array('tstu_ctid' => $ct_id, 'tstu_stuid' => $uid, 'tstu_exampid' => $exam_pid, 'tstu_examid' => $subject_exam[$subject_id], 'tstu_subjectid' => $subject_id);
                        TeacherStudentModel::addTeacherStudent($bind);
                        $insert++;
                    }
                }
                if ($db->commit()) {
                    $data['success'] = <<<EOT
导入Excel文件({$_FILES['file']['name']})成功,共插入{$insert}条师生记录
EOT;
                    admin_log('import', 'teacher_student', $data['success']);
                } else {
                    $err = $db->errorInfo()[2];
                    $db->rollBack();
                    throw new Exception($err);
                }
            } catch (Exception $e) {
                $data['error'] = $e->getMessage();
            }
            break;
        }
        $data['exam_pid'] = $exam_pid;
//.........这里部分代码省略.........
开发者ID:Vincent-Shen,项目名称:origin,代码行数:101,代码来源:teacher_student.php


注:本文中的Excel::readSimpleUploadFile2方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。