将数据导【入】【excel表】格式
百度网盘依赖文件下载:
链接:https://pan.baidu.com/s/1L8HBtDtiBisAikQKbRnZIg
提取码:cjje
复制这段内容后打开百度网盘手机App,操作更方便哦
导入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; }