编程语言
首页 > 编程语言> > ABAP-层次报表的实现程序例子

ABAP-层次报表的实现程序例子

作者:互联网

  1 *&---------------------------------------------------------------------*
  2 *& Report Y4_B4_TEST20
  3 *&---------------------------------------------------------------------*
  4 *&
  5 *&---------------------------------------------------------------------*
  6 REPORT y4_b4_test20.
  7 TABLES: ekko,ekpo.
  8 SELECT-OPTIONS s_ebeln FOR ekko-ebeln.
  9 
 10 *1、在程序里包含SLIS
 11 TYPE-POOLS:slis.
 12 
 13 *2、声明主表和明细表
 14 DATA: BEGIN OF headertab OCCURS 0,
 15         ebeln LIKE ekko-ebeln,
 16         bstyp LIKE ekko-bstyp,
 17         bsart LIKE ekko-bsart,
 18         statu LIKE ekko-statu,
 19       END OF headertab.
 20 
 21 DATA: BEGIN OF itemtab OCCURS 0,
 22         ebeln LIKE ekpo-ebeln,
 23         ebelp LIKE ekpo-ebelp,
 24         matnr LIKE ekpo-matnr,
 25         werks LIKE ekpo-werks,
 26         menge LIKE ekpo-menge,
 27         netpr LIKE ekpo-netpr,
 28         peinh LIKE ekpo-peinh,
 29         netwr LIKE ekpo-netwr,
 30       END OF itemtab.
 31 
 32 DATA: i_fieldcat TYPE slis_t_fieldcat_alv.
 33 DATA: v_repid LIKE sy-repid."当前程序名
 34 
 35 *3、申明主表和明细表的名称变量
 36 DATA: g_tabname_header TYPE slis_tabname,
 37       g_tabname_item   TYPE slis_tabname.
 38 
 39 *4、声明主表和明细表连接关键字变量
 40 DATA: gs_keyinfo TYPE slis_keyinfo_alv.
 41 
 42 INITIALIZATION.
 43   v_repid = sy-repid.
 44 
 45 START-OF-SELECTION.
 46 
 47   PERFORM get_data.
 48 
 49 END-OF-SELECTION.
 50 
 51 *5、获取头表输出表头字段名称
 52   CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
 53     EXPORTING
 54       i_program_name         = v_repid "当前程序名
 55       i_internal_tabname     = 'HEADERTAB' "主表名
 56       i_inclname             = v_repid "当前程序名
 57       i_bypassing_buffer     = 'X'
 58       i_buffer_active        = ''
 59     CHANGING
 60       ct_fieldcat            = i_fieldcat "ALV输出的主表标题
 61     EXCEPTIONS
 62       inconsistent_interface = 1
 63       program_error          = 2
 64       OTHERS                 = 3.
 65   IF sy-subrc <> 0.
 66     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1
 67     sy-msgv2 sy-msgv3 sy-msgv4.
 68   ENDIF.
 69 
 70 *6、获取从表输出表头字段名称
 71   CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
 72     EXPORTING
 73       i_program_name         = sy-repid
 74       i_internal_tabname     = 'ITEMTAB'
 75       i_inclname             = v_repid
 76       i_bypassing_buffer     = 'X'
 77       i_buffer_active        = ''
 78     CHANGING
 79       ct_fieldcat            = i_fieldcat
 80     EXCEPTIONS
 81       inconsistent_interface = 1
 82       program_error          = 2
 83       OTHERS                 = 3.
 84   IF sy-subrc <> 0.
 85     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1
 86     sy-msgv2 sy-msgv3 sy-msgv4.
 87   ENDIF.
 88 
 89 *7、设置主从表的关联字段,最多可以设置五个字段关联。
 90   gs_keyinfo-header01 = 'EBELN'.
 91   gs_keyinfo-item01 = 'EBELN'.
 92 
 93 *8、设置主表和明细表变量对应的内表名称
 94   g_tabname_header = 'HEADERTAB'."主表
 95   g_tabname_item = 'ITEMTAB'."明细表
 96 
 97 *9、调用层次ALV输出函数
 98   CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
 99     EXPORTING
100       i_callback_program = v_repid
101       it_fieldcat        = i_fieldcat
102       i_save             = 'A'
103       i_tabname_header   = g_tabname_header
104       i_tabname_item     = g_tabname_item
105       is_keyinfo         = gs_keyinfo
106       i_bypassing_buffer = 'X'
107       i_buffer_active    = ' '
108     TABLES
109       t_outtab_header    = headertab
110       t_outtab_item      = itemtab
111     EXCEPTIONS
112       program_error      = 1
113       OTHERS             = 2.
114   IF sy-subrc <> 0.
115     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
116     WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
117   ENDIF.
118 
119 FORM get_data.
120 
121   SELECT ebeln bstyp bsart statu
122   INTO TABLE headertab
123   FROM ekko
124   WHERE ebeln IN s_ebeln.
125 
126   IF NOT headertab[] IS INITIAL.
127     SELECT ebeln ebelp matnr werks menge netpr peinh netwr
128     INTO TABLE itemtab
129     FROM ekpo
130     FOR ALL ENTRIES IN headertab
131     WHERE ebeln = headertab-ebeln.
132   ENDIF.
133 
134 ENDFORM.

标签:sy,报表,层次,ABAP,tabname,ebeln,repid,ekpo,LIKE
来源: https://www.cnblogs.com/TigerMan/p/16401243.html