test_bdc
【转】
REPORT zbdc_test_by_shir.
* 定义个BDC格式的内表
**************************************************
DATA : BEGIN OF i_bdcdata OCCURS 0,
*BDC的程序名字,比如事物码MM01的程序名字为SAPLMGMM(sap标准程序)
program LIKE bdcdata-program,
*BDC Dynpro 号码
dynpro LIKE bdcdata-dynpro,
*BDC BDC Dynpro 開始
dynbegin LIKE bdcdata-dynbegin,
*BDC 项目名字
fnam LIKE bdcdata-fnam,
*BDC 项目名字的附加值
fval LIKE bdcdata-fval,
END OF i_bdcdata.
*BDC
DATA: t_bdcmsg LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.
*****此方法可以用excel,txt等文件导入
DATA: BEGIN OF itab OCCURS 0,
maktx LIKE makt-maktx,
meins LIKE mara-meins,
matkl LIKE mara-matkl,
END OF itab.
DATA: tab LIKE itab OCCURS 0 WITH HEADER LINE.
DATA: BEGIN OF t_log OCCURS 0,
information(289) TYPE c, "返回信息
END OF t_log.
DATA:BEGIN OF iexcel OCCURS 0.
INCLUDE STRUCTURE alsmex_tabline.
DATA:END OF iexcel.
FIELD-SYMBOLS: <fs>.
SELECTION-SCREEN BEGIN OF BLOCK text WITH FRAME TITLE text-001.
PARAMETERS:p_fname LIKE rlgrap-filename .
SELECTION-SCREEN END OF BLOCK text.
*itab-a = 'A0010000000'.
*APPEND itab.
*itab-a = 'A0010000001'.
*APPEND itab.
*itab-maktx = '测试BDC导入数据'.
*itab-meins = 'BAG'.
*itab-matkl = '100106'.
*
*APPEND itab.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.
PERFORM upload USING p_fname .
* START-OF-SELECTION
START-OF-SELECTION.
LOOP AT itab.
PERFORM append_mm01 USING itab-maktx itab-meins itab-matkl.
PERFORM run_bdc.
ENDLOOP.
*---------------------------------------------------------------------*
* FORM APPEND_DATA_MM01 *
*---------------------------------------------------------------------*
* 定义一个子程序里面,用来存放操作的各个步骤 *
*---------------------------------------------------------------------*
FORM append_mm01 USING fval LIKE itab-maktx
fval1 LIKE itab-meins
fval2 LIKE itab-matkl.
CLEAR i_bdcdata.
CLEAR i_bdcdata[].
*开始新的画面事件
*0173画面
*把程序名字和 BDC DYnpro号码,开始标志 写入内表表头,
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' ''.
PERFORM bdc_field USING 'RMMG1-MBRSH' 'Z'.
PERFORM bdc_field USING 'RMMG1-MTART' 'Z001'.
*开始新的画面事件
PERFORM bdc_dynpro USING 'SAPLMGMM' '0070'.
PERFORM bdc_field USING 'BDC_CURSOR' 'MSICHTAUSW-DYTXT(01)'.
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' itab-maktx.
PERFORM bdc_field USING 'MARA-MEINS' itab-meins.
PERFORM bdc_field USING 'MARA-MATKkL' itab-matkl.
ENDFORM. "APPEND_MM01
*---------------------------------------------------------------------*
* FORM RUN_BDC *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM run_bdc.
*开始调用事务MM01,把做好的bdc对象传递过去,然后把模式设置为A,
*就是说所有窗口,均可以看见.
* N 表示不看见吧
REFRESH t_bdcmsg.
CLEAR t_bdcmsg.
CALL TRANSACTION 'MM01' USING i_bdcdata MODE 'N' MESSAGES INTO t_bdcmsg.
READ TABLE t_bdcmsg WITH KEY msgtyp = 'S'
msgid = 'M3'
msgnr = '800'.
IF sy-subrc EQ 0.
CONCATENATE '物料号:' t_bdcmsg-msgv1 '创建成功' INTO t_log.
ELSE.
LOOP AT t_bdcmsg.
IF t_bdcmsg-msgtyp = 'E'
OR t_bdcmsg-msgv1 <> ''
OR t_bdcmsg-msgv2 <> ''
OR t_bdcmsg-msgv3 <> ''
OR t_bdcmsg-msgv4 <> ''.
MESSAGE ID t_bdcmsg-msgid "收集错误信息,并写入自定义的表t_log中。
TYPE 'S'
NUMBER t_bdcmsg-msgnr
WITH t_bdcmsg-msgv1
t_bdcmsg-msgv2
t_bdcmsg-msgv3
t_bdcmsg-msgv4
INTO t_log-information.
CONCATENATE
t_bdcmsg-msgv1
* itab-posnr
'出错:'
t_log-information
INTO t_log-information.
APPEND t_log.
CLEAR t_log.
ENDIF.
ENDLOOP.
ENDIF.
LOOP AT t_log. "显示出错信息
WRITE:/ t_log-information.
ENDLOOP.
ENDFORM. "RUN_BDC
*&---------------------------------------------------------------------*
*& Form BDC_DYNPRO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0121 text
* -->P_0122 text
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program
dynpro.
i_bdcdata-program = program.
i_bdcdata-dynpro = dynpro.
i_bdcdata-dynbegin = 'X'.
APPEND i_bdcdata.
CLEAR i_bdcdata.
ENDFORM. " BDC_DYNPRO
*&---------------------------------------------------------------------*
*& Form BDC_FIELD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0126 text
* -->P_0127 text
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
i_bdcdata-fnam = fnam .
i_bdcdata-fval = fval.
APPEND i_bdcdata.
CLEAR i_bdcdata.
ENDFORM. " BDC_FIELD
*&---------------------------------------------------------------------*
*& Form UPLOAD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM upload USING p_fname.
DATA:cc TYPE i.
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
mask = ',Excel Files,*.xls,All Files,*.*.'(101)
title = '选择文件'(100)
IMPORTING
filename = p_fname
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
IF sy-subrc <> 0 AND sy-subrc <> 3.
MESSAGE e398(00) WITH '选择文件出错!'(007).
ENDIF.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = p_fname
i_begin_col = 1
i_begin_row = 1
i_end_col = 100
i_end_row = 1000
TABLES
intern = iexcel
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
IF sy-subrc <> 0.
WRITE:/'文件读入错误!',p_fname.
ELSE.
cc = 1.
LOOP AT iexcel.
ASSIGN COMPONENT cc OF
STRUCTURE itab TO <fs>.
<fs> = iexcel-value(25).
cc = cc + 1.
AT END OF row.
IF iexcel-row <> '0001'.
APPEND itab.
ENDIF.
CLEAR itab.
cc = 1.
ENDAT.
ENDLOOP.
ENDIF.
ENDFORM. " UPLOAD