事务代码:IL01,更改功能位置。
输入功能位置,功能位置描述,维护工厂,结构标识,功能位置种类,成本中心,计划工厂,投运日期,在功能位置所允许的设备安装。
实际业务中需要导入大量的功能位置数据,因此需要使用BAPI批量导入功能位置。可使用BAPI:BAPI_FUNCLOC_CREATE批量导入功能。
BAPI参数声明:
TABLES:sscrfields.
*----------------------------------------------------------------------*
* 定义内表
*----------------------------------------------------------------------*
DATA:BEGIN OF gt_input OCCURS 0,
eid(6) TYPE c,
* tplkz TYPE iflot-tplkz, "结构标识
* fltyp TYPE iflot-fltyp, "功能位置种类
tplnr TYPE iflot-tplnr, "功能位置编码
pltxt TYPE iflotx-pltxt, "功能位置名称
kostl TYPE iloa-kostl, "成本中心
swerk TYPE iloa-swerk, "维护工厂
iwerk TYPE iflot-iwerk, "计划工厂
erdat TYPE iflot-erdat, "投运日期
iequi TYPE iflot-iequi, "在功能位置所允许的设备安装
status(20) TYPE c, "导入是否成功状态
drjg TYPE c,
message(100) TYPE c,
cjr(10) TYPE c,
date TYPE iflot-erdat,
END OF gt_input.
DATA:gt_output LIKE gt_input OCCURS 0 WITH HEADER LINE.
DATA: gt_specific LIKE bapi_itob_fl_only OCCURS 0 WITH HEADER LINE,
gt_general LIKE bapi_itob OCCURS 0 WITH HEADER LINE,
g_funcloc TYPE bapi_itob_parms-funcloc,
gt_return LIKE bapiret2 OCCURS 0 WITH HEADER LINE.
* gt_return TYPE TABLE OF bapiret2.
*----------------------------------------------------------------------*
* 定义alv数据对象
*----------------------------------------------------------------------*
DATA: t_fieldcat TYPE slis_t_fieldcat_alv,
w_fieldcat TYPE slis_fieldcat_alv,
w_layout TYPE slis_layout_alv.
*----------------------------------------------------------------------*
* 定义选择屏幕
*----------------------------------------------------------------------*
SELECTION-SCREEN FUNCTION KEY 1.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
PARAMETERS: p_infile TYPE rlgrap-filename OBLIGATORY MEMORY ID m01.
SELECTION-SCREEN END OF BLOCK b1.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_infile.
PERFORM frm_get_path."获取路径
AT SELECTION-SCREEN.
PERFORM frm_download_excel."下载模板
AT SELECTION-SCREEN OUTPUT.
PERFORM frm_set_screen."屏幕设置
INITIALIZATION.
PERFORM frm_screen_ini."初始化屏幕
START-OF-SELECTION.
PERFORM frm_check_data1."上传文件校验
PERFORM frm_upload_data."上载数据
PERFORM frm_call_bapi."调用BAPI
PERFORM frm_display_alv."ALV显示
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*& Form FRM_GET_PATH
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_get_path .
CALL FUNCTION 'TB_LIMIT_WS_FILENAME_GET'
EXPORTING
* DEF_FILENAME = ' '
* DEF_PATH = ' '
mask = 'Excel Files,*.xlsx,*.*. '
* MODE = ' '
* TITLE = ' '
IMPORTING
filename = p_infile
* PATH =
* FILE =
EXCEPTIONS
selection_cancel = 1
selection_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
* MESSAGE '获取文件路径失败!' TYPE 'E'.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DOWNLOAD_EXCEL
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_download_excel .
CASE sscrfields-ucomm .
WHEN 'FC01'.
DATA: l_filename TYPE string,
l_path TYPE string,
l_fullpath TYPE string,
l_filelength TYPE i.
PERFORM frm_filesave_dialog USING '功能位置批量导入模板.xls'
CHANGING l_filename
l_path
l_fullpath.
PERFORM frm_download_files USING l_fullpath .
CLEAR sscrfields-ucomm .
WHEN OTHERS .
ENDCASE .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DOWNLOAD_FILES
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> L_FULLPATH
*&---------------------------------------------------------------------*
FORM frm_download_files USING p_fullpath.
DATA:ls_key TYPE wwwdatatab,
lv_rc TYPE sy-subrc.
DATA: l_destination LIKE rlgrap-filename .
l_destination = p_fullpath .
SELECT SINGLE relid objid INTO CORRESPONDING FIELDS OF ls_key
FROM wwwdata
WHERE srtf2 EQ 0
AND relid EQ 'MI'
* AND objid EQ lv_objid.
AND objid EQ sy-cprog.
IF sy-subrc NE 0.
MESSAGE '模板文件不存在' TYPE 'E'.
RETURN.
ENDIF.
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
key = ls_key
destination = l_destination
IMPORTING
rc = lv_rc.
IF lv_rc <> 0.
MESSAGE '模板文件:下载失败,请与相关人员联系' TYPE 'E'.
RETURN.
ENDIF.
DATA: application TYPE ole2_object,
workbook TYPE ole2_object,
sheet TYPE ole2_object.
*&& 打开模版 文件
CREATE OBJECT application 'EXCEL.APPLICATION'.
IF sy-subrc NE 0.
MESSAGE '不能创建Excel对象' TYPE 'E'.
RETURN.
ENDIF.
CALL METHOD OF
application
'WORKBOOKS' = workbook.
CALL METHOD OF
workbook
'OPEN'
EXPORTING
#1 = l_destination.
CALL METHOD OF
application
'WORKSHEETS' = sheet
EXPORTING
#1 = 1.
GET PROPERTY OF application 'ACTIVESHEET' = sheet.
SET PROPERTY OF application 'VISIBLE' = 1.
FREE sheet.
FREE workbook.
FREE application.
CALL METHOD OF
workbook
'CLOSE'.
CALL METHOD OF
application
'QUIT'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_FILESAVE_DIALOG
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> P_
*& <-- L_FILENAME
*& <-- L_PATH
*& <-- L_FULLPATH
*&---------------------------------------------------------------------*
FORM frm_filesave_dialog USING p_value
CHANGING p_filename
p_path
p_fullpath.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
default_file_name = p_value
default_extension = 'xls'
CHANGING
filename = p_filename
path = p_path
fullpath = p_fullpath
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_SCREEN
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_set_screen .
LOOP AT SCREEN.
IF screen-name = 'P_INFILE'.
screen-required = '2'.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SCREEN_INI
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_screen_ini .
sscrfields-functxt_01 = '模板下载'. "模板下载
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_CHECK_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_check_data1 .
IF p_infile IS INITIAL.
MESSAGE '文件路径不能为空!' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_UPLOAD_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_upload_data .
DATA: l_infile TYPE string.
l_infile = p_infile.
DATA: t_raw_data TYPE truxs_t_text_data.
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
* I_FIELD_SEPERATOR =
i_line_header = 'X'
i_tab_raw_data = t_raw_data
i_filename = p_infile
TABLES
i_tab_converted_data = gt_input
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_CALL_BAPI
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_call_bapi .
DATA tlen TYPE i.
LOOP AT gt_input.
tlen = strlen( gt_input-tplnr ).
g_funcloc = gt_input-tplnr.
gt_general-maintplant = gt_input-swerk.
gt_general-planplant = gt_input-iwerk.
gt_general-descript = gt_input-pltxt.
gt_general-costcenter = gt_input-kostl.
gt_general-read_crdat = gt_input-erdat.
APPEND gt_general.
* gt_specific-strind = gt_input-tplkz.
gt_specific-strind = 'CDFLS'.
* gt_specific-category = gt_input-fltyp.
gt_specific-category = 'M'.
IF tlen LT 15.
IF tlen = 2.
gt_specific-supfloc = gt_input-tplnr+0(2).
ENDIF.
IF tlen = 7.
gt_specific-supfloc = gt_input-tplnr+0(2).
ENDIF.
IF tlen = 12.
gt_specific-supfloc = gt_input-tplnr+0(7).
ENDIF.
ELSEIF tlen GE 15.
gt_specific-supfloc = gt_input-tplnr+0(15).
ENDIF.
gt_specific-eqinstall = gt_input-iequi.
APPEND gt_specific.
CALL FUNCTION 'BAPI_FUNCLOC_CREATE'
EXPORTING
external_number = g_funcloc
* LABELING_SYSTEM =
data_general = gt_general
data_specific = gt_specific
* AUTOMATIC_INSTALL =
IMPORTING
* FUNCTLOCATION =
* DATA_GENERAL_EXP =
* DATA_SPECIFIC_EXP =
return = gt_return
* TABLES
* EXTENSIONIN =
* EXTENSIONOUT =
.
IF syst-msgty = 'I'.
gt_output-drjg = syst-msgty.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
INTO gt_output-message
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ELSEIF syst-msgty = 'S'.
gt_output-drjg = syst-msgty.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' .
WAIT UP TO '0.1' SECONDS.
ENDIF.
IF gt_output-drjg = 'I'.
gt_output-status = icon_led_red.
ELSEIF gt_output-drjg = 'S'.
gt_output-status = icon_led_green.
ENDIF.
gt_output-eid = gt_input-eid.
gt_output-tplnr = gt_input-tplnr.
gt_output-pltxt = gt_input-pltxt.
gt_output-swerk = gt_input-swerk.
gt_output-cjr = sy-uname.
gt_output-date = sy-datum.
APPEND gt_output.
CLEAR gt_output.
CLEAR gt_return.
REFRESH gt_return.
CLEAR g_funcloc.
CLEAR gt_general.
REFRESH gt_general.
CLEAR gt_specific.
REFRESH gt_specific.
CLEAR tlen.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_display_alv .
PERFORM frm_fill_fieldcat.
PERFORM frm_alv_layout.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_buffer_active = 'X'
i_callback_program = sy-repid
is_layout = w_layout
it_fieldcat = t_fieldcat
* i_callback_pf_status_set = 'SET_PF_STATUS'
i_callback_user_command = 'USER_COMMAND'
TABLES
t_outtab = gt_output
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_FILL_FIELDCAT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_fill_fieldcat .
CLEAR w_fieldcat.
w_fieldcat-fieldname = 'EID'.
w_fieldcat-seltext_m = '导入编码'.
APPEND w_fieldcat TO t_fieldcat.
CLEAR w_fieldcat.
w_fieldcat-fieldname = 'STATUS'.
w_fieldcat-seltext_m = '指示灯'.
w_fieldcat-icon = 'X'.
APPEND w_fieldcat TO t_fieldcat.
CLEAR w_fieldcat.
w_fieldcat-fieldname = 'DRJG'.
w_fieldcat-seltext_m = '导入结果'.
APPEND w_fieldcat TO t_fieldcat.
CLEAR w_fieldcat.
w_fieldcat-fieldname = 'MESSAGE'.
w_fieldcat-seltext_m = '消息'.
APPEND w_fieldcat TO t_fieldcat.
CLEAR w_fieldcat.
w_fieldcat-fieldname = 'TPLNR'.
w_fieldcat-seltext_m = '功能位置编码'.
w_fieldcat-no_zero = 'X'.
APPEND w_fieldcat TO t_fieldcat.
CLEAR w_fieldcat.
w_fieldcat-fieldname = 'PLTXT'.
w_fieldcat-seltext_m = '导入功能位置名称'.
APPEND w_fieldcat TO t_fieldcat.
CLEAR w_fieldcat.
w_fieldcat-fieldname = 'SWERK'.
w_fieldcat-seltext_m = '维护工厂'.
APPEND w_fieldcat TO t_fieldcat.
CLEAR w_fieldcat.
w_fieldcat-fieldname = 'CJR'.
w_fieldcat-seltext_m = '创建人'.
APPEND w_fieldcat TO t_fieldcat.
CLEAR w_fieldcat.
w_fieldcat-fieldname = 'DATE'.
w_fieldcat-seltext_m = '日期时间'.
APPEND w_fieldcat TO t_fieldcat.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_ALV_LAYOUT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_alv_layout .
w_layout-colwidth_optimize = 'X'.
ENDFORM.
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
CASE r_ucomm.
WHEN '&IC1'.
IF rs_selfield-value IS INITIAL.
r_ucomm = '&ETA'.
ENDIF.
* CHECK rs_selfield-value IS NOT INITIAL.
* READ TABLE it_tab INTO wa_tab INDEX rs_selfield-tabindex.
CASE rs_selfield-fieldname.
WHEN 'TPLNR'. "交货单号
SET PARAMETER ID 'IFL' FIELD rs_selfield-value.
CALL TRANSACTION 'IL02' AND SKIP FIRST SCREEN.
WHEN OTHERS.
r_ucomm = '&ETA'.
ENDCASE.
ENDCASE.
ENDFORM.