ALV报表是SAP中非常重要的报表显示方式,下面的这个例子演示了如何在Report程序中创建一个ALV报表,以透明表spfli为例:
REPORT z_myalv .
TYPE-POOLS:slis."首先引入类型组slis
DATA:fieldcat TYPE slis_t_fieldcat_alv with header line,
layout TYPE slis_layout_alv,layout结构体用于控制alv的显示方式
w_repid LIKE sy-repid."用于存放当前程序名
DATA itab_spfli TYPE STANDARD TABLE OF spfli.
START-OF-SELECTION.
PERFORM getdata."get the data from the database
PERFORM catalog.
PERFORM alvshow.
FORM getdata .
SELECT * FROM spfli INTO CORRESPONDING FIELDS OF TABLE itab_spfli.
ENDFORM. " getdata
FORM catalog .
w_repid = sy-repid.
CLEAR fieldcat.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = w_repid
* I_INTERNAL_TABNAME =
I_STRUCTURE_NAME = 'spfli'
* I_CLIENT_NEVER_DISPLAY = 'X'
* I_INCLNAME =
* I_BYPASSING_BUFFER =
* I_BUFFER_ACTIVE =
changing
ct_fieldcat = 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.
read table fieldcat index 2.
fieldcat-key = 'x'.
modify fieldcat index 2.
read table fieldcat index 4.
fieldcat-key = 'x'.
modify fieldcat index 4.
read table fieldcat index 5.
fieldcat-checkbox = 'x'.
modify fieldcat index 5.
layout-colwidth_optimize = 'x'.
ENDFORM. " catalog
*&---------------------------------------------------------------------*
*& Form alvshow
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM alvshow .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
* I_INTERFACE_CHECK = ' '
* I_BYPASSING_BUFFER = ' '
* I_BUFFER_ACTIVE = ' '
I_CALLBACK_PROGRAM = w_repid
* I_CALLBACK_PF_STATUS_SET = ' '
* I_CALLBACK_USER_COMMAND = ' '
* I_CALLBACK_TOP_OF_PAGE = ' '
* I_CALLBACK_HTML_TOP_OF_PAGE = ' '
* I_CALLBACK_HTML_END_OF_LIST = ' '
* I_STRUCTURE_NAME =
* I_BACKGROUND_ID = ' '
I_GRID_TITLE = 'MY ALV REPORT LIST'
* I_GRID_SETTINGS =
IS_LAYOUT = layout
IT_FIELDCAT = fieldcat[]
* IT_EXCLUDING =
* IT_SPECIAL_GROUPS =
* IT_SORT =
* IT_FILTER =
* IS_SEL_HIDE =
* I_DEFAULT = 'X'
* I_SAVE = ' '
* IS_VARIANT =
* IT_EVENTS =
* IT_EVENT_EXIT =
* IS_PRINT =
* IS_REPREP_ID =
* I_SCREEN_START_COLUMN = 0
* I_SCREEN_START_LINE = 0
* I_SCREEN_END_COLUMN = 0
* I_SCREEN_END_LINE = 0
* IT_ALV_GRAPHICS =
* IT_HYPERLINK =
* IT_ADD_FIELDCAT =
* IT_EXCEPT_QINFO =
* I_HTML_HEIGHT_TOP =
* I_HTML_HEIGHT_END =
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER =
* ES_EXIT_CAUSED_BY_USER =
TABLES
t_outtab = itab_spfli
* 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.
ENDFORM. " alvshow