使用了动态内表的方法,参考了网上的案例,具体如下
*&---------------------------------------------------------------------*
*& REPORT ZMM002
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZMM002.
TABLES:T001.
DATA: BEGIN OF WA_TAB,
BUKRS(10),
NETPR LIKE EKPO-NETPR,
ODATE(8),
END OF WA_TAB.
DATA:ITAB LIKE WA_TAB OCCURS 0 WITH HEADER LINE.
DATA: GT_FIELDCAT TYPE LVC_T_FCAT,
GS_FIELDCAT TYPE LVC_S_FCAT,
GS_LAYOUT TYPE LVC_S_LAYO.
DEFINE : ADD_FIELD.
GS_FIELDCAT-FIELDNAME = &1.
GS_FIELDCAT-COLTEXT = &2.
APPEND GS_FIELDCAT TO GT_FIELDCAT.
END-OF-DEFINITION.
INITIALIZATION.
WA_TAB-BUKRS = 'TX'.
WA_TAB-NETPR = 1213.
WA_TAB-ODATE = 20170501.
APPEND WA_TAB TO ITAB.
WA_TAB-BUKRS = 'TX'.
WA_TAB-NETPR = 121.
WA_TAB-ODATE = 20170503.
APPEND WA_TAB TO ITAB.
WA_TAB-BUKRS = 'TX'.
WA_TAB-NETPR = 112.
WA_TAB-ODATE = 20170505.
APPEND WA_TAB TO ITAB.
WA_TAB-BUKRS = 'AX'.
WA_TAB-NETPR = 12154.
WA_TAB-ODATE = 20170501.
APPEND WA_TAB TO ITAB.
WA_TAB-BUKRS = 'AX'.
WA_TAB-NETPR = 1223.
WA_TAB-ODATE = 20170503.
APPEND WA_TAB TO ITAB.
WA_TAB-BUKRS = 'AX'.
WA_TAB-NETPR = 13.
WA_TAB-ODATE = 20170505.
APPEND WA_TAB TO ITAB.
WA_TAB-BUKRS = 'XX'.
WA_TAB-NETPR = 13.
WA_TAB-ODATE = 20170501.
APPEND WA_TAB TO ITAB.
WA_TAB-BUKRS = 'XX'.
WA_TAB-NETPR = 1213.
WA_TAB-ODATE = 20170503.
APPEND WA_TAB TO ITAB.
WA_TAB-BUKRS = 'XX'.
WA_TAB-NETPR = 3.
WA_TAB-ODATE = 20170505.
APPEND WA_TAB TO ITAB.
WA_TAB-BUKRS = 'ZX'.
WA_TAB-NETPR = 1213.
WA_TAB-ODATE = 20170501.
APPEND WA_TAB TO ITAB.
WA_TAB-BUKRS = 'ZX'.
WA_TAB-NETPR = 1223.
WA_TAB-ODATE = 20170503.
APPEND WA_TAB TO ITAB.
WA_TAB-BUKRS = 'ZX'.
WA_TAB-NETPR = 143.
WA_TAB-ODATE = 20170505.
APPEND WA_TAB TO ITAB.
WA_TAB-BUKRS = 'HX'.
WA_TAB-NETPR = 13.
WA_TAB-ODATE = 20170501.
APPEND WA_TAB TO ITAB.
WA_TAB-BUKRS = 'HX'.
WA_TAB-NETPR = 1213.
WA_TAB-ODATE = 20170503.
APPEND WA_TAB TO ITAB.
WA_TAB-BUKRS = 'HX'.
WA_TAB-NETPR = 113.
WA_TAB-ODATE = 20170505.
APPEND WA_TAB TO ITAB.
START-OF-SELECTION.
PERFORM DYNAMIC_INTERNAL_TABLE.
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*& FORM GET_AND_PROCESS_DATA
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*----------------------------------------------------------------------*
FORM DYNAMIC_INTERNAL_TABLE.
SORT ITAB BY BUKRS ODATE .
DATA TDATE LIKE SY-DATUM OCCURS 0 WITH HEADER LINE.
LOOP AT ITAB.
TDATE = ITAB-ODATE.
COLLECT TDATE.
ENDLOOP.
DATA: DY_TABLE TYPE REF TO DATA,
DY_WA TYPE REF TO DATA,
IT_STR TYPE LVC_T_FCAT, "是TABLE TYPE
WA_STR TYPE LVC_S_FCAT. "是一个STRUCTURE 用于存储即将构建的动态内表结构
FIELD-SYMBOLS: <DYN_TABLE> TYPE TABLE, "是一个标志,可以理解为一个指针,将来创建的内表就要通过它们来访问
<DYN_WA> TYPE ANY,
<FS> TYPE ANY.
WA_STR-FIELDNAME = 'BUKRS'.
WA_STR-COL_POS = 1. " 表示第一列 --- 可省略,默认情况下,1对应到内表的第一列,如果指定,则按指定的列顺序生成内表
WA_STR-INTTYPE = 'CHAR'. "数据类型
WA_STR-INTLEN = 20. "长度
APPEND WA_STR TO IT_STR.
CLEAR WA_STR.
PERFORM INPUT_FIELDCAT USING 'BUKRS' 'BUKRS'.
LOOP AT TDATE.
WA_STR-FIELDNAME = TDATE.
WA_STR-COL_POS = 1.
WA_STR-INTTYPE = 'CHAR'.
WA_STR-INTLEN = 20.
APPEND WA_STR TO IT_STR.
CLEAR WA_STR.
PERFORM INPUT_FIELDCAT USING TDATE TDATE.
ENDLOOP.
CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING
IT_FIELDCATALOG = IT_STR
IMPORTING
EP_TABLE = DY_TABLE.
ASSIGN DY_TABLE->* TO <DYN_TABLE>.
CREATE DATA DY_WA LIKE LINE OF <DYN_TABLE>.
ASSIGN DY_WA->* TO <DYN_WA>.
DATA FIELDNAME(20).
LOOP AT ITAB.
ASSIGN COMPONENT 'BUKRS' OF STRUCTURE <DYN_WA> TO <FS>. "分配结构里的销售办事处,用FS字段 指向它
<FS> = ITAB-BUKRS.
FIELDNAME = ITAB-ODATE. "这里把日期类型定义为CHAR型,因为用DATS报错。
ASSIGN COMPONENT FIELDNAME OF STRUCTURE <DYN_WA> TO <FS>. "分配结构的日期,将FS指向它
<FS> = ITAB-NETPR.
AT END OF BUKRS.
APPEND <DYN_WA> TO <DYN_TABLE>.
ENDAT.
ENDLOOP.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
* I_INTERFACE_CHECK = ' '
* I_BYPASSING_BUFFER =
* I_BUFFER_ACTIVE =
I_CALLBACK_PROGRAM = SY-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 = 'TEST'
* I_GRID_SETTINGS =
IS_LAYOUT_LVC = GS_LAYOUT
IT_FIELDCAT_LVC = GT_FIELDCAT
* IT_EXCLUDING =
* IT_SPECIAL_GROUPS_LVC =
* IT_SORT_LVC =
* IT_FILTER_LVC =
* IT_HYPERLINK =
* IS_SEL_HIDE =
* I_DEFAULT = 'X'
* I_SAVE = ' '
* IS_VARIANT =
* IT_EVENTS =
* IT_EVENT_EXIT =
* IS_PRINT_LVC =
* IS_REPREP_ID_LVC =
* I_SCREEN_START_COLUMN = 0
* I_SCREEN_START_LINE = 0
* I_SCREEN_END_COLUMN = 0
* I_SCREEN_END_LINE = 0
* I_HTML_HEIGHT_TOP =
* I_HTML_HEIGHT_END =
* IT_ALV_GRAPHICS =
* IT_EXCEPT_QINFO_LVC =
* IR_SALV_FULLSCREEN_ADAPTER =
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER =
* ES_EXIT_CAUSED_BY_USER =
TABLES
T_OUTTAB = <DYN_TABLE>
* EXCEPTIONS
* PROGRAM_ERROR = 1
* OTHERS = 2
.
IF SY-SUBRC <> 0.
* IMPLEMENT SUITABLE ERROR HANDLING HERE
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& FORM INPUT_FIELDCAT
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* -->P_TDATE TEXT
* -->P_TDATE TEXT
*----------------------------------------------------------------------*
FORM INPUT_FIELDCAT USING P_TDATE1
P_TDATE2.
ADD_FIELD P_TDATE1 P_TDATE2.
ENDFORM.