*&---------------------------------------------------------------------*
*& Report Z_GAOJIAN10293
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_SALAH_DYNAMICTAB_OLE_SL1.
TABLES T001.
TYPE-POOLS: ABAP,SLIS,OLE2.
DATA D_REF TYPE REF TO DATA.
DATA GT_FIELDCAT TYPE LVC_T_FCAT.
DATA GV_FIELDCAT TYPE LVC_S_FCAT.
DATA GT_EVENTS TYPE SLIS_T_EVENT.
DATA GV_EVENTS TYPE SLIS_ALV_EVENT.
DATA GT_01 LIKE TABLE OF DNTAB.
DATA WA_01 TYPE DNTAB.
DATA WA_02 LIKE LINE OF GT_01.
DATA DYN_WA TYPE REF TO DATA.
DATA WA_11 TYPE REF TO DATA.
DATA P_FULLPATH TYPE STRING.
PARAMETERS P_NAME TYPE TABNAME.
PARAMETERS P_FILE TYPE IBIPPARMS-PATH.
*AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
*CALL FUNCTION 'F4_FILENAME'
* EXPORTING
** PROGRAM_NAME = SYST-CPROG
** DYNPRO_NUMBER = SYST-DYNNR
* FIELD_NAME = 'p_file'
* IMPORTING
* FILE_NAME = p_file
* .
P_FULLPATH = P_FILE.
START-OF-SELECTION.
FIELD-SYMBOLS <DYN_TABLE> TYPE TABLE.
FIELD-SYMBOLS <DYN_WA> TYPE ANY.
FIELD-SYMBOLS <DYN_FIELD> TYPE ANY.
"取出表结构的字段目录
CALL FUNCTION 'NAMETAB_GET'
EXPORTING
LANGU = SY-LANGU
TABNAME = P_NAME
TABLES
NAMETAB = GT_01
EXCEPTIONS
INTERNAL_ERROR = 1
TABLE_HAS_NO_FIELDS = 2
TABLE_NOT_ACTIV = 3
NO_TEXTS_FOUND = 4
OTHERS = 5.
"根据取出的字段目录生成参考字段目录
LOOP AT GT_01 INTO WA_01.
GV_FIELDCAT-FIELDNAME = WA_01-FIELDNAME.
GV_FIELDCAT-REF_TABLE = P_NAME.
GV_FIELDCAT-REF_FIELD = WA_01-FIELDNAME.
APPEND GV_FIELDCAT TO GT_FIELDCAT.
CLEAR GV_FIELDCAT.
ENDLOOP.
"创建内表
CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING
IT_FIELDCATALOG = GT_FIELDCAT
IMPORTING
EP_TABLE = D_REF.
"指定生成的内表到字段符号
ASSIGN D_REF->* TO <DYN_TABLE>.
"从动态表中取数到动态内表中
SELECT *
FROM (P_NAME)
INTO CORRESPONDING FIELDS OF TABLE <DYN_TABLE>
UP TO 3 ROWS.
"事件获取
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
IMPORTING
ET_EVENTS = GT_EVENTS
EXCEPTIONS
LIST_TYPE_WRONG = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
LOOP AT GT_EVENTS INTO GV_EVENTS.
IF GV_EVENTS-NAME = 'PF_STATUS_SET'.
GV_EVENTS-FORM = 'FRM_STATUS'.
ENDIF.
IF GV_EVENTS-NAME = 'USER_COMMAND'.
GV_EVENTS-FORM = 'FRM_COMMAND'.
ENDIF.
MODIFY GT_EVENTS FROM GV_EVENTS INDEX SY-TABIX.
ENDLOOP.
"显示数据
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
I_CALLBACK_PROGRAM = SY-CPROG
I_STRUCTURE_NAME = P_NAME
IT_EVENTS = GT_EVENTS
TABLES
T_OUTTAB = <DYN_TABLE>
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF