一个国外小哥扩展了CL_GUI_ALV_GRID类,实现了一个表头对应多列的效果。这里给大家看一下大致的效果和代码结构。详细情况请看链接里的内容。

https://answers.sap.com/questions/193699/merge-column-cells-alv-using-cl-gui-alv-grid.html?tdsourcetag=s_pcqq_aiomsg

" span over 2 rows (outputlen must be the number of rows)

wa_col_merge-col_id = 1.

wa_col_merge-outputlen = 2.

APPEND wa_col_merge TO it_col_merge.

CALL METHOD g_alv_grid->z_set_merge_vert EXPORTING row = 1

CHANGING tab_col_merge = it_col_merge.

 

" span over 2 columns (outputlen must be the number of the last column to merge)

wa_col_merge-col_id = 1.

wa_col_merge-outputlen = 2.

APPEND wa_col_merge TO it_col_merge.

CALL METHOD g_alv_grid->z_set_merge_vert EXPORTING row = 1

CHANGING tab_col_merge = it_col_merge.

 

http://www.tricktresor.de/blog/zellen-verbinden/

 

 

 

Code listing   for class: ZCL_GUI_ALV_GRID

Description:   Vererbte CL_GUI_ALV_GRID

         
   

**************************************************************************
    * Class attributes. *
    **************************************************************************

   

Instantiation: Public
    Message class: 
    State: Implemented
    Final Indicator: 
    R/3 Release: 700
   
   

   

**************************************************************************
    * Public section of class. *
    **************************************************************************

   

class ZCL_GUI_ALV_GRID definition
      public
      inheriting from CL_GUI_ALV_GRID
      create public .
   
   

   

*"* public components of class ZCL_GUI_ALV_GRID
    *"* do not include other source files here!!

   

public section.
   
      methods <a href="public_methods     z_set_merge_horiz.html"="">Z_SET_MERGE_HORIZ
        importing
          ROW type I
        changing
          TAB_COL_MERGE type LVC_T_CO01 .
      methods <a href="public_methods     z_set_merge_vert.html"="">Z_SET_MERGE_VERT
        importing
          ROW type I
        changing
          TAB_COL_MERGE type LVC_T_CO01 .
      methods <a href="public_methods     z_display ..html"="">Z_DISPLAY .
      methods <a href="public_methods     z_set_cell_style.html"="">Z_SET_CELL_STYLE
        importing
          ROW type I optional
          COL type I optional
          STYLE type LVC_STYLE
          STYLE2 type LVC_STYLE optional .
      methods <a href="public_methods     z_set_fixed_col_row.html"="">Z_SET_FIXED_COL_ROW
        importing
          COL type I
          ROW type I .
      methods <a href="public_methods     z_init_cell_styles ..html"="">Z_INIT_CELL_STYLES .
   
    </a href="public_methods></a href="public_methods></a href="public_methods></a href="public_methods></a href="public_methods></a href="public_methods>

   

**************************************************************************
    * Private section of class. *
    **************************************************************************
    *"* private components of class ZCL_GUI_ALV_GRID
    *"* do not include other source files here!!

   

private section.
   
   

   

**************************************************************************
    * Protected section of class. *
    **************************************************************************
    *"* protected components of class ZCL_GUI_ALV_GRID
    *"* do not include other source files here!!

   

protected section.
   
   

   

**************************************************************************
    * Types section of class. *
    **************************************************************************
    *"* dummy include to reduce generation dependencies between
    *"* class ZCL_GUI_ALV_GRID and it's users.
    *"* touched if any type reference has been changed

   

Extracted by   Mass Download version 1.5.2 - E.G.Mellodew. 1998-2014. Sap Release 740

 

Code listing   for: Z_SET_MERGE_HORIZ

Description:   Merge cells

         
   

**************************************************************************
    *   Method attributes.                                                   *
    **************************************************************************

   

Instantiation: Public

   

**************************************************************************
   
   

   

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�ren
    * 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.

   

Extracted by   Mass Download version 1.5.2 - E.G.Mellodew. 1998-2014. Sap Release 740

 

Code listing   for: Z_SET_MERGE_VERT

Description:   Merge cells

         
   

**************************************************************************
    *   Method attributes.                                                   *
    **************************************************************************

   

Instantiation: Public

   

**************************************************************************
   
   

   

method Z_SET_MERGE_VERT.
   
   

   

* 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>-mergevert = outputlen.

   

