首頁 > 後端開發 > php教程 > phpexcel導入excel資料的範例程式碼

phpexcel導入excel資料的範例程式碼

WBOY
發布: 2016-07-25 08:55:23
原創
770 人瀏覽過
  1. /**

  2. * 匯入商品基本資訊
  3. * edit: bbs.it-home.org
  4. */
  5. public function importProductBasicInfo$data){
  6. include PHPExcel.php';
  7. include_once 'PHPExcel/IOFactory.php';
  8. include_once 'PHPExcel/Reader/Excel5.php';
  9. // 定義一個錯誤集合.
  10. $error = php';
  11. // 定義一個錯誤集合.
  12. $error = php';
  13. // 定義一個錯誤集合.
  14. $error = php'; $resultInfo = null;
  15. $needNext = true;
  16. //上傳檔案到伺服器指定位置
  17. $fileName = $_FILES["productinfo"]['name'];
  18. $filePath = CBase_Common_UploadPicture::uploadFile($data["productinfo"], 'product');
  19. //如果上傳檔案成功,就執行匯入excel操作
  20. if($filePath == 1) {
  21. $error[1] = "上傳的檔案超過了php.ini 中upload_max_filesize 選項限制的值";
  22. }else if($filePath == 4){
  23. $error[4] = "沒有檔案上傳";
  24. }else{
  25. $objReader = PHPExcel_IOFactory::createReader('Excel5');
  26. $objReader->setReadDataOnly(true);
  27. $objPHPExcel = $objRe.> $filePath);
  28. $objWorksheet = $objPHPExcel->getActiveSheet();
  29. $highestRow = $objWorksheet->getHighestRow();
  30. $highestColumn = $objWorksheet-getHighheum highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
  31. $colums = array();
  32. $data = array();
  33. $Id(AllId = array(); $excel( ;
  34. $execlAllShopLinkedId = array();
  35. for($i=0;$i $cValue = trim($objWorksheet->getCellByumnAndRow($i,11) ->getValue());
  36. switch ($cValue) {
  37. case self::PRODUCT_SAP_CODE : $colums[$i] = "sap_code"; break;
  38. case self::PRODUCT_NAME : $colums[ $i] = "pname"; break;
  39. case self::PRODUCT_GROUP : $colums[$i] = "product_group"; break;
  40. case self::PRODUCT_BRAND : $colums[$i] = 'product_brandAND : $colums[$i] = 'product_brandAND : $colums[$i] = 'product_brandAND : $colums[$i] = 'product_brandAND : $colums[$i] = 'product_brand '; break;
  41. case self::PRODUCT_PROXY_FLAG : $colums[$i] = "product_proxy_flag"; break;
  42. case self::PRODUCT_BINNING : $colums[$i] = "product_bin"; > case self::PRODUCT_SELL_PICK : $colums[$i] = "product_sell_pick"; break;
  43. case self::PRODUCT_ATTRIBUTE : $colums[$i] = "product_attribute"; break; case self::PRODUCT_SUPPLY_ADDRESS : $colums[$i] = "zzwerk_code"; break;
  44. case self::DUCT_BATCH PRO. ] = "zzlgort_code"; break;
  45. default : $error[3][] = $cValue; break;
  46. }
  47. }
  48. //偵測Excel中的基本資訊是否存在
  49. $dataCount = $highestRow - 1;
  50. if(count($colums) == 0) {
  51. $error[5] = "沒有表頭";
  52. }
  53. else if(!in_array ('sap_code',$colums)){
  54. $error[2] = "表頭中商品SAP編碼不存在";
  55. }
  56. else if($dataCount $error[6] = "Excel檔案中沒有資料";
  57. }
  58. else if(count($error)==0){
  59. for ($i=2;$i $colkey = array_search('sap_code');
  60. $shopLinkedIdValue = trim($objWorksheet->getCellByColumnAndRow($colkey,$i)->getValue());
  61. if( !$shopLinkedIdValue) {
  62. continue;
  63. }
  64. if(in_array($shopLinkedIdValue,$execlAllShopLinkedId)){
  65. $error[7][$shopLinkedIdValue]['dtrueuplicValue' = true; 🎜> $error[7][$shopLinkedIdValue]['excelRow'][] = $i;
  66. $execlAllShopLinkedId[$i] = $shopLinkedIdValue;
  67. $error[7][$shopLinkedIdValue]['noId '] = true;
  68. }else {
  69. $excelIdRow[$shopLinkedIdValue] = $i;
  70. $execlAllShopLinkedId[$i] = $shopLinkedIdValue;
  71. }
  72. }
  73. }
  74. $dealMultiple = ceil($dataCount / 1000);
  75. $allProduct = array();
  76. for($i=0;$i $offset = $i*1000 2;
  77. $max = ($i 1)*1000 1;
  78. $max = ($max > $dataCount) ? $highestRow : $max;
  79. $allShopLinkedId = array() ;
  80. for($j=$offset;$j if($execlAllShopLinkedId[$j]){
  81. $allShopLinkedId[] = $execlAllShopLinkedId[$j] ;
  82. }
  83. }
  84. // 根據SAP商品編碼查詢在庫中的記錄數.
  85. $dbShopProducts = $this->getShopLinkedByIds($allShopLinkedId);
  86. for($j=$offset;$j $product = array();
  87. for($k=0;$k $tempV = trim($objWorksheet->getCellByColumnAndRow($k,$j)->getValue());
  88. if($tempV && $tempV != '') {
  89. $product[ $colums[$k]] = $tempV;
  90. }
  91. }
  92. //取得檔案中的SAP編碼
  93. $id = $product['sap_code'];
  94. if(! $id){
  95. continue;
  96. }
  97. //偵測商品SAP編碼是否已經存在
  98. if(!in_array($id,$dbShopProducts)){
  99. $allProduct[$id] = $product;
  100. }else{
  101. $error[7][$id]['hasId'] = true;
  102. }
  103. //商品名稱是否為空
  104. if(! isset($product['pname'])){
  105. $error[7][$id]['emptyName'] = true;
  106. }
  107. //商品類目(商品組)是否為空
  108. if(!isset($product['product_group'])){
  109. $error[7][$id]['emptyProductGroup'] = true;
  110. }
  111. //產品層次(品牌)是否為空
  112. if(!isset($product['product_brand'])){
  113. $error[7][$id]['emptyProductBrand'] = true;
  114. }
  115. //經代銷標誌是否為空
  116. if(!isset($product['product_proxy_flag'])){
  117. $error[7][$id]['emptyProductProxyFlag'] = true;
  118. }
  119. //裝箱清單是否為空
  120. if(!isset($product['product_binning'])){
  121. $error[7][$id]['emptyProductBinning'] = true;
  122. }
  123. //先銷後採識別是否為空
  124. if(!isset($product['product_sell_pick'])){
  125. $error[7][$id]['emptyProductSellPick '] = true;
  126. }
  127. //商品屬性是否為空
  128. if(!isset($product['product_attribute'])){
  129. $error[7][$id][ 'emptyProductAttribute'] = true;
  130. }
  131. //供應商編碼是否為空
  132. if(!isset($product['vendor_code'])){
  133. $error[7][$error[7][$ id]['emptyVendorCode'] = true;
  134. }
  135. //供應地點是否為空
  136. if(!isset($product['zzwerk_code'])){
  137. $error[7] [$id]['emptyZzwerkCode'] = true;
  138. }
  139. //庫區是否為空
  140. if(!isset($product['zzlgort_code'])){
  141. $error[ 7][$id]['emptyZzlgortCode'] = true;
  142. }
  143. if(isset($error[7][$id])){
  144. $error[7][$id][ 'excelRow'] = $j;
  145. }
  146. }
  147. }
  148. }
  149. }
  150. $resultInfo['fileName'] = $fileName;
  151. //回傳錯誤訊息
  152. if(count($error)>0){
  153. if(isset($error[1])){
  154. $resultInfo['type'] = 1;
  155. $resultInfo['msg '] = $error[1];
  156. }else if(isset($error[2])){
  157. $resultInfo['type'] = 2;
  158. $resultInfo['msg'] = $error[2];
  159. }else if(isset($error[3])){
  160. $resultInfo['type'] = 3;
  161. $resultInfo['msg'] = '表頭【'.implode(',',$error[3]).'】不存在';
  162. }else if(isset($error[4])){
  163. $resultInfo['type'] = 4;
  164. $resultInfo['msg'] = $error[4];
  165. }else if( isset($error[6])){
  166. $resultInfo['type'] = 6;
  167. $resultInfo['msg'] = $error[6];
  168. }else if(isset($ error[7])){
  169. $excelName = null;
  170. $objPHPWriteExcel = new PHPExcel();
  171. $objPHPWriteExcel->getProperties()->setCreator("yuer")
  172. -ifiedLastMod ("yuer")->setTitle("")->setSubject("")
  173. ->setDescription("")->setKeywords("")->setCategory("" );
  174. $prefix = substr($fileName,0,strrpos($fileName,'.'));
  175. $suffix = substr($fileName,strrpos($fileName,'.'));
  176. $excelName = date("Y_m_d_H_i_s ").'_'.mt_rand(1,99).'_'.$prefix.'ErrorReport'.$suffix;
  177. $excelName = Base_Tool_Pinyin::getPinyin($$excelName) ;
  178. $objPHPWriteExcel-> setActiveSheetIndex(0);
  179. $activeSheet = $objPHPWriteExcel->getActiveSheet();
  180. $activeSheet->setTitle('錯誤報告');
  181. $activeSheet ->setCellValueByygAnd PRODUCT_SAP_CODE);
  182. $activeSheet->setCellValueByColumnAndRow(1,1,'原excel行號');
  183. $activeSheet->setCellValueByColumnAndRow(2,1 ,'第幾行編碼存在重複');
  184. 編碼存在重複'); $activeSheet->setCellValueByColumnAndRow(3,1,self::PRODUCT_NAME);
  185. $activeSheet->setCellValueByColumnAndRow(4,1,self::PRODUCT_GROUP) ;
  186. $activeactiveSheet- :PRODUCT_BRAND);
  187. $activeSheet->setCellValueByColumnAndRow(6,1,self::PRODUCT_PROXY_FLAG);
  188. $activeSheet->setCellValueByColumnAndRow (7,1,1,221,3); (8,1,self::PRODUCT_SELL_PICK);
  189. $activeSheet->setCellValueByColumnAndRow(9,1,self::PRODUCT_ATTRIBUTE) ;
  190. $activeSheet->setCellValueBySUumnAndRow(10,1. 🎜> $activeSheet->setCellValueByColumnAndRow(11,1,self::PRODUCT_SUPPLY_ADDRESS);
  191. $activeSheet->setCellValueByColumnAndRow (12,1,self::PRODUCT_BATCH); '其他原因');
  192. $activeSheet->getColumnDimensionByColumn(0)->setWidth(15) ;
  193. $activeSheet->getColumnDimensionByColumn(1)->setWidth(20);setWidth(20);setWidth(20);
  194. $activeSheet->getColumnDimensionByColumn (3)->setWidth(20);
  195. $activeSheet->getColumnDimensionByColumn(4)->setWidth(20);getColumnDimensionByColumn(5)->setWidth(20) ;
  196. $activeSheet->getColumnDimensionByColumn(6)->setWidth(20);
  197. $activeSheet->getColumnDisionBy.
  198. $activeSheet->getColumnDimensionByColumn (8)->setWidth(20);
  199. $activeSheet->getColumnDimensionByColumn(9)->setWidth(20);
  200. $activeSheet-Bgetumn( (20) ;
  201. $activeSheet->getColumnDimensionByColumn(11)->setWidth(20);
  202. $activeSheet->getColumnDimensionByColumn(12)->setWidth(20);setWidth(20);
  203. $writeExcelIndex = 2;
  204. foreach ($error[7] as $pId=>$pInfo){
  205. if(isset($pInfo['hasId'] ) ){
  206. $activeSheet->setCellValueByColumnAndRow(0,$writeExcelIndex,$pId.'-此供應商編碼已存在');
  207. } else {
  208. $activeactiveSheet->setCellValueByValueBellMo(celdemoneamadmdaldimaad,adimaadsadsad,adakfIncxIncm​​Odemak,ad , $pId);
  209. }
  210. $activeSheet->setCellValueByColumnAndRow(1,$writeExcelIndex,$pInfo['excelRow']);
  211. if(isset($pInfo['duplicate) '])){
  212. $activeSheet->setCellValueByColumnAndRow(2,$writeExcelIndex,$excelIdRow[$pId]);
  213. }
  214. if(isset($pInfo['emptyName'])){
  215. $active(isset($pInfo['emptyName'])){
  216. $active-t setCellValueByColumnAndRow (3,$writeExcelIndex,'-為空');
  217. }
  218. if(isset($pInfo['emptyProductGroup'])){
  219. $activeSheet->setCellValueByColumnAndRow(4,$writeExcelIndex,'-為空');
  220. }
  221. if(isset( $ pInfo['emptyProductBrand'])){
  222. $activeSheet->setCellValueByColumnAndRow(5,$writeExcelIndex,'-為空');
  223. }
  224. if(isset($pInfo['emptyProduct');
  225. }
  226. if(isset($pInfo['emptyProduct]Proxy'plag') {
  227. $activeSheet->setCellValueByColumnAndRow(6,$writeExcelIndex,'-為空');
  228. }
  229. if(isset($pInfo['emptyProductBinning'])){
  230. $SmInfo['emptyProductBinning'])){
  231. $Sheet-> setCellValueByColumnAndRow(7,$writeExcelIndex,'-為空');
  232. }
  233. if(isset($pInfo['emptyProductSellPick'])){
  234. $activeSheet->setCellcelValueByn,Andcowrite( -為空');
  235. }
  236. if(isset($pInfo['emptyProductAttribute'])){
  237. $activeSheet->setCellValueByColumnAndRow(9,$writeExcelIndex,'-為空'); }
  238. if(isset($pInfo['emptyVendorCode'])){
  239. $activeSheet->setCellValueByColumnAndRow(10,$writeExcelIndex,'-為空');
  240. }
  241. }
  242. if(ississet ($pInfo['emptyZzwerkCode'])){
  243. $activeSheet->setCellValueByColumnAndRow(11,$writeExcelIndex,'-為空');
  244. }
  245. if(isset ($pInfopInfo['emptyZnepInfo )){
  246. $activeSheet->setCellValueByColumnAndRow(12,$writeExcelIndex,'-為空');
  247. }
  248. if(isset($pInfo['other']) ){

  249. $activeSheet->setCellValueByColumnAndRow(13,$writeExcelIndex,$pInfp['other']);
  250. }
  251. $writeExcelIndex ;
  252. } $excelPath = FILE_PATH.DS.'feedback'.DS.$excelName;
  253. $objWriter->save( $excelPath);
  254. $resultInfo['type'] = 7;
  255. $resultInfo['msg'] = $fileName."檔案中存在錯誤";
  256. $resultInfo['errorReport'] = $ excelName;
  257. // 日誌操作,暫時空著
  258. }
  259. }else{
  260. // 導入資料
  261. $logIds = '';
  262. $i = 0;
  263. foreach ($allProduct as $pId => $pInfo){
  264. $updateProductSql = '插入yr_product 集合';
  265. if(isset($pInfo['pname']) && trim($pInfo['pname') '])){
  266. $updateProductSql = $ 'pname=''.str_replace(''','''',$pInfo['pname']).'',';
  267. }
  268. //如果SAP不足編碼18位,則用0由左開始補全
  269. if(isset($pInfo['sap_code'])){
  270. if(strlen($pInfo['sap_code'] ) $pInfo['sap_code' ] = str_pad($pInfo['sap_code'], 18, "0", STR_PAD_LEFT);
  271. $updateProductSql = $updateProductSql.'sap_code='' .str_replace(''','''',$pInfo','''',$pInfo[ 'sap_code']).'',';
  272. }
  273. }
  274. if(isset($pInfo['product_group'])) {
  275. $updateProductSql = $updateProductSql.'product_groupl.'product='' .$pInfo['product_group'].'',';
  276. }
  277. if(isset($pInfo['product_brand'])){
  278. $updateProductSql = $updateProductSql.'product_brand=''. $pInfo['product_brand'].'',';
  279. }
  280. if(isset($pInfo['product_proxy_flag'])){
  281. $updateProductSql = $updateProductSql.'' pInfo['product_proxy_flag'].'',';
  282. }
  283. if(isset($pInfo['product_binning'])){
  284. $updateProductSql = $updateProductSInfol.' > $updateProductSql = $updateProductSInfol.'product_bin.' ['product_binning'].'',';
  285. }
  286. if(isset($pInfo['product_sell_pick'])){
  287. $updateProductSql = $updateProductSql.'product''sell_pick[ 'product_sell_pick'].'',';
  288. }
  289. if(isset($pInfo['product_attribute'])){
  290. $ updateProductSql = $updateProductSInfol.'product''f產品屬性'].'',';
  291. }
  292. if(isset($pInfo['vendor_code'])){
  293. $updateProductSql = $updateProductSql.'vendor_code=''.$pInfo[' vendor_code '].'',';
  294. }
  295. if(isset($pInfo['zzwerk_code'])){ $updateProductSql = $updateProductSql.'zzwerk_code=''.$pInfo ' ].'','; }
  296. if(isset($pInfo['zzlgort_code'])){
  297. $updateProductSql = $updateProductSql.'zzlgort_code=''.$pInfo['zzlgort_code'].'';
  298. }
  299. //最終的SQL語句
  300. $result = $this->excuteMultiInsertSql($updateProductSql);
  301. }
  302. $resultInfo['type'] = 8;
  303. $resultInfo['msg'] = "匯入商品基本資訊成功";
  304. /*

  305. * // 日誌操作.
  306. * $content = '大量新商品匯入作業成功:匯入的供應商品編碼有->';
  307. * $logData['content'] = $content.$logIds;
  308. */
  309. }
  310. return $resultInfo;
  311. }
複製程式碼


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板