ホームページ  >  記事  >  バックエンド開発  >  Excelデータをインポートするためのphpexcelのサンプルコード

Excelデータをインポートするためのphpexcelのサンプルコード

WBOY
WBOYオリジナル
2016-07-25 08:55:23727ブラウズ
  1. /**

  2. * 基本的な製品情報をインポートします
  3. * 編集: 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. // ファイルを指定された場所にアップロードします。 server
  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 = トリム($objWorksheet->getCellByColumnAndRow($i,1)->getValue());
  35. switch ($ cValue) {
  36. case self::PRODUCT_SAP_CODE : $colums[$i] = "sap_code";
  37. case self::PRODUCT_NAME : $colums[$i] = "pname";
  38. case self::PRODUCT_GROUP : $colums[$i] = "product_group"; ブレーク;
  39. ケース self::PRODUCT_BRAND : $colums[$i] = 'product_brand'; ブレーク;
  40. ケース self::PRODUCT_PROXY_FLAG : $colums[$i] = "product_proxy_flag" ; ブレーク;
  41. ケース self::PRODUCT_BINNING : $colums[$i] = "product_binning";
  42. case self::PRODUCT_SELL_PICK : $colums[$i] = "product_sell_pick";
  43. case self::PRODUCT_ATTRIBUTE : $colums[$i] = "製品属性"; ブレーク;
  44. ケース self::PRODUCT_SUPPLIER_CODE : $colums[$i] = "ベンダーコード"; ブレーク;
  45. ケース self::PRODUCT_SUPPLY_ADDRESS : $colums[$i] = "zzwerk_code" ; ブレーク;
  46. case self ::PRODUCT_BATCH : $colums[$i] = "zzlgort_code"; ブレーク;
  47. デフォルト : $error[3] [] = $cValue; ブレーク;
  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] = "ヘッダーには製品の SAP コードが存在しませんExcel ファイル内のデータ";
  59. }
  60. else if(count($error)==0){
  61. for ($i=2;$i $colkey = array_search('sap_code ');
  62. $shopLinkedIdValue = トリム( $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<=$max;$j++){
  83. if($execlAllShopLinkedId[$j]){
  84. $allShopLinkedId[] = $execlAllShopLinkedId[$j];
  85. }
  86. }
  87. // SAP 製品に基づいたライブラリ内のクエリコード レコードの数。
  88. $dbShopProducts = $this->getShopLinkedByIds($allShopLinkedId);

  89. for($j=$offset;$j<=$max;$j++){

  90. $ product = array();
  91. for($k=0;$k<$highestColumnIndex;$k++){
  92. $tempV = trim($objWorksheet->getCellByColumnAndRow($k,$j)->getValue( )) ;
  93. if($tempV && $tempV != '') {
  94. $product[$colums[$k]] = $tempV;
  95. }
  96. }
  97. // ファイル内の SAP エンコーディングを取得します
  98. $id = $product[ 'sap_code'];
  99. if(!$id){
  100. continue;
  101. }
  102. //製品の SAP コードがすでに存在するかどうかを確認します
  103. if(!in_array($id,$dbShopProducts)){
  104. $allProduct[ $id] = $product;
  105. }else{
  106. $error[7][$id]['hasId'] = true;
  107. }
  108. //商品名が空かどうか
  109. if(!isset($product[' pname'])) {
  110. $error[7][$id]['emptyName'] = true;
  111. }
  112. //商品カテゴリー(商品グループ)が空かどうか
  113. if(!isset($product['product_group) '])){
  114. $error[7][$id]['emptyProductGroup'] = true;
  115. }
  116. //商品レベル(ブランド)が空かどうか
  117. if(!isset($product['product_brand'] )){
  118. $error [7][$id]['emptyProductBrand'] = true;
  119. }
  120. //代理販売フラグが空かどうか
  121. if(!isset($product['product_proxy_flag'])){
  122. $error[7][$ id]['emptyProductProxyFlag'] = true;
  123. }
  124. //梱包リストが空かどうか
  125. if(!isset($product['product_binning'])){
  126. $error[7] [$id]['emptyProductBinning '] = true;
  127. }
  128. //先に売って後で買うインジケーターが空かどうか
  129. if(!isset($product['product_sell_pick'])){
  130. $error[7][ $id]['emptyProductSellPick'] = true;
  131. }
  132. //商品属性が空かどうか
  133. if(!isset($product['product_attribute'])){
  134. $error[7][$id][' emptyProductAttribute'] = true;
  135. }
  136. / /サプライヤーコードが空かどうか
  137. if(!isset($product['vendor_code'])){
  138. $error[7][$id]['emptyVendorCode'] = true ;
  139. }
  140. //供給場所が空かどうか
  141. if(!isset($product['zzwerk_code'])){
  142. $error[7][$id]['emptyZzwerkCode'] = true;
  143. }
  144. / /ライブラリ領域が空かどうか
  145. if(!isset ($product['zzlgort_code'])){
  146. $error[7][$id]['emptyZzlgortCode'] = true;
  147. }
  148. if(isset($error) [7][$id])){
  149. $ error[7][$id]['excelRow'] = $j;
  150. }
  151. }
  152. }
  153. }
  154. }
  155. $resultInfo['fileName'] = $fileName ;
  156. //エラーメッセージを返す
  157. if(count( $error)>0){
  158. if(isset($error[1])){
  159. $resultInfo['type'] = 1;
  160. $resultInfo['msg '] = $error[1];
  161. }else if(isset($error[2])){
  162. $resultInfo['type'] = 2;
  163. $resultInfo['msg'] = $error[2];
  164. }else if(isset($error[3] )){
  165. $resultInfo['type'] = 3;
  166. $resultInfo['msg'] = 'ヘッダー ['.implode(',',$error[3] ]).'] は存在しません';
  167. }else if(isset($error[4])){
  168. $resultInfo['type'] = 4;
  169. $resultInfo['msg'] = $error[4];
  170. }else if(isset($error[6] ])){
  171. $resultInfo['type'] = 6;
  172. $resultInfo['msg'] = $error[6];
  173. }else if(isset($error[7])){
  174. $excelName = null ;
  175. $objPHPWriteExcel = new PHPExcel();
  176. $objPHPWriteExcel->getProperties()->setCreator("yuer")
  177. ->setLastModifiedBy("yuer")->setTitle("")->setSubject ("")
  178. ->setDescription("")->setKeywords("")->setCategory("");
  179. $prefix = substr($fileName,0,strrpos($fileName,'.') );
  180. $suffix = substr($fileName,strrpos($fileName,'.'));
  181. $excelName = date("Y_m_d_H_i_s").'_'.mt_rand(1,99).'_'.$prefix .'ErrorReport'.$suffix;
  182. $excelName = Base_Tool_Pinyin::getPinyin($excelName);
  183. $objPHPWriteExcel->setActiveSheetIndex(0);
  184. $activeSheet = $objPHPWriteExcel->getActiveSheet();
  185. $activeSheet-> ;setTitle('错误报告');
  186. $activeSheet->setCellValueByColumnAndRow(0,1,self::PRODUCT_SAP_CODE);
  187. $activeSheet->setCellValueByColumnAndRow(1,1,'原excel行号');
  188. $activeSheet ->setCellValueByColumnAndRow(2,1,'第几行コード存在重复');
  189. $activeSheet->setCellValueByColumnAndRow(3,1,self::PRODUCT_NAME);
  190. $activeSheet->setCellValueByColumnAndRow(4,1,self) ::PRODUCT_GROUP);
  191. $activeSheet->setCellValueByColumnAndRow(5,1,self::PRODUCT_BRAND);
  192. $activeSheet->setCellValueByColumnAndRow(6,1,self::PRODUCT_PROXY_FLAG);
  193. $activeSheet->setCellValueByColumnAndRow(7) ,1,self::PRODUCT_BINNING);
  194. $activeSheet->setCellValueByColumnAndRow(8,1,self::PRODUCT_SELL_PICK);
  195. $activeSheet->setCellValueByColumnAndRow(9,1,self::PRODUCT_ATTRIBUTE);
  196. $activeSheet-> ;setCellValueByColumnAndRow(10,1,self::PRODUCT_SUPPLIER_CODE);
  197. $activeSheet->setCellValueByColumnAndRow(11,1,self::PRODUCT_SUPPLY_ADDRESS);
  198. $activeSheet->setCellValueByColumnAndRow(12,1,self::PRODUCT_BATCH);
  199. $activeSheet->setCellValueByColumnAndRow(13,1,'その他の原因');
  200. $activeSheet->getColumnDimensionByColumn(0)->setWidth(15);
  201. $activeSheet->getColumnDimensionByColumn(1)->setWidth( 20);
  202. $activeSheet->getColumnDimensionByColumn(2)->setWidth(20);
  203. $activeSheet->getColumnDimensionByColumn(3)->setWidth(20);
  204. $activeSheet->getColumnDimensionByColumn(4)- >setWidth(20);
  205. $activeSheet->getColumnDimensionByColumn(5)->setWidth(20);
  206. $activeSheet->getColumnDimensionByColumn(6)->setWidth(20);
  207. $activeSheet->getColumnDimensionByColumn (7)->setWidth(20);
  208. $activeSheet->getColumnDimensionByColumn(8)->setWidth(20);
  209. $activeSheet->getColumnDimensionByColumn(9)->setWidth(20);
  210. $activeSheet ->getColumnDimensionByColumn(10)->setWidth(20);
  211. $activeSheet->getColumnDimensionByColumn(11)->setWidth(20);
  212. $activeSheet->getColumnDimensionByColumn(12)->setWidth(20) ;
  213. $activeSheet->getColumnDimensionByColumn(13)->setWidth(20);
  214. $writeExcelIndex = 2;
  215. foreach ($error[7] as $pId=>$pInfo){
  216. if(isset($pInfo) ['hasId'])){
  217. $activeSheet->setCellValueByColumnAndRow(0,$writeExcelIndex,$pId.'-此供应商编码已经存在');
  218. } else {
  219. $activeSheet->setCellValueByColumnAndRow(0,$ writeExcelIndex,$pId);
  220. }
  221. $activeSheet->setCellValueByColumnAndRow(1,$writeExcelIndex,$pInfo['excelRow']);
  222. if(isset($pInfo['duplicate'])){
  223. $activeSheet-> ;setCellValueByColumnAndRow(2,$writeExcelIndex,$excelIdRow[$pId]);
  224. }
  225. if(isset($pInfo['emptyName'])){
  226. $activeSheet->setCellValueByColumnAndRow(3,$writeExcelIndex,'-のため空');
  227. }
  228. if(isset($pInfo['emptyProductGroup'])){
  229. $activeSheet->setCellValueByColumnAndRow(4,$writeExcelIndex,'-である空');
  230. }
  231. if(isset($pInfo['emptyProductBrand']) ){
  232. $activeSheet->setCellValueByColumnAndRow(5,$writeExcelIndex,'-である空');
  233. }
  234. if(isset($pInfo['emptyProductProxyFlag'])){
  235. $activeSheet->setCellValueByColumnAndRow(6,$ writeExcelIndex,'-のため空');
  236. }
  237. if(isset($pInfo['emptyProductBinning'])){
  238. $activeSheet->setCellValueByColumnAndRow(7,$writeExcelIndex,'-のため空');
  239. }
  240. if (isset($pInfo['emptyProductSellPick'])){
  241. $activeSheet->setCellValueByColumnAndRow(8,$writeExcelIndex,'-である空');
  242. }
  243. if(isset($pInfo['emptyProductAttribute'])){
  244. $activeSheet->setCellValueByColumnAndRow(9,$writeExcelIndex,'-である空');
  245. }
  246. if(isset($pInfo['emptyVendorCode'])){
  247. $activeSheet->setCellValueByColumnAndRow(10,$writeExcelIndex, '-のため空');
  248. }
  249. if(isset($pInfo['emptyZzwerkCode'])){
  250. $activeSheet->setCellValueByColumnAndRow(11,$writeExcelIndex,'-のため空');
  251. }
  252. if(isset ($pInfo['emptyZzlgortCode'])){
  253. $activeSheet->setCellValueByColumnAndRow(12,$writeExcelIndex,'-のため空');
  254. }

  255. if(isset($pInfo['other'])){

  256. $activeSheet->setCellValueByColumnAndRow(13,$writeExcelIndex,$pInfp['other']);
  257. }
  258. $writeExcelIndex++;
  259. }
  260. $objWriter = PHPExcel_IOFactory: :createWriter($objPHPWriteExcel, 'Excel5');
  261. $excelPath = FILE_PATH.DS.'フィードバック'.DS.$excelName;
  262. $objWriter->save($excelPath);
  263. $resultInfo['type'] = 7 ;
  264. $resultInfo['msg'] = $fileName."文件中に存在错误";
  265. $resultInfo['errorReport'] = $excelName;
  266. // 日志操作,暂時空着
  267. }
  268. }else{
  269. //导入架
  270. $logIds = '';
  271. $i = 0;
  272. foreach ($allProduct as $pId => $pInfo){
  273. $updateProductSql = 'yr_product set に挿入 ';
  274. if(isset($pInfo[' pname']) && trim($pInfo['pname'])){
  275. $updateProductSql = $updateProductSql.'pname=''.str_replace(''','''',$pInfo['pname'])。 '',';
  276. }
  277. //如果SAP编码不足18位,则用0从左开始补全
  278. if(isset($pInfo['sap_code'])){
  279. if(strlen($pInfo['sap_code] '])<18){
  280. $pInfo['sap_code'] = str_pad($pInfo['sap_code'], 18, "0", STR_PAD_LEFT);
  281. $updateProductSql = $updateProductSql.'sap_code=''.str_replace ('','''',$pInfo['sap_code']).'',';
  282. }
  283. }
  284. if(isset($pInfo['product_group'])){
  285. $updateProductSql = $updateProductSql. 'product_group=''.$pInfo['product_group'].'',';
  286. }
  287. if(isset($pInfo['product_brand'])){
  288. $updateProductSql = $updateProductSql.'product_brand=''.$ pInfo['product_brand'].'',';
  289. }
  290. if(isset($pInfo['product_proxy_flag'])){
  291. $updateProductSql = $updateProductSql.'product_proxy_flag=''.$pInfo['product_proxy_flag']. '',';
  292. }
  293. if(isset($pInfo['product_binning'])){
  294. $updateProductSql = $updateProductSql.'product_binning=''.$pInfo['product_binning'].'',';
  295. }
  296. if(isset($pInfo['product_sell_pick'])){
  297. $updateProductSql = $updateProductSql.'product_sell_pick=''.$pInfo['product_sell_pick'].'',';
  298. }
  299. if(isset($pInfo ['product_attribute'])){
  300. $updateProductSql = $updateProductSql.'product_attribute=''.$pInfo['product_attribute'].'',';
  301. }
  302. if(isset($pInfo['vendor_code'])) {
  303. $updateProductSql = $updateProductSql.'vendor_code=''.$pInfo['vendor_code'].'',';
  304. }
  305. if(isset($pInfo['zzwerk_code'])){
  306. $updateProductSql = $updateProductSql .'zzwerk_code=''.$pInfo['zzwerk_code'].'',';
  307. }
  308. if(isset($pInfo['zzlgort_code'])){
  309. $updateProductSql = $updateProductSql.'zzlgort_code=''.$pInfo['zzlgort_code'].''';
  310. }
  311. //最終 SQL ステートメント
  312. $ result = $this->excuteMultiInsertSql($updateProductSql);
  313. }
  314. $resultInfo['type'] = 8;
  315. $resultInfo['msg'] = "基本的な製品情報を正常にインポートしました";

  316. < ;p> /*
  317. * // ログ操作。
  318. * $content = '新しい製品のバッチインポート操作が成功しました: インポートされた供給製品コードは ->';
  319. * $logData['content'] = $logIds. ;
  320. */
  321. }
  322. return $resultInfo;
  323. }

コードをコピー


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。