SAP ABAP FUNCTION ALV报表模板

实现ALV报表展示 快速实现的方法。

实现方案:
1:SE11创建报表需要展示的结构字段。
SAP ABAP FUNCTION ALV报表模板_第1张图片

2.SE38创建一个 INCLUDE 程序此代码可直接copy使用。(后续报表只需要将此INCLUDE嵌入程序即可)

*&---------------------------------------------------------------------*
*& 包含               ZALV
*&---------------------------------------------------------------------*

  TYPE-POOLS:slis.
  DATA:gt_fieldcat TYPE lvc_t_fcat,
       gs_fieldcat LIKE LINE OF gt_fieldcat,
       is_layout   TYPE lvc_s_layo,
       is_variant  TYPE disvariant,
       formname    TYPE slis_formname,
       lt_events   TYPE slis_t_event , "SLIS_T_EVENT
       ls_events   TYPE slis_alv_event.

  DATA: lr_grid    TYPE REF TO cl_gui_alv_grid,
        is_stable  TYPE lvc_s_stbl,
        lc_i       TYPE i,
        es_variant TYPE disvariant,
        e_save     TYPE char1,
        gt_ddval   TYPE lvc_t_drop WITH HEADER LINE,
        lv_gui     TYPE string.

*----------------------------------------------------------------------*
*       CLASS LCL_EVENT_HANDLER DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
  CLASS lcl_event_handler DEFINITION."类定义
    PUBLIC SECTION.
      METHODS:
        handle_data_changed FOR EVENT data_changed_finished OF cl_gui_alv_grid
          IMPORTING e_modified et_good_cells.
  ENDCLASS. "lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
*       CLASS lcl_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
*       类实现
*----------------------------------------------------------------------*

  CLASS lcl_event_handler IMPLEMENTATION."类实现
    METHOD handle_data_changed.
      PERFORM handle_data_changed IN PROGRAM (sy-cprog) IF FOUND ."具体处理数据事件
      IF e_modified = 'X'.
        is_stable-row = 'X'." 基于行的稳定刷新
        is_stable-col = 'X'." 基于列稳定刷新
        CALL METHOD lr_grid->refresh_table_display
          EXPORTING
            is_stable = is_stable.
      ENDIF.
    ENDMETHOD.                    "HANDLE_DATA_CHANGED
  ENDCLASS. "lcl_event_handler IMPLEMENTATION

  DATA: gr_event_handler TYPE REF TO lcl_event_handler.


*&---------------------------------------------------------------------*
*&      Form  show_alv
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->TABLE                  内表
*      -->L_HANDLE               变式名
*      -->I_STRUCTURE_NAME       结构名
*----------------------------------------------------------------------*
  FORM show_alv TABLES table
                 USING l_handle
                       i_structure_name
                       .
*ALV表格显示样式
    PERFORM alv_list_layout.
*ALV报表的字段设置
    PERFORM pf_alv_list_fcat USING i_structure_name
                                  gt_fieldcat.
*ALV报表显示
    PERFORM pf_alv_list_disply TABLES table
                               USING  l_handle
                                      ''
                                      ''
                                      ''
                                      ''
                                      ''
                                      ''
                                      .


  ENDFORM.                    " SHOW_ALV
*&---------------------------------------------------------------------*
*&      Form  show_alv2
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->TABLE                  内表
*      -->L_HANDLE               变式名
*      -->I_STRUCTURE_NAME       结构名
*      -->I_SCREEN_START_COLUMN  text
*      -->I_SCREEN_START_LINE    text
*      -->I_SCREEN_END_COLUMN    text
*      -->I_SCREEN_END_LINE      text
*      -->I_HTML_TOP_OF_PAGE     标题html名
*      -->I_HTML_HEIGHT_TOP      标题htlm的高
*----------------------------------------------------------------------*
  FORM show_alv2 TABLES table
                  USING l_handle
                        i_structure_name

                        i_screen_start_column
                        i_screen_start_line
                        i_screen_end_column
                        i_screen_end_line

                        i_html_top_of_page
                        i_html_height_top
                        .
