*&---------------------------------------------------------------------*
*& Report ZDEMO_UPLOAD
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zdemo_upload.
TABLES:sscrfields.
TYPE-POOLS:slis,isoc.
DATA:ls_layout TYPE slis_layout_alv.
DATA:ct_fieldcat TYPE slis_t_fieldcat_alv.
DATA:gt_fieldcatalog TYPE slis_t_fieldcat_alv .
DATA:ls_ct_fieldcat LIKE LINE OF ct_fieldcat.
DATA:ls_it_fieldcatalog LIKE LINE OF gt_fieldcatalog.
DATA:lv_grid_title TYPE lvc_title ."VALUE '上载数据'.
DATA: lv_begin_col TYPE i VALUE 1,"从第一列开始
lv_begin_row TYPE i VALUE 2,"从第二行开始
lv_end_col TYPE i VALUE 14, "到14列
lv_end_row TYPE i VALUE 65535. "最多65535行
DATA:gt_data TYPE TABLE OF spfli.
DATA:gw_data TYPE spfli.
DATA:lv_objid TYPE wwwdatatab-objid VALUE 'ZLM_TMPLT'. "SWMO 上载模板ID
DATA:lv_alv_structure TYPE dd02l-tabname VALUE 'SPFLI'.
SELECTION-SCREEN:FUNCTION KEY 1.
PARAMETERS :p_file LIKE rlgrap-filename .
INITIALIZATION.
sscrfields-functxt_01 = '下载模板'.
AT SELECTION-SCREEN.
CASE sscrfields-ucomm.
WHEN 'FC01'.
PERFORM frm_down_tmplt USING lv_objid.
WHEN 'ONLI'.
PERFORM frm_upload.
PERFORM frm_show_alv.
WHEN OTHERS.
ENDCASE.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
PERFORM frm_file_open CHANGING p_file.
*&---------------------------------------------------------------------*
*& Form FRM_DOWN_TMPLT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_down_tmplt USING p_objid TYPE wwwdatatab-objid.
DATA: p_file TYPE ibipparms-path.
DATA: lo_objdata LIKE wwwdatatab,
lc_filename TYPE string ,"
lc_fullpath TYPE string VALUE 'C:\',
lc_path TYPE string VALUE 'C:\',
ls_destination LIKE rlgrap-filename,
li_rc LIKE sy-subrc.
* DATA: p_objid TYPE wwwdatatab-objid .
DATA: p_dest LIKE sapb-sappfad.
CONCATENATE p_objid '上载模板.xlsx' INTO lc_filename.
"down
CALL METHOD cl_gui_frontend_services=>file_save_dialog "
EXPORTING
default_extension = 'XLS'
default_file_name = lc_filename
CHANGING
filename = lc_filename
path = lc_path
fullpath = lc_fullpath "full path
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
IF lc_fullpath = ''.
RETURN.
ENDIF.
ls_destination = lc_fullpath.
"check
SELECT SINGLE relid objid
FROM wwwdata INTO CORRESPONDING FIELDS OF lo_objdata
WHERE srtf2 = 0
AND relid = 'MI'
AND objid = p_objid.
IF sy-subrc NE 0 OR lo_objdata-objid EQ space.
MESSAGE text-e12 TYPE 'E'.
ENDIF.
"down
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
key = lo_objdata
destination = ls_destination
IMPORTING
rc = li_rc.
IF li_rc NE 0.
MESSAGE text-e11 TYPE 'E'.
ENDIF.
ENDFORM. " FRM_DOWN_TMPLT
*&---------------------------------------------------------------------*
*& Form FRM_FILE_OPEN
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_P_FILE text
*----------------------------------------------------------------------*
FORM frm_file_open CHANGING p_file.
DATA: lv_file TYPE string.
CLEAR lv_file.
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
mask = ',Excel file,*.xls;*.xlsx;'
title = '选择文件'(100)
IMPORTING
filename = lv_file
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
IF sy-subrc <> 0 AND sy-subrc <> 3.
MESSAGE '选择文件出错!'(007) TYPE 'E'.
ENDIF.
* call function 'F4_FILENAME'
* importing
* file_name = l_path.
p_file = lv_file.
ENDFORM. " FRM_FILE_OPEN
*&---------------------------------------------------------------------*
*& Form FRM_UPLOAD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_upload.
FIELD-SYMBOLS: . "#EC *
DATA: l_i_xlstmp TYPE alsmex_tabline OCCURS 0 WITH HEADER LINE.
DATA: l_col TYPE i.
CLEAR l_i_xlstmp.
REFRESH: l_i_xlstmp,gt_data.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = p_file
i_begin_col = lv_begin_col
i_begin_row = lv_begin_row "
i_end_col = lv_end_col "
i_end_row = lv_end_row
TABLES
intern = l_i_xlstmp
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
IF sy-subrc NE 0 .
MESSAGE s208(00) WITH 'Upload failed'(t04). "#EC *
ENDIF.
CLEAR gt_data[].
SORT l_i_xlstmp BY row col.
LOOP AT l_i_xlstmp.
l_col = l_i_xlstmp-col.
ASSIGN COMPONENT l_col OF STRUCTURE gw_data TO .
= l_i_xlstmp-value.
AT END OF row.
APPEND gw_data TO gt_data.
CLEAR: gw_data.
ENDAT.
ENDLOOP.
CLEAR l_i_xlstmp[].
ENDFORM. " FRM_UPLOAD
*&---------------------------------------------------------------------*
*& Form FRM_SHOW_ALV
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_show_alv .
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = lv_alv_structure
CHANGING
ct_fieldcat = ct_fieldcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
ENDIF.
CLEAR gt_fieldcatalog[].
LOOP AT ct_fieldcat INTO ls_ct_fieldcat .
MOVE-CORRESPONDING ls_ct_fieldcat TO ls_it_fieldcatalog.
ls_it_fieldcatalog-seltext_l = ls_ct_fieldcat-seltext_l.
ls_it_fieldcatalog-seltext_m = ls_ct_fieldcat-seltext_m.
ls_it_fieldcatalog-seltext_s = ls_ct_fieldcat-seltext_s.
CASE ls_it_fieldcatalog-fieldname .
"如果有复选框
WHEN 'CHECKBOX'.
ls_it_fieldcatalog-outputlen = 8.
ls_it_fieldcatalog-ddictxt = '选择'.
ls_it_fieldcatalog-seltext_l = '选择'.
ls_it_fieldcatalog-seltext_m = '选择'.
ls_it_fieldcatalog-seltext_s = '选择'.
ls_it_fieldcatalog-reptext_ddic = '选择'.
ls_it_fieldcatalog-checkbox = 'X'.
ls_it_fieldcatalog-edit = 'X'.
"如果需要调整列描述
WHEN 'ITEM'.
ls_it_fieldcatalog-ddictxt = '行项目'. ls_it_fieldcatalog-seltext_l = '行项目'.
"如果有需要穿透的热键
WHEN 'VBELN' .
ls_it_fieldcatalog-hotspot = 'X'.
WHEN OTHERS.
ENDCASE.
ls_it_fieldcatalog-ddictxt ='L'.
APPEND ls_it_fieldcatalog TO gt_fieldcatalog.
ENDLOOP.
ls_layout-colwidth_optimize = 'X'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_grid_title = lv_grid_title
is_layout = ls_layout
it_fieldcat = gt_fieldcatalog
i_callback_program = sy-repid "
i_callback_pf_status_set = 'ZFORM_SET_PF_STATUS'
i_callback_user_command = 'ALV_USER_COMMAND'
TABLES
t_outtab = gt_data
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. " FRM_SHOW_FC
ENDFORM. " FRM_SHOW_ALV
*&---------------------------------------------------------------------*
*& Form ZFORM_SET_PF_STATUS_FC
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->RT_EXTAB text
*----------------------------------------------------------------------*
FORM zform_set_pf_status USING rt_extab TYPE slis_t_extab.
SET PF-STATUS 'STATUS' .
ENDFORM. "zform_set_PF_STATUS
*&---------------------------------------------------------------------*
*& Form alv_user_command
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->R_UCOMM text
* -->RS_SELFIELD text
*----------------------------------------------------------------------*
FORM alv_user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
DATA: l_grid TYPE REF TO cl_gui_alv_grid.
DATA: lv_value TYPE char20.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = l_grid.
CALL METHOD l_grid->check_changed_data.
CALL METHOD l_grid->refresh_table_display.
rs_selfield-refresh = 'X'.
rs_selfield-col_stable = 'X'.
rs_selfield-row_stable = 'X'.
CASE r_ucomm.
WHEN '&IC1'."热键
lv_value = rs_selfield-value.
CHECK lv_value IS NOT INITIAL .
CASE rs_selfield-fieldname.
WHEN 'VBELN' .
IF lv_value IS NOT INITIAL.
SET PARAMETER ID 'AUN' FIELD lv_value.
CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN .
ENDIF.
WHEN OTHERS.
ENDCASE.
WHEN 'BACK'.
LEAVE TO SCREEN 0.
WHEN 'LEAVE'.
LEAVE PROGRAM.
ENDCASE .
CALL METHOD l_grid->check_changed_data.
CALL METHOD l_grid->refresh_table_display.
ENDFORM . "ALV_USER_COMMAND
标签:checkbox,DEMO,热键,fieldcatalog,lv,ls,file,DATA,TYPE From: https://blog.51cto.com/u_15680210/5751350