將數據導【入】【excel表】格式

百度網盤依賴文件下載:緩存

連接:https://pan.baidu.com/s/1L8HBtDtiBisAikQKbRnZIg 
提取碼:cjje 
複製這段內容後打開百度網盤手機App,操做更方便哦this

 

導入excel表數據
/*
     * 導入excel表數據
     * */
    public function together(){
        if(IS_POST){
            //dump($_POST);die();

            $file = I('filepath');//上傳的表格附件路徑
            $fileHas = file_exists('.'.$file);
            if(!$fileHas) $this->error('未找到上傳的文件');
            $fieldArr = array(
                'A' => 'xuhao',    //序號
                'B' => 'title',    //名稱
                'C' => 'xinghao',  //型號
                'D' => 'caizhi',   //材質
                'E' => 'danwei',   //單位
                'F' => 'num_1',    //數量
                'G' => 'changjia', //廠家
                'H' => 'beizhu'    //備註
            );
            $data = self::get_excel_content('.'.$file, $fieldArr, 2);
            //dump($data);die();
            $t_num = 0;//檢測是幾個設備
            $yiji_num = 0;//檢測一級目錄
            $erji_num = 0;//檢測二級目錄
            $zong_ej = 1;//二級目錄一個多少個
            $list = array();//數據組裝
            foreach ($data as $k=>$r) {
                $xuhao = $r['xuhao'];
                $jc_1 = strstr($xuhao,'###');
                if($jc_1){
                    $cp_title = substr($xuhao,0,-3);
                    $t_num++;
                    $yiji_num = 0;
                    $zong_ej = 1;
                    $cp_pz = array();
                }else{
                    $jc_2 = strstr($xuhao,'@');
                    if($jc_2){
                        $title = substr($xuhao,0,-1);
                        $yiji_num++;
                        $erji_num = 0;
                        $erji = array();
                    }else{
                        $r['xuhao'] = $zong_ej;
                        $r['price'] = 1;
                        $r['dun'] = 0;
                        $r['spriceBase'] = 0;
                        $r['sprice'] = 0;
                        $zong_ej++;
                        $erji[$erji_num] = $r;
                        $erji_num++;
                    }
                    $cp_pz[$yiji_num]['title'] = $title;
                    $cp_pz[$yiji_num]['erji'] = $erji;
                }
                $list[$t_num]['title'] = $cp_title;
                $list[$t_num]['cp_pz'] = $cp_pz;
            }
            $list = array_values($list);
            dump($list);die();
            $time = time();
            $f_wenjian = 'erpm_xm_pz_xls/pz_xls_'.date('YmdHs',$time).rand(111,999);
            F($f_wenjian,$list);//保存至緩存
            $val['xm_id'] = 1;
            $val['ctime'] = $time;
            $val['f_wenjian'] = $f_wenjian;
            M('erpm_xm_pz')->add($val);

            unlink('.'.$file);
            $this->success('上傳成功');
        }
    }

 

/**
     *  表格文件數據導入(xls格式)
     * @param unknown $xls_name "1.xls"
     * @param unknown $arrayField $arrayField = array("A"=>"index","B"=>"name","C"=>"phone","D"=>"phone2");
     * @param unknown $startLine 開始行
     */
    protected function get_excel_content($xls_name,$arrayField, $startLine = 2){
        vendor('PHPExcel.PHPExcel');
        $extension = strtolower( pathinfo($xls_name, PATHINFO_EXTENSION) );
        if ($extension =='xlsx') {
            $objReader = new \PHPExcel_Reader_Excel2007();
            $objExcel = $objReader ->load($xls_name);
        } else if ($extension =='xls') {
            $objReader = new \PHPExcel_Reader_Excel5();
            $objExcel = $objReader ->load($xls_name);
        } else if ($extension=='csv') {
            $PHPReader = new \PHPExcel_Reader_CSV();
            //默認輸入字符集
            $PHPReader->setInputEncoding('GBK');
            //默認的分隔符
            $PHPReader->setDelimiter(',');
            //載入文件
            $objExcel = $PHPReader->load($xls_name);
        }
        /*
        $reader = \PHPExcel_IOFactory::createReader("Excel5");
        $excel = $reader->load($xls_name);
        */
        //getSheet獲取第一張表
        $sheet = $objExcel->getSheet(0);
        $height = $sheet->getHighestRow();
        $array = array();
        for ($j = $startLine; $j <= $height; $j++) {  //$j表示從第幾行開始讀取數據
            $one = array();
            foreach($arrayField as $k => $v){
                $one[$v] = (string)$sheet->getCell($k.$j)->getValue();
            }
            array_push($array, $one);
        }
        return $array;
    }
相關文章
相關標籤/搜索