*ALV表格显示样式
    PERFORM alv_list_layout.
*ALV报表的字段设置
    PERFORM pf_alv_list_fcat USING i_structure_name
                                  gt_fieldcat.
*ALV报表显示
    PERFORM pf_alv_list_disply TABLES table
                               USING  l_handle
                                      i_html_top_of_page
                                      i_screen_start_column
                                      i_screen_start_line
                                      i_screen_end_column
                                      i_screen_end_line
                                      i_html_height_top
                                      .


  ENDFORM.                    " SHOW_ALV
*&---------------------------------------------------------------------*
*&      Form  ALV_LIST_LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
  FORM alv_list_layout .
    is_layout-zebra = 'X'. " 斑马线
    is_layout-cwidth_opt  = 'X'.   " 自动调节字段长度
    is_layout-box_fname = 'SEL'.

    PERFORM frm_layout_usexit IN PROGRAM (sy-cprog) IF FOUND CHANGING is_layout.
  ENDFORM.                    " ALV_LIST_LAYOUT
*&---------------------------------------------------------------------*
*&      Form  PF_ALV_LIST_FCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_IT_FIELDCAT  text
*----------------------------------------------------------------------*
  FORM pf_alv_list_fcat  USING i_structure_name    p_it_fieldcat TYPE lvc_t_fcat.

    FREE p_it_fieldcat.
    DATA: p_st_fieldcat TYPE lvc_s_fcat.
    CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
      EXPORTING
        i_buffer_active        = ''
        i_structure_name       = i_structure_name
        i_client_never_display = 'X'
        i_bypassing_buffer     = 'X'
        i_internal_tabname     = 'X'
      CHANGING
        ct_fieldcat            = p_it_fieldcat
      EXCEPTIONS
        inconsistent_interface = 1
        program_error          = 2
        OTHERS                 = 3.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.

    DELETE p_it_fieldcat WHERE fieldname(6) = 'FIELD0'.
    DELETE p_it_fieldcat WHERE fieldname    = 'CELLCOLOR'.
*    DELETE p_it_fieldcat WHERE fieldname    = 'SEL'.
*    p_st_fieldcat-checkbox = 'X'.
*    MODIFY p_it_fieldcat FROM p_st_fieldcat TRANSPORTING checkbox WHERE fieldname = 'ZTLBS' .
*    p_st_fieldcat-edit = 'X'.
*    MODIFY p_it_fieldcat FROM p_st_fieldcat TRANSPORTING edit WHERE  fieldname = 'STEUS'.
    PERFORM frm_fieldcat_usexit IN PROGRAM (sy-cprog) IF FOUND CHANGING p_it_fieldcat.


  ENDFORM.                    " PF_ALV_LIST_FCAT
*&---------------------------------------------------------------------*
*&      Form  PF_ALV_LIST_DISPLY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_TABLE  text
*----------------------------------------------------------------------*
  FORM pf_alv_list_disply  TABLES  table USING
                                          l_handle
                                          i_html_top_of_page
                                          i_screen_start_column
                                          i_screen_start_line
                                          i_screen_end_column
                                          i_screen_end_line
                                          i_html_height_top
                                          .
    is_variant-report = sy-repid.
    is_variant-handle = l_handle.
    formname = i_html_top_of_page .

*--------------------------------------------------------------------*
*去掉中间对象

    FIELD-SYMBOLS: TYPE lvc_s_fcat.
    LOOP AT gt_fieldcat ASSIGNING .
      -scrtext_m = ''.
      -scrtext_s = ''.
*      -ref_field = ''.
*      -ref_table = ''.

    ENDLOOP.

    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
      EXPORTING
        i_callback_program          = sy-repid
        i_callback_pf_status_set    = 'FRM_PF_STATUS_SET'
        i_callback_user_command     = 'FRM_USER_COMMAND'
        i_callback_html_top_of_page = formname
        is_layout_lvc               = is_layout
        it_fieldcat_lvc             = gt_fieldcat
        i_save                      = 'A'
        is_variant                  = is_variant
        i_screen_start_column       = i_screen_start_column
        i_screen_start_line         = i_screen_start_line
        i_screen_end_column         = i_screen_end_column
        i_screen_end_line           = i_screen_end_line
        i_html_height_top           = i_html_height_top   "html高度
        it_events                   = lt_events   "
      TABLES
        t_outtab                    = table
      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.                    " PF_ALV_LIST_DISPLY
