ALV响应自定义function code

我们经常会遇到一些用户, 他们会要求我们把ALV的Export to Local功能改成直接download excel. 下面记录一下怎么实现这个需求。(之前做过一次了, 但有个细节还是忘了...)


1. Copy标准的ALV GUI Status到目标程序

ALV响应自定义function code_第1张图片


ALV响应自定义function code_第2张图片


2. 修改Function Code

ALV响应自定义function code_第3张图片


3. 实现代码

TYPE-POOLS: slis.

TYPES: BEGIN OF ty_data,
  fld1(10),
  fld2(10),
  END OF ty_data.

DATA: gt_data TYPE STANDARD TABLE OF ty_data WITH HEADER LINE.
DATA: gt_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE.

CLEAR gt_data.
gt_data-fld1 = 'Zero'.
gt_data-fld2 = 'Palm'.
APPEND gt_data.

CLEAR gt_fieldcat.
gt_fieldcat-fieldname = 'FLD1'.
gt_fieldcat-seltext_l = 'Field 1'.
APPEND gt_fieldcat.

CLEAR gt_fieldcat.
gt_fieldcat-fieldname = 'FLD2'.
gt_fieldcat-seltext_l = 'Field 2'.
APPEND gt_fieldcat.


CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
  EXPORTING
    i_callback_program                = sy-repid
    i_callback_pf_status_set          = 'FRM_SET_PF_STATUS'
    i_callback_user_command           = 'FRM_USER_COMMAND'
*      is_layout                         = g_layout
    it_fieldcat                       = gt_fieldcat[]
  TABLES
    t_outtab                          = gt_data
 EXCEPTIONS
   program_error                     = 1
   OTHERS                            = 2 .


*&---------------------------------------------------------------------*
*&      Form  frm_user_command
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->R_UCOMM      text
*      -->RS_SELFIELD  text
*----------------------------------------------------------------------*
FORM frm_user_command USING r_ucomm LIKE sy-ucomm
                  rs_selfield TYPE slis_selfield.
  IF r_ucomm = 'ZDOWN'.
    PERFORM frm_down_excel.
  ENDIF.
ENDFORM.                    "frm_user_command

*&---------------------------------------------------------------------*
*&      Form  frm_set_pf_status
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->RT_EXTAB   text
*----------------------------------------------------------------------*
FORM frm_set_pf_status USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS'ZTEST'.
ENDFORM.                    "frm_set_pf_status

*&---------------------------------------------------------------------*
*&      Form  frm_down_excel
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM frm_down_excel.

  DATA: lt_excel TYPE STANDARD TABLE OF ty_data WITH HEADER LINE.

  DATA: lv_path TYPE string VALUE 'C:\TEMP\TEST.XLS'.

  CLEAR lt_excel.
  lt_excel-fld1 = 'Field1'.
  lt_excel-fld2 = 'Field2'.
  APPEND lt_excel.

  APPEND LINES OF gt_data TO lt_excel.

  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      filename              = lv_path
      write_field_separator = 'X'
      dat_mode              = 'X'
      filetype              = 'DAT'
      codepage              = '8400'
      wk1_n_format          = '0'
      wk1_t_format          = '0'
    TABLES
      data_tab              = lt_excel.

ENDFORM.                    "frm_down_excel

P.S.在ALV function里, 这两个subroutine的调用位置

Set status:

ALV响应自定义function code_第4张图片




User Command:

ALV响应自定义function code_第5张图片




你可能感兴趣的:(code,function,自定义,响应,alv)