phpexcel 导入文件

public function getSheetData($inputFileName)
    {
        vendor("PhpExcel.PHPExcel");
        $objPHPExcel = new \PHPExcel();
        //$inputFileName "D:/360/test1.xlsx"
        $objPHPExcel = \PHPExcel_IOFactory::load($inputFileName);
        $sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
        return $sheetData;
    }
    //导入产品分类
    public function importProductKind($inputFileName)
    {
        $data = $this->getSheetData($inputFileName);
        $kinds = array();
        foreach ($data as $k => $v) {
            if ($k > 1 && !empty($v['A']) && !empty($v['B'])) {
                $temp = array();
                $temp['pd_kind_id'] = $v['A'];
                $temp['pd_kind_name'] = $v['B'];
                $temp['pd_kind_pid'] = $v['C'];
                $temp['create_time'] = date('Y-m-d H:i:s');
                array_push($kinds, $temp);
            }
        }
        $ids = array_column($kinds, "pd_kind_id");
        if (count($ids) == 0) {
            E("未获取到文件数据或文件内容为空", 1005);
        }
        $where = array();
        $where['pd_kind_id'] = array('in', $ids);
        $where['del_flag'] = Constants::DEL_NO;
        $repeatIds = M('ProductKind')->where($where)->select();
        if (empty($repeatIds)) {
            M()->startTrans();
            try {
                $res = M('ProductKind')->addAll($kinds);
                $data = M('ProductKind')->where(array('del_flag' => 0))->select();
                $list = array();
                $list = $this->recursionFunc($list, $data);
                $id = $this->updateKindPath($list);
                M()->commit();
                return $id;
            } catch (\Exception $e) {
                M()->rollback();
                $errStr = $e->getMessage();
                if (empty($errStr)) {
                    $errStr = "插入失败,可能是数据不规范";
                }
                E($errStr, 1005);
            }

        } else {
            $repeatStr = "";
            foreach ($repeatIds as $k => $v) {
                $repeatStr .= "," . $v['pd_kind_id'];
            }
            $repeatStr = substr($repeatStr, 1);
            E("id:'" . $repeatStr . "'已存在", 1005);
        }


    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值