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[