*&---------------------------------------------------------------------*
*&      Form  frm_pf_status_set
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->RT_EXTAB   text
*----------------------------------------------------------------------*
  FORM frm_pf_status_set USING rt_extab TYPE slis_t_extab.
    PERFORM frm_set_gui IN PROGRAM (sy-cprog) IF FOUND USING lv_gui  .
    PERFORM frm_set_gui2 IN PROGRAM (sy-cprog) IF FOUND USING lv_gui rt_extab .
    IF lv_gui IS INITIAL .
      lv_gui = 'GUI01' .
    ENDIF.
    SET PF-STATUS lv_gui EXCLUDING rt_extab .

  ENDFORM.                    "frm_pf_status_set

*&---------------------------------------------------------------------*
*&      Form  FEM_USER_COMMAND
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->R_UCOMM      text
*      -->RS_SELFIELD  text
*----------------------------------------------------------------------*
  FORM frm_user_command USING r_ucomm TYPE sy-ucomm
                          rs_selfield TYPE slis_selfield.

    CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
      IMPORTING
        e_grid = lr_grid.


    CALL METHOD lr_grid->check_changed_data.

    CALL METHOD lr_grid->get_frontend_layout
      IMPORTING
        es_layout = is_layout.

    is_layout-cwidth_opt = 'X'.

    CALL METHOD lr_grid->set_frontend_layout
      EXPORTING
        is_layout = is_layout.
    rs_selfield-refresh = 'X'.


    is_stable-col = 'X'.
    is_stable-row = 'X' .
    CALL METHOD lr_grid->check_changed_data.


    CALL METHOD lr_grid->get_variant
      IMPORTING
        es_variant = es_variant
        e_save     = e_save.

    CALL METHOD lr_grid->register_edit_event
      EXPORTING
        i_event_id = cl_gui_alv_grid=>mc_evt_enter
      EXCEPTIONS
        error      = 1
        OTHERS     = 2.

    PERFORM user_exit_comm IN PROGRAM (sy-cprog) IF FOUND  USING es_variant-handle r_ucomm rs_selfield.

*--------------------------------------------------------------------*
*刷新
    is_stable-col = 'X'.
    is_stable-row = 'X'.
    CALL METHOD lr_grid->refresh_table_display
      EXPORTING
        is_stable = is_stable.
  ENDFORM.                    "FEM_USER_COMMAND
*&---------------------------------------------------------------------*
*&      Form  html_top_of_page
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->DOCUMENT   text
*----------------------------------------------------------------------*
  FORM html_top_of_page USING document TYPE REF TO cl_dd_document.
    DATA: p_html   TYPE string,
          position TYPE i,
          text     TYPE sdydo_text_element..

    PERFORM frm_set_html IN PROGRAM (sy-cprog) IF FOUND USING p_html .

    "接受有长度限制
    APPEND p_html TO document->html_table.

  ENDFORM .                    "f_top_of_page

3.直接调用展示报表(注意:1.报表GUI需要进行重写 2.报表展示内表的结构必须存在SEL字段 )

报表GUI需要进行重写
FORM frm_set_gui  USING   lv_gui.
  lv_gui = 'GUI100'.
ENDFORM.

报表展示内表的结构必须存在SEL字段
DATA: BEGIN OF gt_alv OCCURS 0.
        INCLUDE STRUCTURE zssdr004_alv.
DATA:   sel TYPE flag,
      END OF gt_alv.

调用展示报表
”PERFORM show_alv TABLES 内表 USING '变式名称' '字母大写结构'.
 PERFORM show_alv TABLES gt_alv USING 'SH01' 'ZSSDR004_ALV'.

你可能感兴趣的:(SAP,ABAP,linux,java,mysql)