ecmall导入Excel

来源:互联网 发布:淘宝怎么养小号 编辑:程序博客网 时间:2024/06/15 22:03
function commImport(){    if (IS_POST) {        $name = $_POST['name'];        if (empty($name)) {            $this->show_warning('请选择上传文件目录');            return;        }        $file = $_FILES['f'];        if ($file['error'] == UPLOAD_ERR_NO_FILE) // 没有文件被上传        {            $this->show_warning('请选择文件');            return;        }        //判断是否符合文件格式        $allowedExts = array("xls", "xlsx", "csv");        $temp = explode(".", $file["name"]);        $extension = end($temp);     // 获取文件后缀名        if (!in_array($extension, $allowedExts)) {            $this->show_warning('文件类型不允许,请重新操作');            return;        }        import('uploader.lib');             //导入上传类        import('image.func');        $uploader = new Uploader();        $uploader->addFile($_FILES['f']);   //上传excel        if (!$uploader->file_info()) {            $this->show_warning('请选择文件');            exit();        }        // 保存路径        $filename = local_date('YmdHis' . gmtime(), '');        $filename .= "评论导入模板." . $extension;        $filePath = "data/$name/" . $filename;        // 指定保存位置的根目录        $uploader->root_dir(ROOT_PATH);        // 上传        $file_path = $uploader->save($filePath);  //保存到指定目录        include_once(ROOT_PATH . '/excel/toexcel/PHPExcel/IOFactory.php');        include_once(ROOT_PATH . '/excel/toexcel/PHPExcel/Reader/Excel5.php');        include_once(ROOT_PATH . '/excel/toexcel/PHPExcel.php');        $PHPExcel = PHPExcel_IOFactory::load(ROOT_PATH.'/'.$file_path);        $date = new PHPExcel_Shared_Date();        //读取excel文件中的第一个工作表        $sheet = $PHPExcel->getSheet(0);        //取得最大的行号        $allRow = $sheet->getHighestRow();        $count = 0;        $error_note = '';        for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {            $userId = $PHPExcel->getActiveSheet()->getCell('A' . $currentRow)->getValue();    // 用户id            $code = $PHPExcel->getActiveSheet()->getCell('B' . $currentRow)->getValue();    // 门店编号            $commentStar = $PHPExcel->getActiveSheet()->getCell('C' . $currentRow)->getValue();    // 星数            $sctag = $PHPExcel->getActiveSheet()->getCell('D' . $currentRow)->getValue();        // 标签            $img1 = $PHPExcel->getActiveSheet()->getCell('E' . $currentRow)->getValue();       // 图片1            $img2 = $PHPExcel->getActiveSheet()->getCell('F' . $currentRow)->getValue();       // 图片2            $img3 = $PHPExcel->getActiveSheet()->getCell('G' . $currentRow)->getValue();       // 图片3            $img4 = $PHPExcel->getActiveSheet()->getCell('H' . $currentRow)->getValue();       // 图片4            $sex = $PHPExcel->getActiveSheet()->getCell('I' . $currentRow)->getValue();         // 内容            dump($userId);            if (empty($userId)) {                $error_note .= '第' . $currentRow . '行,导入失败:' . '用户不存在;' . "</br>";                continue;            }        }        if ($error_note) {            $this->show_warning($error_note);            return;        } else {            $this->show_warning('导入成功');            return;            // $this->success('导入成功' . $count . '条数据', U('User/index'));        }    } else {        $this->display('storecomment.import.html');    }}
原创粉丝点击