您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> php编程 >> 正文

phpexcel导入excel数据使用方法实例

作者:佚名    责任编辑:admin    更新时间:2022-06-22

 phpexcel导入excel数据使用方法,大家参考使用吧

将Excel文件数据进行读取,并且返回错误的信息   代码如下: /**      * 导入商品基本信息      */     public function importProductBasicInfo($data){         include_once 'PHPExcel.php';         include_once 'PHPExcel/IOFactory.php';         include_once 'PHPExcel/Reader/Excel5.php';         // 定义一个错误集合.         $error = array();         $resultInfo = null;         $needNext = true;         //上传文件到服务器指定位置         $fileName = $_FILES["productinfo"]['name'];         $filePath = CBase_Common_UploadPicture::uploadFile($data["productinfo"], 'product');         //如果上传文件成功,就执行导入excel操作         if($filePath == 1) {             $error[1] = "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值";         }else if($filePath == 4){             $error[4] = "没有文件被上传";         }else{             $objReader = PHPExcel_IOFactory::createReader('Excel5');             $objReader->setReadDataOnly(true);             $objPHPExcel = $objReader->load($filePath);             $objWorksheet = $objPHPExcel->getActiveSheet();             $highestRow = $objWorksheet->getHighestRow();             $highestColumn = $objWorksheet->getHighestColumn();             $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);             $colums = array();             $data = array();             $excelAllId = array();             $excelIdRow = array();             $execlAllShopLinkedId = array();             for($i=0;$i<$highestColumnIndex;$i++){                 $cValue = trim($objWorksheet->getCellByColumnAndRow($i,1)->getValue());                 switch ($cValue) {                     case self::PRODUCT_SAP_CODE : $colums[$i] = "sap_code"; break;                     case self::PRODUCT_NAME : $colums[$i] = "pname"; break;                     case self::PRODUCT_GROUP : $colums[$i] = "product_group"; break;                     case self::PRODUCT_BRAND : $colums[$i] = 'product_brand'; break;                     case self::PRODUCT_PROXY_FLAG : $colums[$i] = "product_proxy_flag"; break;                     case self::PRODUCT_BINNING : $colums[$i] = "product_binning"; break;                     case self::PRODUCT_SELL_PICK : $colums[$i] = "product_sell_pick"; break;                     case self::PRODUCT_ATTRIBUTE : $colums[$i] = "product_attribute"; break;                     case self::PRODUCT_SUPPLIER_CODE : $colums[$i] = "vendor_code"; break;                     case self::PRODUCT_SUPPLY_ADDRESS : $colums[$i] = "zzwerk_code"; break;                     case self::PRODUCT_BATCH : $colums[$i] = "zzlgort_code"; break;                     default : $error[3][] = $cValue; break;                 }             }             //检测Excel中的基本信息是否存在             $dataCount = $highestRow - 1;             if(count($colums) == 0) {                 $error[5] = "没有表头";             }             else if(!in_array('sap_code',$colums)){                 $error[2] = "表头中商品SAP编码不存在";             }             else if($dataCount <= 0){                 $error[6] = "Excel文件中没有数据";             }             else if(count($error)==0){                 for ($i=2;$i<=$highestRow;$i++){                     $colkey = array_search('sap_code');                     $shopLinkedIdValue = trim($objWorksheet->getCellByColumnAndRow($colkey,$i)->getValue());                     if(!$shopLinkedIdValue) {                         continue;                     }                     if(in_array($shopLinkedIdValue,$execlAllShopLinkedId)){                         $error[7][$shopLinkedIdValue]['duplicate'] = true;                         $error[7][$shopLinkedIdValue]['excelRow'][] = $i;                         $execlAllShopLinkedId[$i] = $shopLinkedIdValue;                         $error[7][$shopLinkedIdValue]['noId'] = true;                     }else {                         $excelIdRow[$shopLinkedIdValue] = $i;                         $execlAllShopLinkedId[$i] = $shopLinkedIdValue;                     }                 }                 $dealMultiple = ceil($dataCount / 1000);                 $allProduct = array();                 for($i=0;$i<$dealMultiple;$i++){                     $offset = $i*1000+2;                     $max = ($i+1)*1000+1;                     $max = ($max > $dataCount) ? $highestRow : $max;                     $allShopLinkedId = array();                     for($j=$offset;$j<=$max;$j++){                         if($execlAllShopLinkedId[