abap alv multiple header using write
A standard SAP ALV list report will show only one line header, but there will be a requirement someday for you to create a multiple lines header in your ALV list report and in order to do this, you must first set the no_colhead property to “X” in the ALV Layout, this code is to exclude the standard ALV columns and after that we replace the columns text by using WRITE command at the top of page event.
Okay now let’s create a small ALV report that will display multiple lines header.
1. Execute TCODE SE38, and name the program Zmultipleheader
2. Copy and paste this code below.
*&---------------------------------------------------------------------*
*& Report ZMULTIPLEHEADER
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZMULTIPLEHEADER.
TYPE-POOLS: slis, icon.
DATA: ld_fieldcat TYPE slis_fieldcat_alv.
DATA: t_alv_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE,
Alv_Layout TYPE SLIS_LAYOUT_ALV .
DATA : it_fld TYPE slis_t_fieldcat_alv ,
it_evt TYPE slis_t_event ,
wa_fld TYPE slis_fieldcat_alv ,
wa_evt TYPE slis_alv_event ,
wa_lay TYPE slis_layout_alv .
data:
BEGIN OF itab OCCURS 0,
carrid like sflight-carrid,
connid like sflight-connid,
planetype like sflight-planetype,
seatsmax like sflight-seatsmax,
END OF itab.
START-OF-SELECTION.
"We are using table sflight to populate the internal
"table
SELECT carrid connid planetype seatsmax
FROM sflight
INTO TABLE itab.
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
IMPORTING
et_events = it_evt.
READ TABLE it_evt INTO wa_evt
WITH KEY name = slis_ev_after_line_output .
wa_evt-form = slis_ev_after_line_output .
MODIFY it_evt FROM wa_evt INDEX sy-tabix .
READ TABLE it_evt INTO wa_evt
WITH KEY name = slis_ev_top_of_page .
wa_evt-form = slis_ev_top_of_page .
MODIFY it_evt FROM wa_evt INDEX sy-tabix .
CLEAR: ld_fieldcat.
ld_fieldcat-tabname = 'ITAB'.
ld_fieldcat-fieldname = 'CARRID'.
ld_fieldcat-ref_tabname = 'SFLIGHT'.
ld_fieldcat-outputlen = '10'.
APPEND ld_fieldcat TO t_alv_fieldcat.
CLEAR ld_fieldcat.
CLEAR: ld_fieldcat.
ld_fieldcat-tabname = 'ITAB'.
ld_fieldcat-fieldname = 'CONNID'.
ld_fieldcat-ref_tabname = 'SFLIGHT'.
ld_fieldcat-outputlen = '10'.
APPEND ld_fieldcat TO t_alv_fieldcat.
CLEAR ld_fieldcat.
CLEAR: ld_fieldcat.
ld_fieldcat-tabname = 'ITAB'.
ld_fieldcat-fieldname = 'PLANETYPE'.
ld_fieldcat-ref_tabname = 'SFLIGHT'.
ld_fieldcat-outputlen = '10'.
APPEND ld_fieldcat TO t_alv_fieldcat.
CLEAR ld_fieldcat.
CLEAR: ld_fieldcat.
ld_fieldcat-tabname = 'ITAB'.
ld_fieldcat-fieldname = 'SEATSMAX'.
ld_fieldcat-ref_tabname = 'SFLIGHT'.
ld_fieldcat-outputlen = '10'.
APPEND ld_fieldcat TO t_alv_fieldcat.
CLEAR ld_fieldcat.
"This is where we exclude the standard ALV columns
ALV_LAYOUT-no_colhead = 'X' .
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
IS_LAYOUT = ALV_LAYOUT
i_bypassing_buffer = 'X'
i_callback_program = sy-repid
it_fieldcat = t_alv_fieldcat[]
it_events = it_evt
i_save = 'A'
TABLES
t_outtab = ITAB. "internal table
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 top_of_page .
"Uline for creating a horizontal line
ULINE AT 1(45) .
"Format color for header background
FORMAT COLOR 7 .
"This is where we manually create the header text,
"in this example I'm using 2 lines header, if you
"want to have 3 lines header or more, you can just
"add new write command.
WRITE: / sy-vline , 02 'CARRID AND CONNID',
23 SY-VLINE, 25 'PLANE & SEATS MAX', 45 SY-VLINE.
WRITE: / sy-vline , 02 'CARRID' ,12 sy-vline, 14 'CONNID',
23 SY-VLINE, 25 'PLANE ', 34 SY-VLINE, 36 'SEATS MAX', 45 SY-VLINE.
ENDFORM.
3. Here’s the multiple header result.