需要注意的地方:
行项目这个字段需要自己去生成,而且必须得填,否则会出现都按照第一行数据去生成.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
REPORT zhdl_mm_report_064. TABLES:sscrfields. DATA: i_excel TYPE TABLE OF alsmex_tabline. DATA: wa_excel TYPE alsmex_tabline. *------------------------上传的内表-------------------------* DATA:BEGIN OF ty_upload , pr_type TYPE bsart, "订单类型(采购) auto_source TYPE kzzuo, "自动货源确定 material TYPE matnr, "物料 quantity TYPE bamng, "采购申请数量 deliv_date TYPE eindt, "项目交货日期 plant TYPE ewerk, "工厂 trackingno TYPE bednr, "需求跟踪号 END OF ty_upload. DATA:i_upload LIKE TABLE OF ty_upload WITH HEADER LINE. DATA:i_uploadx LIKE TABLE OF ty_upload WITH HEADER LINE. DATA:n TYPE i. "内表行数 TYPES:BEGIN OF ty_result, type TYPE bapi_mtype, "消息类型 material(18), pr_type TYPE bsart, "订单类型(采购) flag(1), number TYPE bapimereqheader-preq_no, message(1200), END OF ty_result. DATA:gt_result TYPE TABLE OF ty_result WITH HEADER LINE. DATA:flag(1). DATA:prheader TYPE bapimereqheader, "抬头 prheaderx TYPE bapimereqheaderx, "抬头参数 number TYPE bapimereqheader-preq_no, "采购申请号(输出) pritem TYPE TABLE OF bapimereqitemimp WITH HEADER LINE, "项目 pritemx TYPE TABLE OF bapimereqitemx WITH HEADER LINE, "项目参数 return TYPE TABLE OF bapiret2 WITH HEADER LINE. "返回值 DATA:lv_num(4) TYPE n VALUE 10."行项目 *------------------------上传的内表-------------------------* *----------------------------------------------- * 定义屏幕输入字段,在这里我们要将本地文件 * (excel格式文件)上载到SAP系统 *----------------------------------------------- *------------------------------------------------ *Selection Screen *------------------------------------------------ SELECTION-SCREEN BEGIN OF BLOCK bl0 WITH FRAME TITLE text-002. PARAMETERS:p_file LIKE rlgrap-filename . SELECTION-SCREEN END OF BLOCK bl0. SELECTION-SCREEN FUNCTION KEY 1. "制作一个按钮 *------------------------------------------------- *INITIALIZATION *------------------------------------------------- INITIALIZATION. sscrfields-functxt_01 = '默认模板下载'. "设置按钮的默认名字 *------------------------------------------------- *AT SELECTION-SCREEN. *------------------------------------------------- *调用函数获取本地文件名 AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file. PERFORM frm_get_file_name. AT SELECTION-SCREEN. "下载模板到本地(模板通过SMW0上传) CASE sscrfields-ucomm. "判断按钮的功能代码 当为FC01时 就是前台点击了默认模板下载按钮。 WHEN 'FC01'. PERFORM frm_excel_download. WHEN OTHERS. ENDCASE. *---------------------上传文件格式检查----------------------* IF p_file IS INITIAL. MESSAGE '请填写上传excel的路径' TYPE 'E' DISPLAY LIKE 'W'. ENDIF. PERFORM frm_file_check. *---------------------上传文件格式检查----------------------* *------------------------------------------------- *START-OF-SELECTION. *------------------------------------------------- * 上载数据 START-OF-SELECTION. *------------------------上传数据---------------------------* PERFORM frm_upload_data. *------------------------上传数据---------------------------* *------------------------------------------------- *END-OF-SELECTION. *------------------------------------------------- END-OF-SELECTION. *&---------------------------------------------------------------------* *& Form FRM_GET_FILE_NAME *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM frm_get_file_name . CALL FUNCTION 'WS_FILENAME_GET' EXPORTING * DEF_FILENAME = ' ' * DEF_PATH = ' ' * MASK = ' ' mode = ' O' title = ' OPEN FILE' IMPORTING filename = p_file * RC = EXCEPTIONS inv_winsys = 1 no_batch = 2 selection_cancel = 3 selection_error = 4 OTHERS = 5. IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. ENDFORM. " FRM_GET_FILE_NAME *&---------------------------------------------------------------------* *& Form FRM_UPLOAD_DATA *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM frm_upload_data . DATA: l_filename TYPE string. l_filename = p_file. **Excel upload CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE' EXPORTING filename = p_file i_begin_col = 1 i_begin_row = 2 "从第二行开始,第一行是表头 i_end_col = 7 "共7列数据 i_end_row = 50000 "P_ROW TABLES intern = i_excel EXCEPTIONS inconsistent_parameters = 1 upload_ole = 2 OTHERS = 3. IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. "-----将excel中数据赋给上传用的内表中 LOOP AT i_excel INTO wa_excel. CASE wa_excel-col. WHEN '001'. i_upload-pr_type = wa_excel-value. WHEN '002'. i_upload-auto_source = wa_excel-value. WHEN '003'. i_upload-material = wa_excel-value. WHEN '004'. i_upload-quantity = wa_excel-value. WHEN '005'. i_upload-deliv_date = wa_excel-value. WHEN '006'. i_upload-plant = wa_excel-value. WHEN '007'. i_upload-trackingno = wa_excel-value. WHEN OTHERS. ENDCASE. AT END OF row. APPEND i_upload. CLEAR: i_upload.. ENDAT. CLEAR: wa_excel. ENDLOOP. *----------------- 处理传上来的数据,然后调用BAPI ---------------------* i_uploadx[] = i_upload[]. DELETE ADJACENT DUPLICATES FROM i_uploadx COMPARING pr_type auto_source. DESCRIBE TABLE i_uploadx LINES n. IF n <> 1. MESSAGE '请检查导入的数据,单次导入采购申请类型和货源确定请保持一直' TYPE 'E' DISPLAY LIKE 'I'. ENDIF. READ TABLE i_uploadx INDEX 1. "读取内表数据用来确定抬头数据. IF sy-subrc = 0. prheader-pr_type = i_uploadx-pr_type. "订单类型(采购) prheader-auto_source = i_uploadx-auto_source."自动货源确定 prheaderx-pr_type = 'X'. "订单类型(采购) prheaderx-auto_source = 'X'."自动货源确定 ENDIF. lv_num = 10. "重置计数器 LOOP AT i_upload. "增加前导零 CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' EXPORTING input = i_upload-material IMPORTING output = i_upload-material. pritem-preq_item = lv_num."行项目 pritem-material = i_upload-material."物料 pritem-quantity = i_upload-quantity . "采购申请数量 pritem-deliv_date = i_upload-deliv_date. "项目交货日期 pritem-plant = i_upload-plant . "工厂 pritem-trackingno = i_upload-trackingno. "需求跟踪号 APPEND pritem. pritemx-preq_item = lv_num."行项目 pritemx-preq_itemx = 'X'."行项目 pritemx-material = 'X'."物料 pritemx-quantity = 'X'. "采购申请数量 pritemx-deliv_date = 'X'. "项目交货日期 pritemx-plant = 'X'. "工厂 pritemx-trackingno = 'X'. "需求跟踪号 APPEND pritemx. * 行项目号叠加 lv_num = lv_num + 10. CLEAR:pritem,i_upload,pritemx. ENDLOOP. *----------------- 处理传上来的数据,然后调用BAPI ---------------------* *--------------------------调用采购申请BAPI----------------------------* CALL FUNCTION 'BAPI_PR_CREATE' EXPORTING prheader = prheader prheaderx = prheaderx * TESTRUN = IMPORTING number = number * PRHEADEREXP = TABLES return = return pritem = pritem pritemx = pritemx. LOOP AT return WHERE type = 'E' OR type = 'A'. flag = 'X'. gt_result-type = 'E'. CONCATENATE gt_result-message '&' return-message INTO gt_result-message. APPEND gt_result. ENDLOOP. IF flag = 'X'. CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. ELSE. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'. WAIT UP TO 1 SECONDS. gt_result-type = 'S'. gt_result-message = '成功创建!'. gt_result-number = number. APPEND gt_result. ENDIF. LOOP AT gt_result. WRITE:/ gt_result-type,'采购申请单:',gt_result-number,gt_result-message. ENDLOOP. ENDFORM. " FRM_UPLOAD_DATA *&---------------------------------------------------------------------* *& Form FRM_FILE_CHECK *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM frm_file_check . DATA: l_len TYPE string. DATA: l_len1 TYPE string. l_len = STRLEN( p_file ). l_len = l_len - 3. l_len1 = l_len - 1. IF p_file+l_len(3) <> 'xls' AND p_file+l_len(3) <> 'csv' AND p_file+l_len1(4) <> 'xlsx' AND p_file+l_len(3) <> 'XLS' AND p_file+l_len(3) <> 'CSV' AND p_file+l_len1(4) <> 'XLSX' . MESSAGE '该文件不是EXCEL文件,请选择EXCEL文件!' TYPE 'E'. ENDIF. ENDFORM. " FRM_FILE_CHECK *&---------------------------------------------------------------------* *& Form FRM_EXCEL_DOWNLOAD *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM frm_excel_download . DATA: lo_objdata LIKE wwwdatatab, lo_mime LIKE w3mime, lc_filename TYPE string VALUE'ZPR_EXCEL' , lc_fullpath TYPE string VALUE'C:\' , lc_path TYPE string VALUE'C:\' , ls_destination LIKE rlgrap-filename, ls_objnam TYPE string, li_rc LIKE sy-subrc, ls_errtxt TYPE string. DATA:p_objid TYPE wwwdatatab-objid, p_dest LIKE rlgrap-filename . "sapb-sappfad. p_objid = 'ZPR_EXCEL.XLSX'. "此处为EXCEL模板名称 CALL METHOD cl_gui_frontend_services=>file_save_dialog "调用保存对话框 EXPORTING default_extension = 'XLSX' default_file_name = lc_filename CHANGING filename = lc_filename path = lc_path fullpath = lc_fullpath EXCEPTIONS cntl_error = 1 error_no_gui = 2 not_supported_by_gui = 3 OTHERS = 4. IF lc_fullpath = ''. MESSAGE '不能打开excel' TYPE 'E'. ENDIF. IF sy-subrc = 0. p_dest = lc_fullpath. * concatenate p_objid '.XLS' into ls_objnam. CONDENSE ls_objnam NO-GAPS. SELECT SINGLE relid objid FROM wwwdata INTO CORRESPONDING FIELDS OF lo_objdata WHERE srtf2 = 0 AND relid = 'MI' AND objid = p_objid. *检查表wwwdata中是否存在所指定的模板文件 IF sy-subrc NE 0 OR lo_objdata-objid EQ space."如果不存在,则给出错误提示 CONCATENATE '模板文件' ls_objnam '不存在' INTO ls_errtxt. MESSAGE ls_errtxt TYPE 'I'. ENDIF. ls_destination = p_dest. "保存路径 *如果存在,调用DOWNLOAD_WEB_OBJECT 函数下载模板到路径下 CALL FUNCTION 'DOWNLOAD_WEB_OBJECT' EXPORTING key = lo_objdata destination = ls_destination IMPORTING rc = li_rc. IF li_rc NE 0. CONCATENATE '模板文件:' ls_objnam '下载失败' INTO ls_errtxt. MESSAGE ls_errtxt TYPE 'E'. ELSE. MESSAGE '模板文件下载成功' TYPE 'E' DISPLAY LIKE 'S'. ENDIF. ENDIF. * CALL SCREEN '1000'. ENDFORM. " FRM_EXCEL_DOWNLOAD |