BDC 批量数据交换小示例
1.使用SHDB创建BDC录屏记录:
2.开始记录到MM02开始进行录屏操作:更改,物料描述,毛重量,净重量,重量单位数据:
3.录屏结果如下:
4.SE38创建程序:
4.1 ZTEST_BDC01页面:
*&---------------------------------------------------------------------*
*& Report ZTEST_BDC01
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZTEST_BDC01.
INCLUDE ZTEST_BDC01TOP.
INCLUDE ZTEST_BDC01FORM.
INITIALIZATION.
*SSCRFIELDS-FUNCTXT_01 = '@49@下载模板'.
*或者用以下方法加按钮图标
WRITE ICON_EXPORT AS ICON TO SSCRFIELDS-FUNCTXT_01+0(4).
SSCRFIELDS-FUNCTXT_01+4(*) = '下载模板'.
*响应屏幕PAI按钮事件
AT SELECTION-SCREEN.
PERFORM SELECTION_PAI_SCREEN.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILES.
PERFORM GET_FILENAME.
START-OF-SELECTION.
*取数
*1.从EXCEL上传
*2.手工插入数据
PERFORM GET_DATA.
END-OF-SELECTION.
*转换
*PERFORM APPEND_BDCDATA. "直接执行
PERFORM APPEND_BDCDATA_BATCH. "创建session后SM35执行
4.2 ZTEST_BDC01TOP页面:
*&---------------------------------------------------------------------*
*& 包含 ZTEST_BDC01TOP
*&---------------------------------------------------------------------*
TYPES: BEGIN OF GTY_MATERIAL,
MATNR TYPE C LENGTH 18, "物料编码
MAKTX TYPE C LENGTH 40, "物料描述
BRGEW TYPE C LENGTH 20, "毛重量
NTGEW TYPE C LENGTH 20, "净重量
GEWEI TYPE C LENGTH 3, "重量单位
END OF GTY_MATERIAL.
DATA: GT_MATERIAL TYPE TABLE OF GTY_MATERIAL,
GS_MATERIAL TYPE GTY_MATERIAL.
DATA: BEGIN OF GT_TEMPLATE OCCURS 0,
TAB1 TYPE C LENGTH 10 VALUE '物料代码' ,
TAB2 TYPE C LENGTH 10 VALUE '物料描述' ,
TAB3 TYPE C LENGTH 10 VALUE '毛重量' ,
TAB4 TYPE C LENGTH 10 VALUE '净重量' ,
TAB5 TYPE C LENGTH 10 VALUE '重量单位' ,
END OF GT_TEMPLATE.
DATA BDCDATA TYPE TABLE OF BDCDATA WITH HEADER LINE.
DATA MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.
TABLES SSCRFIELDS.
PARAMETERS P_FILES TYPE RLGRAP-FILENAME.
*应用工具栏增加按钮
SELECTION-SCREEN FUNCTION KEY 1. "数字1~5,对应界面中可以增加的5个按钮
4.3 ZTEST_BDC01FORM页面
*&---------------------------------------------------------------------*
*& 包含 ZTEST_BDC01FORM
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form APPEND_BDCDATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM APPEND_BDCDATA .
DATA LV_MESSAGE TYPE C LENGTH 200.
LOOP AT GT_MATERIAL INTO GS_MATERIAL.
CLEAR: BDCDATA, BDCDATA[], "循环执行注意清空
MESSTAB, MESSTAB[].
PERFORM APPEND_SCREEN USING 'SAPLMGMM' '0060' .
PERFORM APPEND_FIELDS USING 'BDC_CURSOR' 'RMMG1-MATNR'.
PERFORM APPEND_FIELDS USING 'BDC_OKCODE' '=ENTR'.
PERFORM APPEND_FIELDS USING 'RMMG1-MATNR' GS_MATERIAL-MATNR.
PERFORM APPEND_SCREEN USING 'SAPLMGMM' '0070' .
PERFORM APPEND_FIELDS USING 'BDC_CURSOR' 'MSICHTAUSW-DYTXT(01)'.
PERFORM APPEND_FIELDS USING 'BDC_OKCODE' '=ENTR'.
PERFORM APPEND_FIELDS USING 'MSICHTAUSW-KZSEL(01)' 'X'.
PERFORM APPEND_SCREEN USING 'SAPLMGMM' '4004' .
PERFORM APPEND_FIELDS USING 'BDC_OKCODE' '=BU'.
PERFORM APPEND_FIELDS USING 'BDC_CURSOR' 'MAKT-MAKTX'. "一个界面CURSOR有一个就行了,SUBSCR都不要
PERFORM APPEND_FIELDS USING 'MAKT-MAKTX' GS_MATERIAL-MAKTX.
PERFORM APPEND_FIELDS USING 'MARA-BRGEW' GS_MATERIAL-BRGEW.
PERFORM APPEND_FIELDS USING 'MARA-NTGEW' GS_MATERIAL-NTGEW.
PERFORM APPEND_FIELDS USING 'MARA-GEWEI' GS_MATERIAL-GEWEI.
CALL TRANSACTION 'MM02' USING BDCDATA "使用内表调用
UPDATE 'S' "更新模式: S.同步(执行到这,程序会停下来,等着执行完再往下);A 异步(执行到这就走了,分成两部分分开执行)
MODE 'N' "显示模式:A,前台 N, 后台 E,只显示错误信息
MESSAGES INTO MESSTAB .
READ TABLE MESSTAB WITH KEY MSGID = 'M3'
MSGNR = '801'.
IF SY-SUBRC = 0.
WRITE:/ '物料',GS_MATERIAL