PHPExcel 批量导入数据

说明:

前台使用layui插件  非常简单 不会的去看文档 没有任何难度 地址 https://www.layui.com/

使用的PHP框架是CI

HTML页面

//批量导入按钮---------------------------------------------------------

PHP页面

public function import_data()
    {
        try{
            if ($_FILES["file"]["error"] > 0) {
                throw new Exception('');
            }
            //只允许上传excel表格
            $extension = substr(strrchr($_FILES['file']['name'], '.'), 1);
            if ($extension != 'xlsx' && $extension != 'xls') {
                throw new Exception('上传文件格式错误');
            }
            //重命名
            $fileName = 'MATERIAL_PURCHASE'.time().rand(1000000, 9999999);
            //文件存储路径
            $pathName = FCPATH . 'upload/files/' .$fileName .$extension;
            $res = move_uploaded_file($_FILES["file"]['tmp_name'],$pathName);
            //加载PHPExcel
            $this->load->library('PHPExcel/IOFactory');
            if ($res) {
                $type = $extension == 'xlsx' ? 'Excel2007' : 'Excel5';
                $objReader = $this->iofactory->createReader($type);
                $objPHPExcel = $objReader->load($pathName);
                $sheet = $objPHPExcel->getSheet(0);//获取第一个excel里的sheet
                $highestRow = $sheet->getHighestRow();
                if ($highestRow > 10001) {
                    throw new Exception('至多一次导入10000条数据');
                }
                $arr_A = [];
                $arr_B = [];
                for ($i=2; $i<= $highestRow; $i++) {
                    $k_A = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();//获取每一个的值
                    $k_B = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
                    array_push($arr_A, $k_A);
                    array_push($arr_B, $k_B);
                }
                $arr_A_id = [];
                foreach ($arr_A as $key => $value) {
                    $uid = $this->accountModel->getFieldValue('id', ['account_name'=>$value,'is_locked'=>0]);
                    if (!$uid) {
                        throw new Exception('用户'.$value.'已锁定');
                    }
                    array_push($arr_A_id, $uid);
                }
                $data = [];
                for ($i=2; $i<=$highestRow; $i++) {
                    $data[$i-2][0] = $arr_A_id[$i-2];
                    $data[$i-2][1] = $arr_B[$i-2];
                }
                $gmt_create = Carbon::now()->toDateTimeString();
                $gmt_modified = Carbon::now()->toDateTimeString();
                //组装插入语句
                $sql = 'INSERT INTO swap_material_purchase(purchaser, material_code, gmt_create, gmt_modified) VALUES ';
                foreach ($data as $key => $value) {
                    if ($value == end($data)) {
                        $sql .= "("."'$value[0]'".","."'$value[1]'".","."'$gmt_create'".",
"."'$gmt_modified'".");";
                    } else {
                        $sql .= "("."'$value[0]'".","."'$value[1]'".","."'$gmt_create'".",
"."'$gmt_modified'"."),";
                    }
                }
                //插入数据
                $result = $this->materialPurchaseModel->query($sql);
                if ($result) {
                    $msg = '导入成功';
                } else {
                    $msg = '导入失败';
                }
                $data = [
                    'msg' => $msg
                ];
                echo json_encode($data);
            } else {
                throw new Exception('导入失败');
            }
        }catch(Exception $e){
            $msg = $e -> getMessage();
            $data = [
                'msg' => $msg
            ];
            echo json_encode($data);
        }
    }

excel 内容格式如下:

PHPExcel 批量导入数据_第1张图片

效果图:

PHPExcel 批量导入数据_第2张图片

 

你可能感兴趣的:(PHP)