学了abap一个月了,写个教程试一下
以下以MM01为例介绍一下如何编写BDC程序
首先运行T-code SHDB
录屏是比较简单的,按你需要的步骤做就可以了,最后可以得到一张BDC表如下所示:
见附件
点击后退,再点PROGRAM系统就会自动生成程序了,但是系统生成的程序经常不能满足我们的要求。
根据这张表我们就可以写程序了。程序基本上可以分为四个部分:
1、 写BDC表
由于写表时有许多程序是重复的操作,可以先写两个子程序,分别用来保存DYNPRO和FIELD信息,这个在系统生成的标准程序里面都有:
FORM bdc_dynpro USING program dynpro.
CLEAR bdcdata1.
bdcdata1-program = program.
bdcdata1-dynpro = dynpro.
bdcdata1-dynbegin = 'X'.
APPEND bdcdata1.
ENDFORM.
FORM bdc_field USING fnam fval.
CLEAR bdcdata1.
bdcdata1-fnam = fnam.
bdcdata1-fval = fval.
APPEND bdcdata1.
ENDFORM.
把这两个子程序放在INCLUDE里面,然后再写个填表的子程序,BDC_SUBSCR的内容不用写:
FORM append_mm01 USING arg LIKE wa.
PERFORM bdc_dynpro USING 'SAPLMGMM' '0060'.
PERFORM bdc_field USING 'BDC_CURSOR' 'RMMG1-MATNR'.
PERFORM bdc_field USING 'BDC_OKCODE' '/00'.
PERFORM bdc_field USING 'RMMG1-MATNR' arg-MATNR.
PERFORM bdc_field USING 'RMMG1-MBRSH' arg-MBRSH.
PERFORM bdc_field USING 'RMMG1-MTART' arg-MTART.
*开始新的画面
PERFORM bdc_dynpro USING 'SAPLMGMM' '0070'.
PERFORM bdc_field USING 'BDC_CURSOR' 'MSICHTAUSW-DYTXT(02)'.
PERFORM bdc_field USING 'BDC_OKCODE' '=ENTR'.
PERFORM bdc_field USING 'MSICHTAUSW-KZSEL(01)' 'X'.
*开始新的画面事件
PERFORM bdc_dynpro USING 'SAPLMGMM' '4004'.
PERFORM bdc_field USING 'BDC_OKCODE' '=BU'.
PERFORM bdc_field USING 'BDC_CURSOR' 'MAKT-MAKTX'.
PERFORM bdc_field USING 'MAKT-MAKTX' arg-MAKTX.
PERFORM bdc_field USING 'MARA-MEINS' arg-MEINS.
PERFORM bdc_field USING 'MARA-MTPOS_MARA' arg-MTPOS_MARA.
CALL TRANSACTION'MM01' USING bdcdata1 MODE 'N' MESSAGES INTO msgtab.
ENDFORM.
2、 定义所需的数据结构
TABLES : sscrfields, "sscrfields是一个structure,
"不知道为什么可以这样用
T100. "存放消息的表
DATA :
BEGIN OF wa, "定义导入的数据的结构
MATNR TYPE RMMG1-MATNR,
MBRSH TYPE RMMG1-MBRSH,
MTART TYPE RMMG1-MTART,
MAKTX TYPE MAKT-MAKTX,
MEINS TYPE MARA-MEINS,
MTPOS_MARA TYPE MARA-MTPOS_MARA,
END OF wa,
line(1000) TYPE c, "存放excel文件的一行
itab2 LIKE TABLE OF line WITH HEADER LINE, "存放excel文件的内表
itab1 LIKE TABLE OF wa WITH HEADER LINE, "定义导入数据的内表
bdcdata1 LIKE BDCDATA OCCURS 0 WITH HEADER LINE,"定义BDC内表
msgtab LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE,"定义消息内表
L_MSTRING(480). "消息字符串
3、 写SELECTION-SCREEN,做好上传文件界面
SELECTION-SCREEN:
BEGIN OF BLOCK b1 WITH FRAME NO INTERVALS,
BEGIN OF LINE,
COMMENT (7) text1.
PARAMETERS file1 LIKE RLGRAP-FILENAME VISIBLE LENGTH 20.
SELECTION-SCREEN:
PUSHBUTTON 32(10) btn1 USER-COMMAND cli1,
END OF LINE,
END OF BLOCK b1.
屏幕事件的监听:
AT SELECTION-SCREEN.
CASE sscrfields.
WHEN 'CLI1'.
CALL FUNCTION 'WS_FILENAME_GET'"打开文件对话框
IMPORTING
FILENAME = file1
EXCEPTIONS
INV_WINSYS = 1
NO_BATCH = 2
SELECTION_CANCEL = 3
SELECTION_ERROR = 4.
ENDCASE.
INITIALIZATION.
text1 = '上传文件:'.
btn1 = '浏览…'.
4、 对上传的excel文件进行处理,并输出系统消息
START-OF-SELECTION.
CALL FUNCTION 'WS_UPLOAD'"上传文件
EXPORTING
FILENAME = file1
TABLES
DATA_TAB = itab2
EXCEPTIONS
CONVERSION_ERROR = 1
FILE_OPEN_ERROR = 2
FILE_READ_ERROR = 3
INVALID_TYPE = 4
NO_BATCH = 5
UNKNOWN_ERROR = 6
INVALID_TABLE_WIDTH = 7
GUI_REFUSE_FILETRANSFER = 8
CUSTOMER_ERROR = 9
OTHERS = 10.
LOOP AT itab2.
SPLIT itab2 AT ',' INTO wa-MATNR wa-MBRSH wa-MTART wa-MAKTX wa-MEINS wa-MTPOS_MARA.
wa-MTPOS_MARA.
APPEND wa TO itab1.
ENDLOOP.
LOOP AT itab1 INTO wa.
PERFORM append_mm01 USING wa.
CLEAR bdcdata1[]. "清空BDCDATA的所有数据
ENDLOOP.
输出系统消息
WRITE : / '消息类型', 20 '消息内容'.
LOOP AT msgtab.
SELECT SINGLE * FROM T100 WHERE SPRSL = msgtab-MSGSPRA
AND ARBGB = msgtab-MSGID
AND MSGNR = msgtab-MSGNR.
IF SY-SUBRC = 0.
L_MSTRING = T100-TEXT.
IF L_MSTRING CS '&1'.
REPLACE '&1' WITH msgtab-MSGV1 INTO L_MSTRING.
REPLACE '&2' WITH msgtab-MSGV2 INTO L_MSTRING.
REPLACE '&3' WITH msgtab-MSGV3 INTO L_MSTRING.
REPLACE '&4' WITH msgtab-MSGV4 INTO L_MSTRING.
ELSE.
REPLACE '&' WITH msgtab-MSGV1 INTO L_MSTRING.
REPLACE '&' WITH msgtab-MSGV2 INTO L_MSTRING.
REPLACE '&' WITH msgtab-MSGV3 INTO L_MSTRING.
REPLACE '&' WITH msgtab-MSGV4 INTO L_MSTRING.
ENDIF.
CONDENSE L_MSTRING.
WRITE: / msgtab-MSGTYP, 20 L_MSTRING.
ELSE.
WRITE: / msgtab.
ENDIF.
ENDLOOP.
测试csv文件
zchen11,C,WERB,bdc1,CM,NORM
zchen12,C,WERB,bdc2,CM,NORM
zchen13,C,WERB,bdc3,CM,NORM
![44_078736_1189395066.jpg](https://i-blog.csdnimg.cn/blog_migrate/861ea28f5f5dd8d7ad3232311c0848df.png)
未命名-1.jpg
以下以MM01为例介绍一下如何编写BDC程序
首先运行T-code SHDB
录屏是比较简单的,按你需要的步骤做就可以了,最后可以得到一张BDC表如下所示:
见附件
点击后退,再点PROGRAM系统就会自动生成程序了,但是系统生成的程序经常不能满足我们的要求。
根据这张表我们就可以写程序了。程序基本上可以分为四个部分:
1、 写BDC表
由于写表时有许多程序是重复的操作,可以先写两个子程序,分别用来保存DYNPRO和FIELD信息,这个在系统生成的标准程序里面都有:
FORM bdc_dynpro USING program dynpro.
CLEAR bdcdata1.
bdcdata1-program = program.
bdcdata1-dynpro = dynpro.
bdcdata1-dynbegin = 'X'.
APPEND bdcdata1.
ENDFORM.
FORM bdc_field USING fnam fval.
CLEAR bdcdata1.
bdcdata1-fnam = fnam.
bdcdata1-fval = fval.
APPEND bdcdata1.
ENDFORM.
把这两个子程序放在INCLUDE里面,然后再写个填表的子程序,BDC_SUBSCR的内容不用写:
FORM append_mm01 USING arg LIKE wa.
PERFORM bdc_dynpro USING 'SAPLMGMM' '0060'.
PERFORM bdc_field USING 'BDC_CURSOR' 'RMMG1-MATNR'.
PERFORM bdc_field USING 'BDC_OKCODE' '/00'.
PERFORM bdc_field USING 'RMMG1-MATNR' arg-MATNR.
PERFORM bdc_field USING 'RMMG1-MBRSH' arg-MBRSH.
PERFORM bdc_field USING 'RMMG1-MTART' arg-MTART.
*开始新的画面
PERFORM bdc_dynpro USING 'SAPLMGMM' '0070'.
PERFORM bdc_field USING 'BDC_CURSOR' 'MSICHTAUSW-DYTXT(02)'.
PERFORM bdc_field USING 'BDC_OKCODE' '=ENTR'.
PERFORM bdc_field USING 'MSICHTAUSW-KZSEL(01)' 'X'.
*开始新的画面事件
PERFORM bdc_dynpro USING 'SAPLMGMM' '4004'.
PERFORM bdc_field USING 'BDC_OKCODE' '=BU'.
PERFORM bdc_field USING 'BDC_CURSOR' 'MAKT-MAKTX'.
PERFORM bdc_field USING 'MAKT-MAKTX' arg-MAKTX.
PERFORM bdc_field USING 'MARA-MEINS' arg-MEINS.
PERFORM bdc_field USING 'MARA-MTPOS_MARA' arg-MTPOS_MARA.
CALL TRANSACTION'MM01' USING bdcdata1 MODE 'N' MESSAGES INTO msgtab.
ENDFORM.
2、 定义所需的数据结构
TABLES : sscrfields, "sscrfields是一个structure,
"不知道为什么可以这样用
T100. "存放消息的表
DATA :
BEGIN OF wa, "定义导入的数据的结构
MATNR TYPE RMMG1-MATNR,
MBRSH TYPE RMMG1-MBRSH,
MTART TYPE RMMG1-MTART,
MAKTX TYPE MAKT-MAKTX,
MEINS TYPE MARA-MEINS,
MTPOS_MARA TYPE MARA-MTPOS_MARA,
END OF wa,
line(1000) TYPE c, "存放excel文件的一行
itab2 LIKE TABLE OF line WITH HEADER LINE, "存放excel文件的内表
itab1 LIKE TABLE OF wa WITH HEADER LINE, "定义导入数据的内表
bdcdata1 LIKE BDCDATA OCCURS 0 WITH HEADER LINE,"定义BDC内表
msgtab LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE,"定义消息内表
L_MSTRING(480). "消息字符串
3、 写SELECTION-SCREEN,做好上传文件界面
SELECTION-SCREEN:
BEGIN OF BLOCK b1 WITH FRAME NO INTERVALS,
BEGIN OF LINE,
COMMENT (7) text1.
PARAMETERS file1 LIKE RLGRAP-FILENAME VISIBLE LENGTH 20.
SELECTION-SCREEN:
PUSHBUTTON 32(10) btn1 USER-COMMAND cli1,
END OF LINE,
END OF BLOCK b1.
屏幕事件的监听:
AT SELECTION-SCREEN.
CASE sscrfields.
WHEN 'CLI1'.
CALL FUNCTION 'WS_FILENAME_GET'"打开文件对话框
IMPORTING
FILENAME = file1
EXCEPTIONS
INV_WINSYS = 1
NO_BATCH = 2
SELECTION_CANCEL = 3
SELECTION_ERROR = 4.
ENDCASE.
INITIALIZATION.
text1 = '上传文件:'.
btn1 = '浏览…'.
4、 对上传的excel文件进行处理,并输出系统消息
START-OF-SELECTION.
CALL FUNCTION 'WS_UPLOAD'"上传文件
EXPORTING
FILENAME = file1
TABLES
DATA_TAB = itab2
EXCEPTIONS
CONVERSION_ERROR = 1
FILE_OPEN_ERROR = 2
FILE_READ_ERROR = 3
INVALID_TYPE = 4
NO_BATCH = 5
UNKNOWN_ERROR = 6
INVALID_TABLE_WIDTH = 7
GUI_REFUSE_FILETRANSFER = 8
CUSTOMER_ERROR = 9
OTHERS = 10.
LOOP AT itab2.
SPLIT itab2 AT ',' INTO wa-MATNR wa-MBRSH wa-MTART wa-MAKTX wa-MEINS wa-MTPOS_MARA.
wa-MTPOS_MARA.
APPEND wa TO itab1.
ENDLOOP.
LOOP AT itab1 INTO wa.
PERFORM append_mm01 USING wa.
CLEAR bdcdata1[]. "清空BDCDATA的所有数据
ENDLOOP.
输出系统消息
WRITE : / '消息类型', 20 '消息内容'.
LOOP AT msgtab.
SELECT SINGLE * FROM T100 WHERE SPRSL = msgtab-MSGSPRA
AND ARBGB = msgtab-MSGID
AND MSGNR = msgtab-MSGNR.
IF SY-SUBRC = 0.
L_MSTRING = T100-TEXT.
IF L_MSTRING CS '&1'.
REPLACE '&1' WITH msgtab-MSGV1 INTO L_MSTRING.
REPLACE '&2' WITH msgtab-MSGV2 INTO L_MSTRING.
REPLACE '&3' WITH msgtab-MSGV3 INTO L_MSTRING.
REPLACE '&4' WITH msgtab-MSGV4 INTO L_MSTRING.
ELSE.
REPLACE '&' WITH msgtab-MSGV1 INTO L_MSTRING.
REPLACE '&' WITH msgtab-MSGV2 INTO L_MSTRING.
REPLACE '&' WITH msgtab-MSGV3 INTO L_MSTRING.
REPLACE '&' WITH msgtab-MSGV4 INTO L_MSTRING.
ENDIF.
CONDENSE L_MSTRING.
WRITE: / msgtab-MSGTYP, 20 L_MSTRING.
ELSE.
WRITE: / msgtab.
ENDIF.
ENDLOOP.
测试csv文件
zchen11,C,WERB,bdc1,CM,NORM
zchen12,C,WERB,bdc2,CM,NORM
zchen13,C,WERB,bdc3,CM,NORM
![44_078736_1189395066.jpg](https://i-blog.csdnimg.cn/blog_migrate/861ea28f5f5dd8d7ad3232311c0848df.png)
未命名-1.jpg
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8078736/viewspace-489094/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/8078736/viewspace-489094/