代码
代码语言:javascript复制<span style="color:#000000"><span style="background-color:#f5f2f0"><code class="language-bash"> TABLES sscrfields.
*----------------------------------------------------------------------*
* CLASS lcl_events DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_events DEFINITION.
PUBLIC SECTION.
METHODS: on_user_command "定义设置SALV on_user_command属性的方法
FOR EVENT added_function OF cl_salv_events_table
IMPORTING e_salv_function.
ENDCLASS. "lcl_events DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_events IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_events IMPLEMENTATION.
METHOD on_user_command.
PERFORM frm_on_user_command USING e_salv_function.
ENDMETHOD. "on_user_command
ENDCLASS. "lcl_events IMPLEMENTATION
TYPES: BEGIN OF ty_out,
icon TYPE char4,
text TYPE char30,
matnr TYPE mara-matnr,
werks TYPE t001w-werks,
END OF ty_out.
DATA: go_alv TYPE REF TO cl_salv_table.
DATA: gt_out TYPE TABLE OF ty_out,
wa_out TYPE ty_out.
TYPE-POOLS: truxs.
TABLES: ztmm_sfzs.
DATA: gs_text TYPE smp_dyntxt.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE l_title1.
PARAMETERS: p_import TYPE c RADIOBUTTON GROUP r1 USER-COMMAND uc01 DEFAULT'X',
p_delete TYPE c RADIOBUTTON GROUP r1,
p_query TYPE c RADIOBUTTON GROUP r1.
SELECTION-SCREEN END OF BLOCK b1.
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE l_title2.
PARAMETERS : p_file TYPE string OBLIGATORY DEFAULT'C:TEMPLATE.XLSX' MODIF ID m1.
SELECT-OPTIONS: s_matnr FOR ztmm_sfzs-matnr MODIF ID m2,
s_werks FOR ztmm_sfzs-werks MODIF ID m2.
SELECTION-SCREEN END OF BLOCK b2.
SELECTION-SCREEN FUNCTION KEY 1.
SELECTION-SCREEN FUNCTION KEY 2.
INITIALIZATION.
PERFORM frm_init_funckey.
l_title1 = '选择功能'.
l_title2 = '选择条件'.
AT SELECTION-SCREEN OUTPUT.
CASE 'X'.
WHEN p_import OR p_delete.
IF p_import = 'X'.
gs_text-icon_id = icon_execute_object.
gs_text-text = '执行新增数据'.
gs_text-icon_text = '执行新增'.
ELSE.
gs_text-icon_id = icon_execute_object.
gs_text-text = '执行删除数据'.
gs_text-icon_text = '执行删除'.
ENDIF.
LOOP AT SCREEN.
IF screen-group1 = 'M2'.
screen-active = 0.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
WHEN p_query.
LOOP AT SCREEN.
IF screen-group1 = 'M1'.
screen-active = 0.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
WHEN OTHERS.
ENDCASE.
AT SELECTION-SCREEN.
IF sscrfields-ucomm = 'FC01'.
PERFORM frm_download_template.
ELSEIF sscrfields-ucomm = 'FC02'.
PERFORM frm_skip_old_tcode.
ENDIF.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
PERFORM frm_f4_request.
START-OF-SELECTION.
CASE 'X'.
WHEN p_import OR p_delete.
PERFORM frm_read_excel.
WHEN p_query.
PERFORM frm_get_database.
WHEN OTHERS.
ENDCASE.
END-OF-SELECTION.
PERFORM frm_show_alv.
*&---------------------------------------------------------------------*
*& Form FRM_F4_REQUEST
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_f4_request .
DATA: lv_rc TYPE i.
DATA: lt_file_table TYPE filetable,
wa_file_table TYPE file_table.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
window_title = '打开文件'
CHANGING
file_table = lt_file_table
rc = lv_rc.
IF sy-subrc = 0.
READ TABLE lt_file_table INTO wa_file_table INDEX 1.
p_file = wa_file_table-filename.
ENDIF.
ENDFORM. " FRM_F4_REQUEST
*&---------------------------------------------------------------------*
*& Form FRM_READ_EXCEL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_read_excel.
TYPES: BEGIN OF ty_marc,
matnr TYPE marc-matnr,
werks TYPE marc-werks,
END OF ty_marc.
DATA: lt_raw TYPE truxs_t_text_data.
DATA: lt_marc TYPE TABLE OF ty_marc.
DATA: lt_ztmm_sfzs TYPE TABLE OF ztmm_sfzs.
DATA: l_tabix TYPE sy-tabix.
TYPES: BEGIN OF lty_upload,
matnr TYPE c LENGTH 18,
werks TYPE c LENGTH 4,
END OF lty_upload.
DATA: lt_upload TYPE TABLE OF lty_upload.
DATA: wa_upload TYPE lty_upload.
DATA: lv_file TYPE rlgrap-filename.
DATA: l_matnr TYPE mara-matnr.
lv_file = p_file.
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
* I_FIELD_SEPERATOR =
i_line_header = 'X'
i_tab_raw_data = lt_raw
i_filename = lv_file
TABLES
i_tab_converted_data = lt_upload
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
LOOP AT lt_upload INTO wa_upload.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
input = wa_upload-matnr
IMPORTING
output = wa_out-matnr.
wa_out-werks = wa_upload-werks.
wa_out-icon = icon_green_light.
wa_out-text = '此记录将被导入'.
APPEND wa_out TO gt_out.
ENDLOOP.
DELETE ADJACENT DUPLICATES FROM gt_out COMPARING ALL FIELDS.
"导入模式
IF p_import = 'X'. "导入新增
SELECT matnr werks FROM marc
INTO TABLE lt_marc
FOR ALL ENTRIES IN gt_out
WHERE matnr = gt_out-matnr
AND werks = gt_out-werks.
LOOP AT gt_out INTO wa_out.
l_tabix = sy-tabix.
READ TABLE lt_marc TRANSPORTING NO FIELDS WITH KEY matnr = wa_out-matnr werks = wa_out-werks.
IF sy-subrc NE 0.
wa_out-icon = icon_red_light.
wa_out-text = '工厂不存在此物料,禁止导入!'.
ENDIF.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT'
EXPORTING
input = wa_out-matnr
IMPORTING
output = l_matnr.
IF wa_out-werks (1) NE 'X'. "xx工厂
wa_out-icon = icon_red_light.
wa_out-text = '非xx工厂,禁止导入!'.
ENDIF.
IF l_matnr (1) NE '2'. "xxx物料
wa_out-icon = icon_red_light.
wa_out-text = 'xxx物料,禁止导入!'.
ENDIF.
CLEAR: l_matnr.
sy-tabix = l_tabix.
MODIFY gt_out FROM wa_out.
ENDLOOP.
ELSEIF p_delete = 'X'. "导入删除
SELECT * FROM ztmm_sfzs "查询数据库中存在的记录
INTO TABLE lt_ztmm_sfzs
FOR ALL ENTRIES IN gt_out
WHERE matnr = gt_out-matnr
AND werks = gt_out-werks.
SORT lt_ztmm_sfzs BY matnr werks.
LOOP AT gt_out INTO wa_out.
l_tabix = sy-tabix.
READ TABLE lt_ztmm_sfzs TRANSPORTING NO FIELDS WITH KEY
matnr = wa_out-matnr werks = wa_out-werks BINARY SEARCH.
IF sy-subrc = 0.
wa_out-icon = icon_green_light.
wa_out-text = '此纪录将被删除!'.
ELSE.
wa_out-icon = icon_red_light.
wa_out-text = '此纪录不存在数据库,禁止导入!'.
ENDIF.
sy-tabix = l_tabix.
MODIFY gt_out FROM wa_out.
ENDLOOP.
ENDIF.
ENDFORM. " FRM_READ_EXCEL
*&---------------------------------------------------------------------*
*& Form FRM_SHOW_ALV
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_show_alv .
DATA: lo_columns TYPE REF TO cl_salv_columns.
DATA: lo_column TYPE REF TO cl_salv_column.
DATA: lo_display TYPE REF TO cl_salv_display_settings.
"注册事件
DATA: lo_events_c TYPE REF TO lcl_events,
lo_events TYPE REF TO cl_salv_events_table.
CREATE OBJECT lo_events_c.
"show data
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = go_alv
CHANGING
t_table = gt_out[] ).
CATCH cx_salv_msg.
ENDTRY.
lo_columns = go_alv->get_columns( ). "获取对象
lo_columns->set_optimize( 'X' ). "设置行宽自适应
TRY.
lo_column = lo_columns->get_column( 'ICON' ). "获取列
lo_column->set_long_text( '状态' ). "设置列长文本描述
lo_column->set_medium_text( '状态' ). "设置列中文本描述
lo_column->set_short_text( '状态' ). "设置列短文本描述
IF p_query = 'X'.
lo_column->set_visible( if_salv_c_bool_sap=>false ).
ENDIF.
CATCH cx_salv_not_found. "#EC NO_HANDLER
ENDTRY.
TRY.
lo_column = lo_columns->get_column( 'TEXT' ). "获取列
lo_column->set_long_text( '状态文本' ). "设置列长文本描述
lo_column->set_medium_text( '状态文本' ). "设置列中文本描述
lo_column->set_short_text( '状态文本' ). "设置列短文本描述
IF p_query = 'X'.
lo_column->set_visible( if_salv_c_bool_sap=>false ).
ENDIF.
CATCH cx_salv_not_found. "#EC NO_HANDLER
ENDTRY.
CASE 'X'.
WHEN p_import OR p_delete.
"设置自定义状态栏
go_alv->set_screen_status(
pfstatus = 'STANDARD_01'
report = 'ZMMT006'
set_functions = go_alv->c_functions_all ).
WHEN p_query.
"设置自定义状态栏
go_alv->set_screen_status(
pfstatus = 'STANDARD_02'
report = 'ZMMT006'
set_functions = go_alv->c_functions_all ).
WHEN OTHERS.
ENDCASE.
lo_events = go_alv->get_event( ). "获取事件对象
SET HANDLER lo_events_c->on_user_command FOR lo_events. "注册on_user_command事件
lo_display = go_alv->get_display_settings( ). "获取对象 "
lo_display->set_list_header( '维护xxxx信息!' ). "设置GUI抬头文本
go_alv->display( ).
ENDFORM. " FRM_SHOW_ALV
*&---------------------------------------------------------------------*
*& Form FRM_on_user_command
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_E_SALV_FUNCTION text
*----------------------------------------------------------------------*
FORM frm_on_user_command USING p_ucomm.
DATA: lt_ztmm_sfzs TYPE TABLE OF ztmm_sfzs,
wa_ztmm_sfzs TYPE ztmm_sfzs.
DATA: is_stable TYPE lvc_s_stbl.
CASE p_ucomm.
WHEN 'EXEC'.
LOOP AT gt_out INTO wa_out WHERE icon = icon_green_light.
MOVE-CORRESPONDING wa_out TO wa_ztmm_sfzs.
APPEND wa_ztmm_sfzs TO lt_ztmm_sfzs.
ENDLOOP.
IF lt_ztmm_sfzs IS NOT INITIAL.
IF p_import = 'X'.
MODIFY ztmm_sfzs FROM TABLE lt_ztmm_sfzs[].
ELSE.
DELETE ztmm_sfzs FROM TABLE lt_ztmm_sfzs[].
ENDIF.
IF sy-subrc = 0.
LOOP AT gt_out INTO wa_out WHERE icon = icon_green_light.
wa_out-text = '导入SAP成功!'.
MODIFY gt_out FROM wa_out.
ENDLOOP.
ENDIF.
ENDIF.
WHEN OTHERS.
ENDCASE.
is_stable-row = 'X'. "保持行
is_stable-col = 'X'. "保持列
CALL METHOD go_alv->refresh( EXPORTING s_stable = is_stable ).
ENDFORM. " FRM_on_user_command
*&---------------------------------------------------------------------*
*& Form FRM_DOWNLOAD_TEMPLATE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_download_template .
DATA: l_fullpath TYPE string,
l_path TYPE string,
l_filename TYPE string,
l_default_name TYPE string,
l_filter TYPE string,
l_destination TYPE rlgrap-filename,
ls_key TYPE wwwdatatab,
l_rc TYPE sy-subrc,
l_objid TYPE wwwdata-objid.
CLEAR: l_fullpath,l_path,l_filename,l_filter,l_destination,ls_key.
l_filter = 'XLS|*.XLS'.
l_default_name = 'xxxx信息导入模板'.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
default_file_name = l_default_name
file_filter = l_filter"cl_gui_frontend_services=>filetype_all
CHANGING
filename = l_filename
path = l_path
fullpath = l_fullpath
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3.
IF l_fullpath IS NOT INITIAL.
IF strlen( l_fullpath ) GT 128.
MESSAGE text-m02 TYPE 'E'.
ENDIF.
l_destination = l_fullpath.
l_objid = sy-repid.
*&**SMW0上传的方式
SELECT relid objid checkout checknew INTO ls_key UP TO 1 ROWS
FROM wwwdata
WHERE relid = 'MI'
AND objid = l_objid
ORDER BY srtf2 DESCENDING.
EXIT.
ENDSELECT.
*&**下载模版
IF ls_key IS NOT INITIAL.
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
key = ls_key
destination = l_destination
IMPORTING
rc = l_rc.
IF l_rc EQ 0.
MESSAGE text-m08 TYPE 'S'.
ELSE.
MESSAGE text-m10 TYPE 'E'.
ENDIF.
ELSE.
MESSAGE text-m09 TYPE 'E'.
ENDIF.
ENDIF.
ENDFORM. " FRM_DOWNLOAD_TEMPLATE
*&---------------------------------------------------------------------*
*& Form FRM_INIT_FUNCKEY
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_init_funckey .
DATA: ls_smp_dyntxt TYPE smp_dyntxt.
ls_smp_dyntxt-text = '下载模板'.
ls_smp_dyntxt-icon_id = icon_export.
ls_smp_dyntxt-icon_text = '下载模板'.
ls_smp_dyntxt-quickinfo = '下载导入模板'.
sscrfields-functxt_01 = ls_smp_dyntxt.
ls_smp_dyntxt-text = '跳转到旧工具'.
ls_smp_dyntxt-icon_id = icon_skip.
ls_smp_dyntxt-icon_text = '跳转到旧工具'.
ls_smp_dyntxt-quickinfo = '跳转到旧工具'.
sscrfields-functxt_02 = ls_smp_dyntxt.
ENDFORM. " FRM_INIT_FUNCKEY
*&---------------------------------------------------------------------*
*& Form FRM_SKIP_OLD_TCODE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_skip_old_tcode .
CALL TRANSACTION 'ZMMT001_OLD'.
ENDFORM. " FRM_SKIP_OLD_TCODE
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATABASE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_get_database .
SELECT matnr werks INTO CORRESPONDING FIELDS OF TABLE gt_out
FROM ztmm_sfzs WHERE matnr IN s_matnr AND werks IN s_matnr.
ENDFORM. " FRM_GET_DATABASE
</code></span></span>