ABAP 实现多表头报表MERGE,如图

结果示例:
ABAP 实现多表头报表MERGE,如图_第1张图片
此例中ALV中实现多表头是通过合并单元格的方式,重写cl_gui_alv_grid中的一些方法来实现,不去构建fieldcat,把所有的数据 连带表头都放进内表中去展示。

参照国外一个兄弟的代码,自己又做了些总结
程序结构
**************** Z_DUOBIAOTOU*******************************
INCLUDE Z_DUOBIAOTOUTOP. "数据定义
INCLUDE Z_DUOBIAOTOUCLS. "类ZCL_GUI_ALV_GRID
INCLUDE Z_DUOBIAOTOUO01. "PBO
INCLUDE Z_DUOBIAOTOUI01. "PAI
INCLUDE Z_DUOBIAOTOUF01. "业务相关逻辑
START-OF-SELECTION.
PERFORM GETDATA ."获取主数据
END-OF-SELECTION.
IF gt_main[] IS NOT INITIAL.
CALL SCREEN 0200.
ELSE.
MESSAGE ‘NO DATA’ TYPE ‘S’ DISPLAY LIKE ‘E’.
ENDIF.
**************** Z_DUOBIAOTOU********************************
code:
DUOBIAOTOUTOP

* *** Allgemeines  ****************************************************
INCLUDE <cl_alv_control>.
INCLUDE <icon>.
DATA retc    TYPE sy-subrc.                  .
DATA ok_code TYPE sy-ucomm.
DATA it_grp  TYPE lvc_t_sgrp.
DATA it_fil  TYPE lvc_t_filt.
DATA wa_fil  TYPE lvc_s_filt.

* **** ALV_GRID    ****************************************************
TYPES: BEGIN OF t_check_styles,
          field01(20),
          field02(20),
          field03(20),
          field04(20),
          field05(20),
          field06(20),
          field07(20),
          field08(20),
          field09(20),
          field10(20),
          field11(20),
          field12(20).
TYPES END OF t_check_styles.

DATA gt_main TYPE  t_check_styles OCCURS 0.
DATA gs_main TYPE t_check_styles.
FIELD-SYMBOLS <fs_styles>        TYPE t_check_styles.
DATA :          lt_fieldcatalog  TYPE lvc_t_fcat.
DATA :          ls_fieldcatalog  TYPE lvc_s_fcat.
DATA :          wa_cat           TYPE lvc_s_fcat.
DATA : fieldname(40).
DATA : fieldnr(2) TYPE n.

FIELD-SYMBOLS  <fs_cat> TYPE lvc_s_fcat.

DATA  lt_iinfo TYPE lvc_t_info.
DATA  wa_iinfo TYPE lvc_s_info.
DATA  lt_idata TYPE lvc_t_data.
DATA  wa_idata TYPE lvc_s_data.

CLASS ZCL_GUI_ALV_GRID DEFINITION DEFERRED .
DATA it_col_merge        TYPE lvc_t_co01.
DATA wa_col_merge        TYPE lvc_s_co01.
DATA: g_container        TYPE scrfname VALUE 'CU_CON'.
DATA: g_custom_container TYPE REF TO cl_gui_custom_container.
DATA  g_alv_grid         TYPE REF TO ZCL_GUI_ALV_GRID.
CLASS cl_gui_cfw DEFINITION LOAD.

DATA: x_save,                     "for Parameter I_SAVE
      gs_variant TYPE disvariant. "for parameter IS_VARIANT
DATA gs_layout TYPE lvc_s_layo.   " Layout
DATA wa_style  TYPE lvc_s_styl.

Z_DUOBIAOTOUCLS

************************************************************************
* Public section of class. *
************************************************************************
CLASS ZCL_GUI_ALV_GRID DEFINITION INHERITING FROM CL_GUI_ALV_GRID  .

*"* public components of class ZCL_GUI_ALV_GRID
*"* do not include other source files here!!
  PUBLIC SECTION.
    METHODS Z_SET_MERGE_HORIZ
      IMPORTING
        ROW           TYPE I
      CHANGING
        TAB_COL_MERGE TYPE LVC_T_CO01 .
    METHODS Z_SET_MERGE_VERT
      IMPORTING
        ROW           TYPE I
      CHANGING
        TAB_COL_MERGE TYPE LVC_T_CO01 .
    METHODS Z_DISPLAY .
    METHODS Z_SET_CELL_STYLE
      IMPORTING
        ROW    TYPE I OPTIONAL
        COL    TYPE I OPTIONAL
        STYLE  TYPE LVC_STYLE
        STYLE2 TYPE LVC_STYLE OPTIONAL .
    METHODS Z_SET_FIXED_COL_ROW
      IMPORTING
        COL TYPE I
        ROW TYPE I .
    METHODS Z_INIT_CELL_STYLES .
ENDCLASS.
CLASS ZCL_GUI_ALV_GRID IMPLEMENTATION.
  METHOD Z_SET_MERGE_HORIZ.
* ROW - Zeile deren Spalten zusammengef hrt werden sollen
* tab_col_merge - Spalten, die zusammengef hrt werden sollen
    FIELD-SYMBOLS <FS_COLS> TYPE LVC_S_CO01.
    FIELD-SYMBOLS <FS_DATA> TYPE LVC_S_DATA.
    DATA OUTPUTLEN TYPE I.

    SORT TAB_COL_MERGE.
* Die Spalten, die zusammengef hrt werden sollen
    LOOP AT TAB_COL_MERGE ASSIGNING <FS_COLS>.
* ein paar Pr fungen
      IF <FS_COLS>-COL_ID    LE 0.                CONTINUE. ENDIF.
      IF <FS_COLS>-OUTPUTLEN LE <FS_COLS>-COL_ID. CONTINUE. ENDIF.
      OUTPUTLEN = <FS_COLS>-OUTPUTLEN - <FS_COLS>-COL_ID.
      LOOP AT MT_DATA ASSIGNING <FS_DATA>
           WHERE ROW_POS = ROW  AND
                 ( COL_POS BETWEEN <FS_COLS>-COL_ID AND
                                   <FS_COLS>-OUTPUTLEN ).
* Setze wie weit soll gemerged werden Von Spalte in L nge
* und zwar wird bei der 1 Spalte angefangen
        IF <FS_DATA>-COL_POS = <FS_COLS>-COL_ID.
          <FS_DATA>-MERGEHORIZ = OUTPUTLEN.
* bei allen anderen, die zusammangeh aaren
* muss der Wert raus, da er aus der 1. Spalte kommt
* und das mergekennzeichen muss auch weg !
        ELSE.
          CLEAR <FS_DATA>-MERGEHORIZ.
          CLEAR <FS_DATA>-VALUE.
        ENDIF.
      ENDLOOP.

    ENDLOOP.

  ENDMETHOD.

  METHOD Z_SET_MERGE_VERT.
* ROW - Zeile deren Spalten zusammengefhrt werden sollen
* tab_col_merge - Spalten, die zusammengefhrt werden sollen
    FIELD-SYMBOLS <FS_COLS> TYPE LVC_S_CO01.
    FIELD-SYMBOLS 

你可能感兴趣的:(ABAP开发回顾总结,程序人生,经验分享,运维,大数据)