Maison >développement back-end >tutoriel php >phpexcel导入excel数据的示例代码

phpexcel导入excel数据的示例代码

WBOY
WBOYoriginal
2016-07-25 08:55:23775parcourir
  1. /**

  2. * 导入商品基本信息
  3. * edit: bbs.it-home.org
  4. */
  5. public function importProductBasicInfo($data){
  6. include_once 'PHPExcel.php';
  7. include_once 'PHPExcel/IOFactory.php';
  8. include_once 'PHPExcel/Reader/Excel5.php';
  9. // 定义一个错误集合.
  10. $error = array();
  11. $resultInfo = null;
  12. $needNext = true;
  13. //上传文件到服务器指定位置
  14. $fileName = $_FILES["productinfo"]['name'];
  15. $filePath = CBase_Common_UploadPicture::uploadFile($data["productinfo"], 'product');
  16. //如果上传文件成功,就执行导入excel操作
  17. if($filePath == 1) {
  18. $error[1] = "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值";
  19. }else if($filePath == 4){
  20. $error[4] = "没有文件被上传";
  21. }else{
  22. $objReader = PHPExcel_IOFactory::createReader('Excel5');
  23. $objReader->setReadDataOnly(true);
  24. $objPHPExcel = $objReader->load($filePath);
  25. $objWorksheet = $objPHPExcel->getActiveSheet();
  26. $highestRow = $objWorksheet->getHighestRow();
  27. $highestColumn = $objWorksheet->getHighestColumn();
  28. $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
  29. $colums = array();
  30. $data = array();
  31. $excelAllId = array();
  32. $excelIdRow = array();
  33. $execlAllShopLinkedId = array();
  34. for($i=0;$i $cValue = trim($objWorksheet->getCellByColumnAndRow($i,1)->getValue());
  35. switch ($cValue) {
  36. case self::PRODUCT_SAP_CODE : $colums[$i] = "sap_code"; break;
  37. case self::PRODUCT_NAME : $colums[$i] = "pname"; break;
  38. case self::PRODUCT_GROUP : $colums[$i] = "product_group"; break;
  39. case self::PRODUCT_BRAND : $colums[$i] = 'product_brand'; break;
  40. case self::PRODUCT_PROXY_FLAG : $colums[$i] = "product_proxy_flag"; break;
  41. case self::PRODUCT_BINNING : $colums[$i] = "product_binning"; break;
  42. case self::PRODUCT_SELL_PICK : $colums[$i] = "product_sell_pick"; break;
  43. case self::PRODUCT_ATTRIBUTE : $colums[$i] = "product_attribute"; break;
  44. case self::PRODUCT_SUPPLIER_CODE : $colums[$i] = "vendor_code"; break;
  45. case self::PRODUCT_SUPPLY_ADDRESS : $colums[$i] = "zzwerk_code"; break;
  46. case self::PRODUCT_BATCH : $colums[$i] = "zzlgort_code"; break;
  47. default : $error[3][] = $cValue; break;
  48. }
  49. }
  50. //检测Excel中的基本信息是否存在
  51. $dataCount = $highestRow - 1;
  52. if(count($colums) == 0) {
  53. $error[5] = "没有表头";
  54. }
  55. else if(!in_array('sap_code',$colums)){
  56. $error[2] = "表头中商品SAP编码不存在";
  57. }
  58. else if($dataCount $error[6] = "Excel文件中没有数据";
  59. }
  60. else if(count($error)==0){
  61. for ($i=2;$i $colkey = array_search('sap_code');
  62. $shopLinkedIdValue = trim($objWorksheet->getCellByColumnAndRow($colkey,$i)->getValue());
  63. if(!$shopLinkedIdValue) {
  64. continue;
  65. }
  66. if(in_array($shopLinkedIdValue,$execlAllShopLinkedId)){
  67. $error[7][$shopLinkedIdValue]['duplicate'] = true;
  68. $error[7][$shopLinkedIdValue]['excelRow'][] = $i;
  69. $execlAllShopLinkedId[$i] = $shopLinkedIdValue;
  70. $error[7][$shopLinkedIdValue]['noId'] = true;
  71. }else {
  72. $excelIdRow[$shopLinkedIdValue] = $i;
  73. $execlAllShopLinkedId[$i] = $shopLinkedIdValue;
  74. }
  75. }
  76. $dealMultiple = ceil($dataCount / 1000);
  77. $allProduct = array();
  78. for($i=0;$i $offset = $i*1000+2;
  79. $max = ($i+1)*1000+1;
  80. $max = ($max > $dataCount) ? $highestRow : $max;
  81. $allShopLinkedId = array();
  82. for($j=$offset;$j if($execlAllShopLinkedId[$j]){
  83. $allShopLinkedId[] = $execlAllShopLinkedId[$j];
  84. }
  85. }
  86. // 根据SAP商品编码查询在库中的记录数.
  87. $dbShopProducts = $this->getShopLinkedByIds($allShopLinkedId);
  88. for($j=$offset;$j $product = array();

  89. for($k=0;$k $tempV = trim($objWorksheet->getCellByColumnAndRow($k,$j)->getValue());
  90. if($tempV && $tempV != '') {
  91. $product[$colums[$k]] = $tempV;
  92. }
  93. }
  94. //获取文件中的SAP编码
  95. $id = $product['sap_code'];
  96. if(!$id){
  97. continue;
  98. }
  99. //检测商品SAP编码是否已经存在
  100. if(!in_array($id,$dbShopProducts)){
  101. $allProduct[$id] = $product;
  102. }else{
  103. $error[7][$id]['hasId'] = true;
  104. }
  105. //商品名是否为空
  106. if(!isset($product['pname'])){
  107. $error[7][$id]['emptyName'] = true;
  108. }
  109. //商品类目(商品组)是否为空
  110. if(!isset($product['product_group'])){
  111. $error[7][$id]['emptyProductGroup'] = true;
  112. }
  113. //产品层次(品牌)是否为空
  114. if(!isset($product['product_brand'])){
  115. $error[7][$id]['emptyProductBrand'] = true;
  116. }
  117. //经代销标志是否为空
  118. if(!isset($product['product_proxy_flag'])){
  119. $error[7][$id]['emptyProductProxyFlag'] = true;
  120. }
  121. //装箱清单是否为空
  122. if(!isset($product['product_binning'])){
  123. $error[7][$id]['emptyProductBinning'] = true;
  124. }
  125. //先销后采标识是否为空
  126. if(!isset($product['product_sell_pick'])){
  127. $error[7][$id]['emptyProductSellPick'] = true;
  128. }
  129. //商品属性是否为空
  130. if(!isset($product['product_attribute'])){
  131. $error[7][$id]['emptyProductAttribute'] = true;
  132. }
  133. //供应商编码是否为空
  134. if(!isset($product['vendor_code'])){
  135. $error[7][$id]['emptyVendorCode'] = true;
  136. }
  137. //供应地点是否为空
  138. if(!isset($product['zzwerk_code'])){
  139. $error[7][$id]['emptyZzwerkCode'] = true;
  140. }
  141. //库区是否为空
  142. if(!isset($product['zzlgort_code'])){
  143. $error[7][$id]['emptyZzlgortCode'] = true;
  144. }
  145. if(isset($error[7][$id])){
  146. $error[7][$id]['excelRow'] = $j;
  147. }
  148. }
  149. }
  150. }
  151. }
  152. $resultInfo['fileName'] = $fileName;
  153. //返回错误信息
  154. if(count($error)>0){
  155. if(isset($error[1])){
  156. $resultInfo['type'] = 1;
  157. $resultInfo['msg'] = $error[1];
  158. }else if(isset($error[2])){
  159. $resultInfo['type'] = 2;
  160. $resultInfo['msg'] = $error[2];
  161. }else if(isset($error[3])){
  162. $resultInfo['type'] = 3;
  163. $resultInfo['msg'] = '表头【'.implode(',',$error[3]).'】不存在';
  164. }else if(isset($error[4])){
  165. $resultInfo['type'] = 4;
  166. $resultInfo['msg'] = $error[4];
  167. }else if(isset($error[6])){
  168. $resultInfo['type'] = 6;
  169. $resultInfo['msg'] = $error[6];
  170. }else if(isset($error[7])){
  171. $excelName = null;
  172. $objPHPWriteExcel = new PHPExcel();
  173. $objPHPWriteExcel->getProperties()->setCreator("yuer")
  174. ->setLastModifiedBy("yuer")->setTitle("")->setSubject("")
  175. ->setDescription("")->setKeywords("")->setCategory("");
  176. $prefix = substr($fileName,0,strrpos($fileName,'.'));
  177. $suffix = substr($fileName,strrpos($fileName,'.'));
  178. $excelName = date("Y_m_d_H_i_s").'_'.mt_rand(1,99).'_'.$prefix.'ErrorReport'.$suffix;
  179. $excelName = Base_Tool_Pinyin::getPinyin($excelName);
  180. $objPHPWriteExcel->setActiveSheetIndex(0);
  181. $activeSheet = $objPHPWriteExcel->getActiveSheet();
  182. $activeSheet->setTitle('错误报告');
  183. $activeSheet->setCellValueByColumnAndRow(0,1,self::PRODUCT_SAP_CODE);
  184. $activeSheet->setCellValueByColumnAndRow(1,1,'原excel行号');
  185. $activeSheet->setCellValueByColumnAndRow(2,1,'第几行编码存在重复');
  186. $activeSheet->setCellValueByColumnAndRow(3,1,self::PRODUCT_NAME);
  187. $activeSheet->setCellValueByColumnAndRow(4,1,self::PRODUCT_GROUP);
  188. $activeSheet->setCellValueByColumnAndRow(5,1,self::PRODUCT_BRAND);
  189. $activeSheet->setCellValueByColumnAndRow(6,1,self::PRODUCT_PROXY_FLAG);
  190. $activeSheet->setCellValueByColumnAndRow(7,1,self::PRODUCT_BINNING);
  191. $activeSheet->setCellValueByColumnAndRow(8,1,self::PRODUCT_SELL_PICK);
  192. $activeSheet->setCellValueByColumnAndRow(9,1,self::PRODUCT_ATTRIBUTE);
  193. $activeSheet->setCellValueByColumnAndRow(10,1,self::PRODUCT_SUPPLIER_CODE);
  194. $activeSheet->setCellValueByColumnAndRow(11,1,self::PRODUCT_SUPPLY_ADDRESS);
  195. $activeSheet->setCellValueByColumnAndRow(12,1,self::PRODUCT_BATCH);
  196. $activeSheet->setCellValueByColumnAndRow(13,1,'其他原因');
  197. $activeSheet->getColumnDimensionByColumn(0)->setWidth(15);
  198. $activeSheet->getColumnDimensionByColumn(1)->setWidth(20);
  199. $activeSheet->getColumnDimensionByColumn(2)->setWidth(20);
  200. $activeSheet->getColumnDimensionByColumn(3)->setWidth(20);
  201. $activeSheet->getColumnDimensionByColumn(4)->setWidth(20);
  202. $activeSheet->getColumnDimensionByColumn(5)->setWidth(20);
  203. $activeSheet->getColumnDimensionByColumn(6)->setWidth(20);
  204. $activeSheet->getColumnDimensionByColumn(7)->setWidth(20);
  205. $activeSheet->getColumnDimensionByColumn(8)->setWidth(20);
  206. $activeSheet->getColumnDimensionByColumn(9)->setWidth(20);
  207. $activeSheet->getColumnDimensionByColumn(10)->setWidth(20);
  208. $activeSheet->getColumnDimensionByColumn(11)->setWidth(20);
  209. $activeSheet->getColumnDimensionByColumn(12)->setWidth(20);
  210. $activeSheet->getColumnDimensionByColumn(13)->setWidth(20);
  211. $writeExcelIndex = 2;
  212. foreach ($error[7] as $pId=>$pInfo){
  213. if(isset($pInfo['hasId'])){
  214. $activeSheet->setCellValueByColumnAndRow(0,$writeExcelIndex,$pId.'-此供应商编码已经存在');
  215. } else {
  216. $activeSheet->setCellValueByColumnAndRow(0,$writeExcelIndex,$pId);
  217. }
  218. $activeSheet->setCellValueByColumnAndRow(1,$writeExcelIndex,$pInfo['excelRow']);
  219. if(isset($pInfo['duplicate'])){
  220. $activeSheet->setCellValueByColumnAndRow(2,$writeExcelIndex,$excelIdRow[$pId]);
  221. }
  222. if(isset($pInfo['emptyName'])){
  223. $activeSheet->setCellValueByColumnAndRow(3,$writeExcelIndex,'-为空');
  224. }
  225. if(isset($pInfo['emptyProductGroup'])){
  226. $activeSheet->setCellValueByColumnAndRow(4,$writeExcelIndex,'-为空');
  227. }
  228. if(isset($pInfo['emptyProductBrand'])){
  229. $activeSheet->setCellValueByColumnAndRow(5,$writeExcelIndex,'-为空');
  230. }
  231. if(isset($pInfo['emptyProductProxyFlag'])){
  232. $activeSheet->setCellValueByColumnAndRow(6,$writeExcelIndex,'-为空');
  233. }
  234. if(isset($pInfo['emptyProductBinning'])){
  235. $activeSheet->setCellValueByColumnAndRow(7,$writeExcelIndex,'-为空');
  236. }
  237. if(isset($pInfo['emptyProductSellPick'])){
  238. $activeSheet->setCellValueByColumnAndRow(8,$writeExcelIndex,'-为空');
  239. }
  240. if(isset($pInfo['emptyProductAttribute'])){
  241. $activeSheet->setCellValueByColumnAndRow(9,$writeExcelIndex,'-为空');
  242. }
  243. if(isset($pInfo['emptyVendorCode'])){
  244. $activeSheet->setCellValueByColumnAndRow(10,$writeExcelIndex,'-为空');
  245. }
  246. if(isset($pInfo['emptyZzwerkCode'])){
  247. $activeSheet->setCellValueByColumnAndRow(11,$writeExcelIndex,'-为空');
  248. }
  249. if(isset($pInfo['emptyZzlgortCode'])){
  250. $activeSheet->setCellValueByColumnAndRow(12,$writeExcelIndex,'-为空');
  251. }
  252. if(isset($pInfo['other'])){

  253. $activeSheet->setCellValueByColumnAndRow(13,$writeExcelIndex,$pInfp['other']);
  254. }
  255. $writeExcelIndex++;
  256. }
  257. $objWriter = PHPExcel_IOFactory::createWriter($objPHPWriteExcel, 'Excel5');
  258. $excelPath = FILE_PATH.DS.'feedback'.DS.$excelName;
  259. $objWriter->save($excelPath);
  260. $resultInfo['type'] = 7;
  261. $resultInfo['msg'] = $fileName."文件中存在错误";
  262. $resultInfo['errorReport'] = $excelName;
  263. // 日志操作,暂时空着
  264. }
  265. }else{
  266. //导入数据
  267. $logIds = '';
  268. $i = 0;
  269. foreach ($allProduct as $pId => $pInfo){
  270. $updateProductSql = 'insert into yr_product set ';
  271. if(isset($pInfo['pname']) && trim($pInfo['pname'])){
  272. $updateProductSql = $updateProductSql.'pname=\''.str_replace('\'','\'\'',$pInfo['pname']).'\',';
  273. }
  274. //如果SAP编码不足18位,则用0从左开始补全
  275. if(isset($pInfo['sap_code'])){
  276. if(strlen($pInfo['sap_code']) $pInfo['sap_code'] = str_pad($pInfo['sap_code'], 18, "0", STR_PAD_LEFT);
  277. $updateProductSql = $updateProductSql.'sap_code=\''.str_replace('\'','\'\'',$pInfo['sap_code']).'\',';
  278. }
  279. }
  280. if(isset($pInfo['product_group'])){
  281. $updateProductSql = $updateProductSql.'product_group=\''.$pInfo['product_group'].'\',';
  282. }
  283. if(isset($pInfo['product_brand'])){
  284. $updateProductSql = $updateProductSql.'product_brand=\''.$pInfo['product_brand'].'\',';
  285. }
  286. if(isset($pInfo['product_proxy_flag'])){
  287. $updateProductSql = $updateProductSql.'product_proxy_flag=\''.$pInfo['product_proxy_flag'].'\',';
  288. }
  289. if(isset($pInfo['product_binning'])){
  290. $updateProductSql = $updateProductSql.'product_binning=\''.$pInfo['product_binning'].'\',';
  291. }
  292. if(isset($pInfo['product_sell_pick'])){
  293. $updateProductSql = $updateProductSql.'product_sell_pick=\''.$pInfo['product_sell_pick'].'\',';
  294. }
  295. if(isset($pInfo['product_attribute'])){
  296. $updateProductSql = $updateProductSql.'product_attribute=\''.$pInfo['product_attribute'].'\',';
  297. }
  298. if(isset($pInfo['vendor_code'])){
  299. $updateProductSql = $updateProductSql.'vendor_code=\''.$pInfo['vendor_code'].'\',';
  300. }
  301. if(isset($pInfo['zzwerk_code'])){
  302. $updateProductSql = $updateProductSql.'zzwerk_code=\''.$pInfo['zzwerk_code'].'\',';
  303. }
  304. if(isset($pInfo['zzlgort_code'])){
  305. $updateProductSql = $updateProductSql.'zzlgort_code=\''.$pInfo['zzlgort_code'].'\'';
  306. }
  307. //最终的SQL语句
  308. $result = $this->excuteMultiInsertSql($updateProductSql);
  309. }
  310. $resultInfo['type'] = 8;
  311. $resultInfo['msg'] = "导入商品基本信息成功";
  312. /*

  313. * // 日志操作.
  314. * $content = '批量新建商品导入操作成功:导入的供应商品编码有->';
  315. * $logData['content'] = $content.$logIds;
  316. */
  317. }
  318. return $resultInfo;
  319. }
复制代码


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn