一、定义变量

type-POOLs:TRUXS,slis.

TYPES: BEGIN OF ty_input ,
matnr TYPE mara-matnr , " 物料号
maktx TYPE makt-maktx,"物料描述
END OF ty_input . DATA: gt_input TYPE STANDARD TABLE OF ty_input.
FIELD-SYMBOLS: <gs_input> TYPE ty_input.
DATA: gt_bdcdata TYPE STANDARD TABLE OF bdcdata,"bdc
gt_msg TYPE STANDARD TABLE OF bdcmsgcoll,"消息
gs_msg TYPE bdcmsgcoll."消息 TYPES: BEGIN OF ty_message ,
matnr TYPE mara-matnr , " 物料号
maktx TYPE makt-maktx, "描述
msg TYPE string ,
END OF ty_message . DATA: gt_message TYPE STANDARD TABLE OF ty_message,
gs_message TYPE ty_message.
*alv
DATA: gt_fieldcat TYPE slis_t_fieldcat_alv.
DATA: gs_fieldcat TYPE LINE OF slis_t_fieldcat_alv.
DATA: gs_layout TYPE slis_layout_alv.

二、屏幕元素

SELECTION-SCREEN BEGIN OF BLOCK bk1 WITH FRAME TITLE text-.      "选择文件路径
PARAMETERS: p_ufile LIKE rlgrap-filename OBLIGATORY MEMORY ID p_ufile.
SELECTION-SCREEN END OF BLOCK bk1.

三、调用代码

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_ufile.
PERFORM get_filepath. START-OF-SELECTION. PERFORM upload_data. "上载文本资料 PERFORM f_bdc. END-OF-SELECTION. PERFORM f_alv_fieldcat.
PERFORM f_layout.
PERFORM f_display.

四、调用的form

FORM get_filepath .

  CALL FUNCTION 'TB_LIMIT_WS_FILENAME_GET'
EXPORTING
def_path = ''
mask = ',Excel Files,*.xls,All Files,*.*.'
title = 'Select the File'
IMPORTING
filename = p_ufile
EXCEPTIONS
selection_cancel =
selection_error =
OTHERS = .
IF sy-subrc <> .
MESSAGE '请输入文件名' TYPE 'S'.
ELSEIF sy-subrc = .
MESSAGE '成功' TYPE 'S'.
ENDIF. ENDFORM. FORM upload_data . DATA:t_raw_data TYPE truxs_t_text_data. CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP' "读取excel到sap的函数
EXPORTING
i_line_header = 'X'
i_tab_raw_data = t_raw_data
i_filename = p_ufile
TABLES
i_tab_converted_data = gt_input
EXCEPTIONS
conversion_failed =
OTHERS = .
IF sy-subrc <> .
ENDIF. IF gt_input IS INITIAL.
MESSAGE '无数据' TYPE 'S' DISPLAY LIKE 'E'.
ENDIF. ENDFORM. " UPLOAD_DATA FORM f_bdc . DATA: inx TYPE sy-tabix. LOOP AT gt_input ASSIGNING <gs_input>. CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
input = <gs_input>-matnr
IMPORTING
output = <gs_input>-matnr
EXCEPTIONS
length_error =
OTHERS = .
IF sy-subrc <> .
* Implement suitable error handling here
ENDIF. FREE:gt_bdcdata,gt_msg. "这些步骤都是根据bdc里面生成的来写的,类似BDC_SUBSCR之类的可以省去不写 PERFORM f_bdcdata TABLES gt_bdcdata USING : 'SAPLMGMM' '' 'X' '' ''."打开屏幕
PERFORM f_bdcdata TABLES gt_bdcdata USING : '' '' '' 'BDC_CURSOR' 'RMMG1-MATNR'. "光标
PERFORM f_bdcdata TABLES gt_bdcdata USING : '' '' '' 'BDC_OKCODE' '=ENTR'."操作按钮(回车)
PERFORM f_bdcdata TABLES gt_bdcdata USING : '' '' '' 'RMMG1-MATNR' <gs_input>-matnr."输入数据(物料号)
"PERFORM f_bdcdata TABLES gt_bdcdata USING : '' '' '' 'R62CLORD-FLG_OVIEW' 'X'. PERFORM f_bdcdata TABLES gt_bdcdata USING : 'SAPLMGMM' '' 'X' '' ''."打开屏幕
PERFORM f_bdcdata TABLES gt_bdcdata USING : '' '' '' 'BDC_OKCODE' '=ENTR'."操作按钮(回车)
PERFORM f_bdcdata TABLES gt_bdcdata USING : '' '' '' 'MSICHTAUSW-KZSEL(01)' 'X'."选择视图01(基本数据)
" PERFORM f_bdcdata TABLES gt_bdcdata USING : '' '' '' 'R62CLORD-FLG_OVIEW' 'X'. PERFORM f_bdcdata TABLES gt_bdcdata USING : 'SAPLMGMM' '' 'X' '' ''.
PERFORM f_bdcdata TABLES gt_bdcdata USING : '' '' '' 'BDC_OKCODE' '=BU'."操作组件按钮
PERFORM f_bdcdata TABLES gt_bdcdata USING : '' '' '' 'MAKT-MAKTX' <gs_input>-maktx."输入数据(物料描述) FREE: gt_msg.
CALL TRANSACTION 'MM02' USING gt_bdcdata
MODE 'N' "执行事务,传递bdc参数,模式为后台模式
"MODE 'A' "前台模式,用户可以看到执行的过程,需要自己点enter键
MESSAGES INTO gt_msg. "message可省去
READ TABLE gt_msg INTO gs_msg WITH KEY msgtyp = 'E'.
IF sy-subrc = .
MOVE-CORRESPONDING <gs_input> TO gs_message.
LOOP AT gt_msg INTO gs_msg.
DATA: return TYPE bapiret2.
CLEAR: return.
PERFORM f_msg USING gs_msg-msgtyp gs_msg-msgid gs_msg-msgnr gs_msg-msgv1 gs_msg-msgv2 gs_msg-msgv3 gs_msg-msgv4 CHANGING return.
IF return-message IS NOT INITIAL.
gs_message-msg = return-message.
ENDIF.
APPEND gs_message TO gt_message.
ENDLOOP.
ROLLBACK WORK.
ELSE.
CLEAR: gs_message.
MOVE-CORRESPONDING <gs_input> TO gs_message.
gs_message-msg = 'OK'.
APPEND gs_message TO gt_message.
COMMIT WORK.
ENDIF. ENDLOOP. ENDFORM. FORM f_alv_fieldcat . DATA: pos TYPE i. DEFINE fieldcat.
clear gs_fieldcat.
pos = pos + .
gs_fieldcat-col_pos = pos.
gs_fieldcat-fieldname = &.
gs_fieldcat-ref_tabname = &.
gs_fieldcat-ref_fieldname = &.
* gs_fieldcat-seltext_l = &.
* gs_fieldcat-no_zero = &.
* gs_fieldcat-key = &.
gs_fieldcat-just = 'L'.
append gs_fieldcat to gt_fieldcat.
END-OF-DEFINITION. fieldcat 'MATNR' 'MARA' 'MATNR'.
fieldcat 'MAKTX' 'MAKT' 'MAKTX' .
fieldcat 'MSG' '' '消息' . ENDFORM. FORM f_layout .
gs_layout-zebra = 'X'.
gs_layout-colwidth_optimize = 'X'.
* gs_layout-box_fieldname = 'BOX'.
ENDFORM. FORM f_display . * 可以保存格式
DATA:gs_variant TYPE disvariant.
gs_variant-report = sy-repid. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
i_save = 'A' "可以保存格式
is_variant = gs_variant "LAYOUT参数 可以保存格式
is_layout = gs_layout
it_fieldcat = gt_fieldcat
TABLES
t_outtab = gt_message
EXCEPTIONS
program_error =
OTHERS = . ENDFORM. FORM f_msg USING itype icl inumber ipar1 ipar2 ipar3 ipar4 CHANGING return. DATA: type TYPE bapireturn-type,
cl TYPE sy-msgid,
number TYPE sy-msgno,
par1 TYPE sy-msgv1,
par2 TYPE sy-msgv2,
par3 TYPE sy-msgv3,
par4 TYPE sy-msgv4. CLEAR: type,cl,number,par1,par2,par3,par4. type = itype.
cl = icl.
number = inumber.
par1 = ipar1.
par2 = ipar2.
par3 = ipar3.
par4 = ipar4. CALL FUNCTION 'BALW_BAPIRETURN_GET2'
EXPORTING
type = type
cl = cl
number = number
par1 = par1
par2 = par2
par3 = par3
par4 = par4
IMPORTING
return = return. ENDFORM. FORM f_bdcdata TABLES gt_bdcdata STRUCTURE bdcdata
USING p_program
p_dynpro
p_dynbegin
p_fnam
p_fval.
DATA: gs_bdcdata TYPE bdcdata. CLEAR gs_bdcdata. gs_bdcdata-program = p_program.
gs_bdcdata-dynpro = p_dynpro.
gs_bdcdata-dynbegin = p_dynbegin.
gs_bdcdata-fnam = p_fnam.
gs_bdcdata-fval = p_fval.
APPEND gs_bdcdata TO gt_bdcdata. ENDFORM.
05-26 18:56