* bei allen anderen, die zusammangeh�ren
    * muss der Wert raus, da er aus der 1. Spalte kommt
    * und das mergekennzeichen muss auch weg !

   

       ELSE.
             CLEAR <fs_data>-mergevert.
             CLEAR <fs_data>-value.
           ENDIF.
         ENDLOOP.
   
       ENDLOOP.
   
    endmethod.

   

Extracted by   Mass Download version 1.5.2 - E.G.Mellodew. 1998-2014. Sap Release 740

 

Code listing   for: Z_DISPLAY

Description:   Display

         
   

**************************************************************************
    *   Method attributes.                                                   *
    **************************************************************************

   

Instantiation: Public

   

**************************************************************************
   
   

   

METHOD z_display.
   
      DATA lv_stable TYPE lvc_s_stbl.
      DATA lv_soft   TYPE c.
   
   

   

**** Prepare refresh
    *  lv_stable-row = 'X'.
    *  lv_stable-col = 'X'.
    *  lv_soft       = 'X'.
    *
    **** Refresh table because Z_SET_CELL_STYLE adds style-values
    **** Refresh initializes mt_data
    *  CALL METHOD refresh_table_display
    *    EXPORTING
    *      is_stable      = lv_stable
    *      i_soft_refresh = lv_soft
    *    EXCEPTIONS
    *      OTHERS         = 1.
   
    * Jetzt noch  �bertragen der ge�nderten Daten

   

  CALL METHOD me->set_data_table
        CHANGING
          data_table = mt_data[].
   
      CALL METHOD set_auto_redraw
        EXPORTING
          enable = 1.
   
    ENDMETHOD.

   

Extracted by   Mass Download version 1.5.2 - E.G.Mellodew. 1998-2014. Sap Release 740

 

Code listing   for: Z_SET_CELL_STYLE

Description:   Cell Style

         
   

**************************************************************************
    *   Method attributes.                                                   *
    **************************************************************************

   

Instantiation: Public

   

**************************************************************************
   
   

   

METHOD z_set_cell_style.
   
      FIELD-SYMBOLS <fs_data> TYPE lvc_s_data.
      IF row IS INITIAL.
        IF col IS INITIAL.

   

* Beides leer -> nichts zu tun.

   

      EXIT.
        ELSE.

   

* Nur Spalte setze komplette Spalte

   

      LOOP AT mt_data ASSIGNING <fs_data>
                WHERE col_pos = col.
            <fs_data>-style  = <fs_data>-style + style.
            <fs_data>-style2 = <fs_data>-style2 + style2.
          ENDLOOP.
        ENDIF.
      ELSE.
        IF col IS INITIAL.

   

* Nur Zeile eingegeben -> komplette Zeile setzen

   

      LOOP AT mt_data ASSIGNING <fs_data>
                WHERE row_pos = row.
            <fs_data>-style  = <fs_data>-style + style.
            <fs_data>-style2 = <fs_data>-style2 + style2.
          ENDLOOP.
        ELSE.
          READ TABLE mt_data ASSIGNING <fs_data>
              WITH KEY row_pos = row
                       col_pos = col.
          IF sy-subrc EQ 0.
            <fs_data>-style  = <fs_data>-style + style.
            <fs_data>-style2 = <fs_data>-style2 + style2.
          ELSE.
            EXIT.
          ENDIF.
        ENDIF.
      ENDIF.
   
    ENDMETHOD.

   

Extracted by   Mass Download version 1.5.2 - E.G.Mellodew. 1998-2014. Sap Release 740

 

Code listing   for: Z_SET_FIXED_COL_ROW

Description:   set fixed

         
   

**************************************************************************
    *   Method attributes.                                                   *
    **************************************************************************

   

Instantiation: Public

   

**************************************************************************

   

method Z_SET_FIXED_COL_ROW.
   
      me->set_fixed_cols( col ).
      me->set_fixed_rows( row ).
   
    endmethod.

   

Extracted by   Mass Download version 1.5.2 - E.G.Mellodew. 1998-2014. Sap Release 740

 

 

Code listing   for: Z_INIT_CELL_STYLES

Description:   Init cell styles

         
   

**************************************************************************
    *   Method attributes.                                                   *
    **************************************************************************

   

Instantiation: Public

   

**************************************************************************

   

METHOD z_init_cell_styles.
   
      FIELD-SYMBOLS <fs_data> TYPE lvc_s_data.

   

* Nur Spalte setze komplette Spalte

   

  LOOP AT mt_data ASSIGNING <fs_data>.
        <fs_data>-style = 0.
      ENDLOOP.
   
    ENDMETHOD.

   

Extracted by   Mass Download version 1.5.2 - E.G.Mellodew. 1998-2014. Sap Release 740