Rumah >php教程 >php手册 >ExecelPHP类 简单导入功能 tp3.1.3

ExecelPHP类 简单导入功能 tp3.1.3

WBOY
WBOYasal
2016-06-07 11:39:111315semak imbas

ExecelPHP类 简单导入功能 tp3.1.3
public function import($file =''){<br>         <br>         vendor('Excel.PHPExcel');<br>         vendor('Excel.PHPExcel.IOFactory');<br>         vendor('Excel.PHPExcel.Reader.Excel5');<br>                 <br>         $objReader = new PHPExcel_Reader_Excel5;<br>         <br>         $objPHPExcel = $objReader->load($file);<br>                 <br>         $sheet = $objPHPExcel->getSheet(0);<br>         $highestRow = $sheet->getHighestRow();<br>         $highestColumn = $sheet->getHighestColumn();<br>         <br>         $count = 0;<br>         $s_count = 0;<br>         $t_count = 0;<br>         $b_count = 0;<br>         $m_count = 0;<br>         $string = '';<br>         <br>         for($j =2; $j              <br>             $haipin_id = trim($objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue()); //海品编号<br>             $goods_name = trim($objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue()); //商品名称<br>             $common_name = trim($objPHPExcel->getActiveSheet()->getCell("E".$j)->getValue()); //通用名<br>             $license_number = trim($objPHPExcel->getActiveSheet()->getCell("F".$j)->getValue()); //批准文号<br>             $spec = trim($objPHPExcel->getActiveSheet()->getCell("G".$j)->getValue()); //规格<br>             $manufacturer = trim($objPHPExcel->getActiveSheet()->getCell("Q".$j)->getValue()); //厂家<br>             $brand = trim($objPHPExcel->getActiveSheet()->getCell("R".$j)->getValue()); //品牌<br>             $type = trim($objPHPExcel->getActiveSheet()->getCell("S".$j)->getValue()); //类型<br>             $cate = trim($objPHPExcel->getActiveSheet()->getCell("U".$j)->getValue()); //分类<br>             $keywords = trim($objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue()); //关键词<br>             $effect = trim($objPHPExcel->getActiveSheet()->getCell("D".$j)->getValue()); //功效<br>             $is_drug = trim($objPHPExcel->getActiveSheet()->getCell("H".$j)->getValue()); //处方药<br>             $drug_description = trim($objPHPExcel->getActiveSheet()->getCell("I".$j)->getValue()); //处方药说明<br>             $form = trim($objPHPExcel->getActiveSheet()->getCell("J".$j)->getValue()); //剂型<br>             $material = trim($objPHPExcel->getActiveSheet()->getCell("L".$j)->getValue()); //原料<br>             $leading_effect = trim($objPHPExcel->getActiveSheet()->getCell("M".$j)->getValue()); //作用<br>             $crowd = trim($objPHPExcel->getActiveSheet()->getCell("N".$j)->getValue()); //适用人群<br>             $instructions = trim($objPHPExcel->getActiveSheet()->getCell("P".$j)->getValue()); //用法用量<br>             $is_import = trim($objPHPExcel->getActiveSheet()->getCell("T".$j)->getValue()); //是否进口<br>             $department = trim($objPHPExcel->getActiveSheet()->getCell("V".$j)->getValue()); //部门名称<br>             $haipin = trim($objPHPExcel->getActiveSheet()->getCell("W".$j)->getValue());  //海品药店<br>             $zhimeilan = trim($objPHPExcel->getActiveSheet()->getCell("X".$j)->getValue());  //植玫兰<br>             $off_line = trim($objPHPExcel->getActiveSheet()->getCell("Y".$j)->getValue()); //线下<br>             <br>             $goods_data = array();<br>             $empty_field = 0;<br>             <br>             if(empty($cate)){<br>                 continue;<br>             }else{<br>     <br>                 $cate_id = A('Admin/Cate')->check_cate_exists($cate);<br>                 <br>                 if(!$cate_id){<br>                     $string .= $cate. "分类不存在分类列表,请先输入.<br> ";<br>                     continue;<br>                 }else{<br>                     $goods_data['cate_id'] = (int)$cate_id;<br>                 }<br>             }<br>             <br>             if(empty($department)){<br>                 $goods_data['department_id'] = 0;<br>             }else{<br>                 <br>                 $department_id = $this->check_department_exists($department);<br>                 <br>                 if(!$department_id){<br>                     $goods_data['department_id'] = 0;<br>                 }else{<br>                     $goods_data['department_id'] = (int)$department_id;<br>                 }<br>             }<br>             <br>             if(empty($brand)){<br>                 //continue;<br>                 $goods_data['brand_id'] = 0;<br>                 $empty_field = 1;<br>                 <br>             }else{<br>                 <br>                 $brand_id = A('Admin/Brand')->check_brand_exists($brand);<br>                 <br>                 if(!$brand_id){<br>                     $data = array(<br>                         'brand_name' => $brand,<br>                     );<br>                     $insert_brand_id = M('Brand')->add($data);<br>                     if($insert_brand_id !== false){<br>                         $b_count ++;<br>                         $goods_data['brand_id'] = $insert_brand_id;<br>                     }else{<br>                         continue;<br>                     }<br>                 }else{<br>                     $goods_data['brand_id'] = $brand_id;<br>                 }<br>             }<br>             <br>             if(empty($type)){<br>                 //continue;<br>                 $goods_data['type_id'] = 0;<br>             }else{<br>                 <br>                 $type_id = A('Admin/GoodsType')->check_type_exists($type);<br>                 <br>                 if(!$type_id){<br>                     $data = array(<br>                         'type_name' => $type,<br>                     );<br>                     $insert_type_id = M('Goods_type')->add($data);<br>                     if($insert_type_id !== false){<br>                         $t_count ++;<br>                         $goods_data['type_id'] = $insert_type_id;<br>                     }else{<br>                         continue;<br>                     }<br>                 }else{<br>                     $goods_data['type_id'] = $type_id;<br>                 }<br>             }<br>             <br>             if(empty($spec)){<br>                 //continue;<br>                 $goods_data['spec_id'] = 0;<br>                 $empty_field = 1;<br>             }else{<br>                 <br>                 $spec_id = A('Admin/GoodsSpec')->check_goods_spec_exists($spec);<br>                 <br>                 if(!$spec_id){<br>                     $data = array(<br>                         'spec_name' => $spec,<br>                     );<br>                     $insert_spec_id = M('Goods_spec')->add($data);<br>                     if($insert_spec_id !== false){<br>                         $s_count ++;<br>                         $goods_data['spec_id'] = $insert_spec_id;<br>                     }else{<br>                         continue;<br>                     }<br>                 }else{<br>                     $goods_data['spec_id'] = $spec_id;<br>                 }<br>                 <br>             }<br>             <br>             if(empty($manufacturer)){<br>                 //continue;<br>                 $goods_data['manufacturer_id'] = 0;<br>                 $empty_field = 1;<br>             }else{<br>                 <br>                 $manufacturer_id = A('Admin/Manufacturer')->check_manufacturer_exists($manufacturer);<br>                 <br>                 if(!$manufacturer_id){<br>                     $data = array(<br>                         'manufacturer_name' => $manufacturer,<br>                     );<br>                     $insert_manufacturer_id = M('Manufacturer')->add($data);<br>                     if($insert_manufacturer_id !== false){<br>                         $m_count ++;<br>                         $goods_data['manufacturer_id'] = $insert_manufacturer_id;<br>                     }else{<br>                         continue;<br>                     }<br>                 }else{<br>                     $goods_data['manufacturer_id'] = $manufacturer_id;<br>                 }<br>             }<br>             <br>             $goods_data['is_drug'] = array_search(strtoupper($is_drug), C(IS_DRUG));<br>             if(empty($goods_data['is_drug'])){<br>                 $goods_data['is_drug'] = 0;<br>                 $empty_field = 1;<br>             }<br> <br>             $goods_id = $this->is_exist_goods($goods_name, $goods_data['brand_id'], $goods_data['is_drug'], $goods_data['spec_id'], $goods_data['manufacturer_id']);<br>                     <br>             if(!$goods_id){<br>                 <br>                 $goods_data['haipin_id'] = $haipin_id;<br>                 $goods_data['goods_name'] = $goods_name;<br>                 $goods_data['common_name'] = $common_name;<br>                 $goods_data['license_number'] = $license_number;<br>                 $goods_data['keywords'] = $keywords;<br>                 $goods_data['effect'] = $effect;<br>                 //$goods_data['is_drug'] = array_search(strtoupper($is_drug), C(IS_DRUG));<br>                 $goods_data['drug_description'] = $drug_description;<br>                 $goods_data['form'] = $form;<br>                 $goods_data['material'] = $material;<br>                 $goods_data['leading_effect'] = $leading_effect;<br>                 $goods_data['crowd'] = $crowd;<br>                 $goods_data['instructions'] = $instructions;<br>                 $goods_data['is_import'] = empty($is_import)? 0: 1;<br>                 $goods_data['add_time'] = time();<br>                 $goods_data['user_id'] = session('uid');<br> <br>                 $goods_model = M('Goods');<br>                 $goods_model->startTrans();<br>                 $goods_id = $goods_model->add($goods_data); <br>                 if($goods_id !== false){<br>                     <br>                     $g_new_goods_sn = '';<br>                     $g_new_goods_sn .= str_pad((string)$goods_data['cate_id'], 1, "0" ,STR_PAD_LEFT);<br>                     $g_new_goods_sn .= str_pad((string)$goods_id, 7, "0" ,STR_PAD_LEFT);<br>                     $goods_model->save(array('new_goods_sn'=>$g_new_goods_sn, 'goods_id'=>$goods_id));<br>                     <br>                     //处理网站和商品关系<br>                     if($haipin){<br>                         $s_data = array(<br>                             'site_id' =>2,<br>                             'goods_id' =>$goods_id,<br>                             'type' =>1,<br>                         );<br>                         $this->deal_with_goods_to_site($s_data);<br>                     }<br>                     if($zhimeilan){<br>                         $s_data = array(<br>                             'site_id' =>3,<br>                             'goods_id' =>$goods_id,<br>                             'type' =>1,<br>                         );<br>                         $this->deal_with_goods_to_site($s_data);<br>                     }<br>                     if($off_line){<br>                         $s_data = array(<br>                             'site_id' =>4,<br>                             'goods_id' =>$goods_id,<br>                             'type' =>1,<br>                         );<br>                         $this->deal_with_goods_to_site($s_data);<br>                     }<br>                     <br>                     if(!$empty_field){<br>                         $goods_sn = '';<br>                         $cate_res = M('Cate')->field('alias_name')->where(array('cate_name'=>$cate))->find();<br>                         $goods_sn .= substr($cate_res['alias_name'], 0, 2);<br>                         $goods_sn .= str_pad((string)$goods_data['brand_id'], 5, "0" ,STR_PAD_LEFT);<br>                         $goods_sn .= str_pad((string)$goods_data['is_drug'], 3, "0" ,STR_PAD_LEFT);<br>                         $goods_sn .= str_pad((string)$goods_id, 5, "0" ,STR_PAD_LEFT);<br>                         $goods_sn .= str_pad((string)$goods_data['manufacturer_id'], 5, "0" ,STR_PAD_LEFT);<br>                         $goods_sn .= str_pad((string)$goods_data['spec_id'], 5, "0" ,STR_PAD_LEFT);<br>                         $data = array(<br>                             'goods_id' => $goods_id,<br>                             'goods_sn' => $goods_sn,<br>                         );<br>                         $res = $goods_model->save($data);<br>                         <br>                         if($res !== false){<br>                             $goods_model->commit();<br>                         }else{<br>                             $goods_model->rollback();    <br>                         }<br>                         <br>                     }else{<br>                         $goods_model->commit();<br>                     }<br>                     <br>                     $count ++;<br>                     <br>                 }else{<br>                     $goods_model->rollback();<br>                     $string .= $haipin_id.'/'.$goods_name. "录入失败.<br> ";<br>                 }<br>                 <br>             }else{<br>                 $string .= $haipin_id.'/'.$goods_name. "已经存在.<br> ";<br>             }<br>         }<br>         <br>         $o_string = '';<br>         $o_string .= '本次操作录入'. $count. '个产品, ';<br>         $o_string .= '录入'. $s_count. '个规格, ';<br>         $o_string .= '录入'. $t_count. '个类型, ';<br>         $o_string .= '录入'. $b_count. '个品牌, ';<br>         $o_string .= '录入'. $m_count. "个厂家.<br><br> ";<br>         <br>         return '<div>'.$o_string . $string.'</div>';<br>     }

AD:真正免费,域名+虚机+企业邮箱=0元

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn