ABAP批量修改物料主数据毛重
选择屏幕
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3453603fdbc4156ad1a1e189a75f5db2.png)
代码:
REPORT ZMM02_MZ.
- 数据声明
TYPES : BEGIN OF LT_TAB ,
BRGEW TYPE mara-BRGEW,
MATERIAL LIKE mara-MATNR,
END OF LT_TAB.
DATA: CLIDAINP LIKE BAPI_MARA_GA, "客户端层次物料数据
CLIDAOUT LIKE BAPI_MARA, "客户端层次物料数据
CLIDAOUTX LIKE BAPI_MARAX, "BAPI_MARA 的复选框结构
HEADDATA LIKE BAPIMATHEAD, "带有控制信息的表头段
RETURN LIKE BAPIRET2, "返回参数
RETURNMES LIKE BAPI_MATRETURN2 OCCURS 0 WITH HEADER LINE.
DATA: BEGIN OF W_EXTENSIONIN OCCURS 0.
INCLUDE STRUCTURE BAPIPAREX. "BAPI 参数 ExtensionIn/ExtensionOut 的参考结构
DATA: END OF W_EXTENSIONIN.
DATA: BEGIN OF W_EXTENSIONINX OCCURS 0.
INCLUDE STRUCTURE BAPIPAREXX. "Extension In/Extension Out 的复选框结构
DATA: END OF W_EXTENSIONINX.
DATA ZZBAPI_TE_MARA LIKE BAPI_TE_MARA. "客户定义字段:客户级别的物料数据
DATA ZZBAPI_TE_MARAX LIKE BAPI_TE_MARAX. "客户定义字段:客户级别的物料数据
- 取出要处理的物料编号
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-002.
PARAMETERS: MATERIAL LIKE BAPI_MARA_GA-MATERIAL, "物料
S_BRGEW LIKE BAPI_TE_MARA-BRGEW, "毛重
S_NTGEW LIKE BAPI_MARA-NET_WEIGHT. "净重
PARAMETER p_p4 TYPE char1 DEFAULT ‘X’ RADIOBUTTON GROUP g1.
PARAMETER p_p5 TYPE char1 RADIOBUTTON GROUP g1."同在G1组只能选着一个
SELECTION-SCREEN END OF BLOCK b1.
***************************批量上传修改
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-001.
PARAMETERS: p_path TYPE rlgrap-filename.
PARAMETERS: rb_up RADIOBUTTON GROUP grp1 DEFAULT ‘X’ USER-COMMAND comm.
PARAMETERS: rb_down RADIOBUTTON GROUP grp1 .
SELECTION-SCREEN END OF BLOCK b2.
IF p_p4 = ‘X’.
DATA:S_GEWEI LIKE BAPI_MARA-UNIT_OF_WT.
-
读取物料数据
CALL FUNCTION ‘BAPI_MATERIAL_GET_ALL’
EXPORTING
MATERIAL = MATERIAL
IMPORTING
CLIENTDATA = CLIDAINP
TABLES
RETURN = RETURNMES. -
抬头数据 ,物料和视图维护
HEADDATA-MATERIAL = MATERIAL. "物料号
HEADDATA-BASIC_VIEW = ‘X’.
CLIDAOUT-NET_WEIGHT = S_NTGEW. "净重
CLIDAOUTX-NET_WEIGHT = ‘X’.
"单位
*CLIDAOUT-UNIT_OF_WT = S_GEWEI. "单位
*CLIDAOUTX-UNIT_OF_WT = ‘KG’.
ZZBAPI_TE_MARA-MATERIAL = HEADDATA-MATERIAL.
ZZBAPI_TE_MARA-BRGEW = S_BRGEW. "毛重
ZZBAPI_TE_MARAX-MATERIAL = HEADDATA-MATERIAL.
ZZBAPI_TE_MARAX-BRGEW = ‘X’.
-
写入新的自定义字段值
-
CALL METHOD cl_abap_container_utilities=>fill_container_c
-
EXPORTING
-
im_value = ZZBAPI_TE_MARA
-
IMPORTING
-
ex_container = W_EXTENSIONIN+30
-
EXCEPTIONS
-
illegal_parameter_type = 1
-
OTHERS = 2.
W_EXTENSIONIN-STRUCTURE = ‘BAPI_TE_MARA’.
W_EXTENSIONIN+30 = ZZBAPI_TE_MARA.
APPEND W_EXTENSIONIN.
-
CALL METHOD cl_abap_container_utilities=>fill_container_c
-
EXPORTING
-
im_value = ZZBAPI_TE_MARAX
-
IMPORTING
-
ex_container = W_EXTENSIONINX+30
-
EXCEPTIONS
-
illegal_parameter_type = 1
-
OTHERS = 2.
W_EXTENSIONINX-STRUCTURE = ‘BAPI_TE_MARAX’.
W_EXTENSIONINX+30 = ZZBAPI_TE_MARAX.
APPEND W_EXTENSIONINX.
- CallBAPI
CALL FUNCTION ‘BAPI_MATERIAL_SAVEDATA’
EXPORTING
HEADDATA = HEADDATA
CLIENTDATA = CLIDAOUT
CLIENTDATAX = CLIDAOUTX
IMPORTING
RETURN = RETURN
TABLES
RETURNMESSAGES = RETURNMES
EXTENSIONIN = W_EXTENSIONIN
EXTENSIONINX = W_EXTENSIONINX.
CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’.
IF RETURN-TYPE = ‘E’.
LOOP AT RETURNMES.
MESSAGE RETURNMES-MESSAGE TYPE 'E'.
ENDLOOP.
ELSEIF RETURN-TYPE = ‘S’…
MESSAGE ‘修改成功’ TYPE ‘S’.
ENDIF.
ENDIF.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path."获取文件地址搜索帮助
PERFORM frm_get_fielpath.
AT SELECTION-SCREEN.
&--------------------------------------------------------------
*& 选择屏幕控制
&------------------------------------------------------------
AT SELECTION-SCREEN OUTPUT .
*程序开始处理
START-OF-SELECTION.
IF p_p5 = ‘X’.
IF rb_up = ‘X’ AND p_path IS NOT INITIAL.
SEARCH p_path FOR ‘.XLS.’. "判断上传文档格式
IF sy-subrc EQ 0.
PERFORM get_up. ".xls 格式上传
ELSE.
SEARCH p_path FOR ‘.XLSX.’.
IF sy-subrc EQ 0.
PERFORM get_up. ".xls 格式上传
ELSE.
MESSAGE ‘请上传正确的excel文件!’ TYPE ‘E’ DISPLAY LIKE ‘I’.
ENDIF.
ENDIF.
ELSEIF rb_down = ‘X’.
PERFORM get_down.
ENDIF.
ENDIF.
FORM frm_get_fielpath .
CALL FUNCTION ‘TB_LIMIT_WS_FILENAME_GET’
IMPORTING
filename = p_path
EXCEPTIONS
selection_cancel = 1
selection_error = 2
OTHERS = 3.
ENDFORM.
FORM get_up.
DATA: i_table TYPE TABLE OF LT_TAB,
w_table TYPE LT_TAB.
DATA: i_excel TYPE TABLE OF alsmex_tabline, “表格结构。”
w_excel TYPE alsmex_tabline.
“ALSMEX_TABLINE是具有Excel数据的表行,有三个组件,row col value 。”
DATA: get_file TYPE rlgrap-filename.“这里参照系统中的结构字段。”
MOVE p_path TO get_file.“将地址栏的值赋值给GET_FILE”
CALL FUNCTION ‘ALSM_EXCEL_TO_INTERNAL_TABLE’
EXPORTING
filename = get_file
i_begin_col = 1
i_begin_row = 2
i_end_col = 9999
i_end_row = 9999
TABLES
intern = i_excel
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
-
DELETE i_excel WHERE row = 1."删除第一行抬头。如果excel文件中不存在抬头的话,可不写此句
IF i_excel IS INITIAL.
CALL FUNCTION ‘POPUP_TO_CONFIRM_WITH_MESSAGE’
EXPORTING -
DEFAULTOPTION = 'Y' diagnosetext1 = '数据错误'
-
DIAGNOSETEXT2 = ' '
-
DIAGNOSETEXT3 = ' ' textline1 = 'Excel表格中没有内容!' titel = '提示'.
RETURN.
ENDIF.CLEAR:i_table.
FIELD-SYMBOLS:.“这里涉及到SAP内部字段的使用,详情请参见微博相关介绍。”
SORT i_excel BY row col.
DATA:num_col TYPE i.
LOOP AT i_excel INTO w_excel.
num_col = w_excel-col.
ASSIGN COMPONENT num_col OF STRUCTURE w_table TO .
= w_excel-value.AT END OF row.
APPEND w_table TO i_table.
CLEAR:w_table.
ENDAT.
CLEAR:w_excel.ENDLOOP.
DATA : c TYPE i.
c = 0.LOOP AT i_table INTO w_table.
c = c + 1. -
读取物料数据
HEADDATA-MATERIAL = |{ w_table-MATERIAL alpha = in }|. "物料号
HEADDATA-BASIC_VIEW = ‘X’.
CLIDAOUT-NET_WEIGHT = ‘’. "净重
CLIDAOUTX-NET_WEIGHT = ‘X’.
"单位
*CLIDAOUT-UNIT_OF_WT = S_GEWEI. "单位
*CLIDAOUTX-UNIT_OF_WT = ‘KG’.
ZZBAPI_TE_MARA-MATERIAL = HEADDATA-MATERIAL.
ZZBAPI_TE_MARA-BRGEW = w_table-BRGEW. "毛重
ZZBAPI_TE_MARAX-MATERIAL = HEADDATA-MATERIAL.
ZZBAPI_TE_MARAX-BRGEW = ‘X’.
写入新的自定义字段值
CALL METHOD cl_abap_container_utilities=>fill_container_c
EXPORTING
im_value = ZZBAPI_TE_MARA
IMPORTING
ex_container = W_EXTENSIONIN+30
EXCEPTIONS
illegal_parameter_type = 1
OTHERS = 2.
W_EXTENSIONIN-STRUCTURE = ‘BAPI_TE_MARA’.
W_EXTENSIONIN+30 = ZZBAPI_TE_MARA.
APPEND W_EXTENSIONIN.
CALL METHOD cl_abap_container_utilities=>fill_container_c
EXPORTING
im_value = ZZBAPI_TE_MARAX
IMPORTING
ex_container = W_EXTENSIONINX+30
EXCEPTIONS
illegal_parameter_type = 1
OTHERS = 2.
W_EXTENSIONINX-STRUCTURE = ‘BAPI_TE_MARAX’.
W_EXTENSIONINX+30 = ZZBAPI_TE_MARAX.
APPEND W_EXTENSIONINX.
- CallBAPI
CALL FUNCTION ‘BAPI_MATERIAL_SAVEDATA’
EXPORTING
HEADDATA = HEADDATA
CLIENTDATA = CLIDAOUT
CLIENTDATAX = CLIDAOUTX
IMPORTING
RETURN = RETURN
TABLES
RETURNMESSAGES = RETURNMES
EXTENSIONIN = W_EXTENSIONIN
EXTENSIONINX = W_EXTENSIONINX.
CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’.
IF RETURN-TYPE = ‘E’.
LOOP AT RETURNMES.
MESSAGE RETURNMES-MESSAGE TYPE 'E'.
ENDLOOP.
ELSEIF RETURN-TYPE = ‘S’…
MESSAGE ‘修改成功’ TYPE ‘S’.
ENDIF.
clear:RETURN-TYPE.
clear:HEADDATA.
clear:W_EXTENSIONINX.
clear:W_EXTENSIONINX[].
clear:W_EXTENSIONIN.
CLEAR:W_EXTENSIONIN[].
CLEAR:RETURN.
clear:CLIDAOUT.
clear:CLIDAOUTX.
CLEAR:w_table.
ENDLOOP.
-
MODIFY zsd090 FROM TABLE gt_table.“这里使用modify,兼顾增加和修改,若主键存在则为修改,否则增加”
*INSERT zCSJLYS FROM TABLE gt_table.
IF sy-subrc = 0.
COMMIT WORK AND WAIT.“成功提交,或者最后程序结束系统也会自动提交”DATA:str_line1 TYPE string .
DATA: str TYPE string.
str = c.
CONCATENATE ‘成功修改数据’ str ‘行’ INTO str_line1 .
“读出excel中的数据条数,用于消息提示”
CALL FUNCTION ‘POPUP_TO_CONFIRM_WITH_MESSAGE’
EXPORTING
diagnosetext1 = ‘成功’
textline1 = str_line1
titel = ‘提示’.
ELSE.
ROLLBACK WORK.“否则失败”
ENDIF.
ENDFORM.
FORM get_down.
DATA:lv_text TYPE string, "message
lv_mod(20), "model
lv_fullpath TYPE string, "full path
ls_object TYPE wwwdatatab, "object name
lv_rc TYPE sy-subrc. "return code
DATA: lv_filepath TYPE string, " 存放路径
lv_filename TYPE string, "文件名称
lv_path TYPE string . " 下载后存放全路径
DATA: lv_objdata LIKE wwwdatatab,
lv_obj_name LIKE wwwdatatab-objid,
lt_mime LIKE w3mime OCCURS 10.
DATA: lv_ret TYPE abap_bool,
lv_answer.
DATA: lv_file TYPE string.
*&—SMWO 模板名称
lv_mod = ‘ZMM02_MZ’. "下载文件名
"保存对话框
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
window_title = ‘下载模板’
default_extension = ‘xlsx’
default_file_name = ‘毛重批量修改下载模板.xls’
file_filter = ‘EXCEL’
prompt_on_overwrite = ‘’
CHANGING
fullpath = lv_path
filename = lv_filename
path = lv_filepath
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
"检查文件是否存在
MOVE lv_path TO lv_file.
CALL METHOD cl_gui_frontend_services=>file_exist
EXPORTING
file = lv_file
RECEIVING
result = lv_ret
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
not_supported_by_gui = 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.
EXIT.
ENDIF.
IF lv_ret EQ ‘X’.
-
模版已存在,是否覆盖
CALL FUNCTION ‘POPUP_TO_CONFIRM’
EXPORTING
text_question = ‘模版已存在,是否覆盖?’
text_button_1 = ‘是’(005)
text_button_2 = ‘否’(006)
IMPORTING
answer = lv_answer
EXCEPTIONS
text_not_found = 1
OTHERS = 2.
IF sy-subrc <> 0.ENDIF.
IF lv_answer EQ ‘A’. "取消
EXIT.
ELSEIF lv_answer NE ‘1’. "否
EXIT.
ENDIF.
ELSE.
lv_answer = ‘1’.
ENDIF.IF lv_answer EQ ‘1’.
SELECT relid objid
FROM wwwdata
INTO CORRESPONDING FIELDS OF lv_objdata
UP TO 1 ROWS
WHERE srtf2 = 0
AND relid = ‘MI’
AND objid = lv_mod.
ENDSELECT.IF lv_path IS NOT INITIAL.
" 装载至本地文件
p_path = lv_path.
CALL FUNCTION ‘DOWNLOAD_WEB_OBJECT’ "
EXPORTING
key = lv_objdata
destination = p_path.
ENDIF.ENDIF.
ENDFORM.