matinal:SAP ABAP ALV 层次报表的实现程序例子

2023-10-14 15:10:10 浏览数 (1)

代码语言:javascript复制
*&---------------------------------------------------------------------*
*& Report Y4_B4_TEST20
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT y4_b4_test20.
TABLES: ekko,ekpo.
SELECT-OPTIONS s_ebeln FOR ekko-ebeln.

*1、在程序里包含SLIS
TYPE-POOLS:slis.

*2、声明主表和明细表
DATA: BEGIN OF headertab OCCURS 0,
        ebeln LIKE ekko-ebeln,
        bstyp LIKE ekko-bstyp,
        bsart LIKE ekko-bsart,
        statu LIKE ekko-statu,
      END OF headertab.

DATA: BEGIN OF itemtab OCCURS 0,
        ebeln LIKE ekpo-ebeln,
        ebelp LIKE ekpo-ebelp,
        matnr LIKE ekpo-matnr,
        werks LIKE ekpo-werks,
        menge LIKE ekpo-menge,
        netpr LIKE ekpo-netpr,
        peinh LIKE ekpo-peinh,
        netwr LIKE ekpo-netwr,
      END OF itemtab.

DATA: i_fieldcat TYPE slis_t_fieldcat_alv.
DATA: v_repid LIKE sy-repid."当前程序名

*3、申明主表和明细表的名称变量
DATA: g_tabname_header TYPE slis_tabname,
      g_tabname_item   TYPE slis_tabname.

*4、声明主表和明细表连接关键字变量
DATA: gs_keyinfo TYPE slis_keyinfo_alv.

INITIALIZATION.
  v_repid = sy-repid.

START-OF-SELECTION.

  PERFORM get_data.

END-OF-SELECTION.

*5、获取头表输出表头字段名称
  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
    EXPORTING
      i_program_name         = v_repid "当前程序名
      i_internal_tabname     = 'HEADERTAB' "主表名
      i_inclname             = v_repid "当前程序名
      i_bypassing_buffer     = 'X'
      i_buffer_active        = ''
    CHANGING
      ct_fieldcat            = i_fieldcat "ALV输出的主表标题
    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.

*6、获取从表输出表头字段名称
  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
    EXPORTING
      i_program_name         = sy-repid
      i_internal_tabname     = 'ITEMTAB'
      i_inclname             = v_repid
      i_bypassing_buffer     = 'X'
      i_buffer_active        = ''
    CHANGING
      ct_fieldcat            = i_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.

*7、设置主从表的关联字段,最多可以设置五个字段关联。
  gs_keyinfo-header01 = 'EBELN'.
  gs_keyinfo-item01 = 'EBELN'.

*8、设置主表和明细表变量对应的内表名称
  g_tabname_header = 'HEADERTAB'."主表
  g_tabname_item = 'ITEMTAB'."明细表

*9、调用层次ALV输出函数
  CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
    EXPORTING
      i_callback_program = v_repid
      it_fieldcat        = i_fieldcat
      i_save             = 'A'
      i_tabname_header   = g_tabname_header
      i_tabname_item     = g_tabname_item
      is_keyinfo         = gs_keyinfo
      i_bypassing_buffer = 'X'
      i_buffer_active    = ' '
    TABLES
      t_outtab_header    = headertab
      t_outtab_item      = itemtab
    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.

FORM get_data.

  SELECT ebeln bstyp bsart statu
  INTO TABLE headertab
  FROM ekko
  WHERE ebeln IN s_ebeln.

  IF NOT headertab[] IS INITIAL.
    SELECT ebeln ebelp matnr werks menge netpr peinh netwr
    INTO TABLE itemtab
    FROM ekpo
    FOR ALL ENTRIES IN headertab
    WHERE ebeln = headertab-ebeln.
  ENDIF.

ENDFORM.

0 人点赞