根据别人的经验总结的如下动态创建内表的代码,自撸版
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE, <dyn_wa> TYPE ANY, <dyn_field>. DATA: dy_table TYPE REF TO data, dy_line TYPE REF TO data, it_structure TYPE lvc_t_fcat, wa_structure TYPE lvc_s_fcat. DATA: lt_table LIKE TABLE OF dntab, ls_table TYPE dntab. DATA gt_fieldcat TYPE slis_t_fieldcat_alv. DATA ls_fieldcat LIKE LINE OF gt_fieldcat. START-OF-SELECTION. PERFORM create_structure. "定义内表的结构 PERFORM create_dynamic_table. "按照定义的内表结构,产生一个内表 PERFORM write_data_to_dyntable. "向动态内表中写数 PERFORM show_alv_data. "显示数据 *&---------------------------------------------------------------------* *& Form create_structure *&---------------------------------------------------------------------* FORM create_structure . * 获取表的结构信息 CALL FUNCTION 'NAMETAB_GET' EXPORTING langu = sy-langu tabname = 'MARA' TABLES nametab = lt_table EXCEPTIONS no_texts_found = 1. LOOP AT lt_table INTO ls_table. CLEAR wa_structure. wa_structure-fieldname = ls_table-fieldname. "列名 wa_structure-col_pos = ls_table-position. "可以省略,默认情况下,第一行对应到生产内表的第一列,如果指定,则按指定的列顺序生成内表 wa_structure-inttype = ls_table-inttype. "数据类型 wa_structure-intlen = ls_table-intlen. "长度 wa_structure-reptext = ls_table-fieldtext. "标题文本 APPEND wa_structure TO it_structure. ENDLOOP. ENDFORM. " create_structure *&---------------------------------------------------------------------* *& Form create_dynamic_table *&---------------------------------------------------------------------* FORM create_dynamic_table . CALL METHOD cl_alv_table_create=>create_dynamic_table EXPORTING it_fieldcatalog = it_structure IMPORTING ep_table = dy_table. ASSIGN dy_table->* TO <dyn_table>. " 用表类型指针 <dyn_table> 指向 数据对象的内容. ENDFORM. " create_dynamic_table *&---------------------------------------------------------------------* *& Form write_data_to_dyntable *&---------------------------------------------------------------------* FORM write_data_to_dyntable . DATA: i TYPE n. CREATE DATA dy_line LIKE LINE OF <dyn_table>. " 建立一个与动态内表结构相同的数据对象,且数据对象为是一个结构 ASSIGN dy_line->* TO <dyn_wa>. " 用<dyn_wa>指针指向该结构 DO 5 TIMES. i = i + 1. "随便写的物料号 LOOP AT it_structure INTO wa_structure. "懒得填数据,所以用IF卡控,随便填物料号 IF wa_structure-fieldname = 'MATNR'. ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE <dyn_wa> TO <dyn_field>. " 用指针 <dyn_field>指向工作区<dyn_wa>中的一个字段,字段名为wa_structure-fieldname. <dyn_field> = i. " 给指针指向的字段赋值 ENDIF. ENDLOOP. APPEND <dyn_wa> TO <dyn_table>. ENDDO. ENDFORM. " write_data_to_dyntable *&---------------------------------------------------------------------* *& Form show_alv_data *&---------------------------------------------------------------------* FORM show_alv_data. LOOP AT it_structure INTO wa_structure.. ls_fieldcat-fieldname = wa_structure-fieldname. ls_fieldcat-seltext_m = wa_structure-reptext. APPEND ls_fieldcat to gt_fieldcat. CLEAR ls_fieldcat. ENDLOOP. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING it_fieldcat = gt_fieldcat TABLES t_outtab = <dyn_table> 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.
根据自己定义的内表创建
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE, <dyn_wa> TYPE ANY, <dyn_field>. DATA: dy_table TYPE REF TO data, dy_line TYPE REF TO data, it_structure TYPE lvc_t_fcat, wa_structure TYPE lvc_s_fcat. DATA: lt_table LIKE TABLE OF dntab, ls_table TYPE dntab. DATA gt_fieldcat TYPE slis_t_fieldcat_alv. DATA ls_fieldcat LIKE LINE OF gt_fieldcat. ***添加结构字段 DATA:BEGIN OF gt_itab, matnr LIKE makt-matnr, spras LIKE makt-spras, maktx LIKE makt-maktx, END OF gt_itab. DATA:gt_components LIKE TABLE OF rstrucinfo, gs_components LIKE LINE OF gt_components. START-OF-SELECTION. PERFORM create_structure. "定义内表的结构 PERFORM create_dynamic_table. "按照定义的内表结构,产生一个内表 PERFORM write_data_to_dyntable. "向动态内表中写数 PERFORM show_alv_data. "显示数据 *&---------------------------------------------------------------------* *& Form create_structure *&---------------------------------------------------------------------* FORM create_structure . * 获取内表的结构信息 CALL FUNCTION 'GET_COMPONENT_LIST' EXPORTING program = sy-repid fieldname = 'GT_ITAB' TABLES components = gt_components. LOOP AT gt_components INTO gs_components. CLEAR wa_structure. wa_structure-fieldname = gs_components-compname. "列名 * wa_structure-col_pos = ls_table-position. "可以省略,默认情况下,第一行对应到生产内表的第一列,如果指定,则按指定的列顺序生成内表 wa_structure-inttype = gs_components-type. "数据类型 wa_structure-intlen = gs_components-olen. "长度 "标题暂时用hard code IF gs_components-compname = 'MATNR'. wa_structure-reptext = '物料号'. "标题文本 ELSEIF gs_components-compname = 'SPRAS'. wa_structure-reptext = '语言'. ELSEIF gs_components-compname = 'MAKTX'. wa_structure-reptext = '描述'. ELSE. ENDIF. APPEND wa_structure TO it_structure. ENDLOOP. ENDFORM. " create_structure *&---------------------------------------------------------------------* *& Form create_dynamic_table *&---------------------------------------------------------------------* FORM create_dynamic_table . CALL METHOD cl_alv_table_create=>create_dynamic_table EXPORTING it_fieldcatalog = it_structure IMPORTING ep_table = dy_table. ASSIGN dy_table->* TO <dyn_table>. " 用表类型指针 <dyn_table> 指向 数据对象的内容. ENDFORM. " create_dynamic_table *&---------------------------------------------------------------------* *& Form write_data_to_dyntable *&---------------------------------------------------------------------* FORM write_data_to_dyntable . CREATE DATA dy_line LIKE LINE OF <dyn_table>. " 建立一个与动态内表结构相同的数据对象,且数据对象为是一个结构 ASSIGN dy_line->* TO <dyn_wa>. " 用<dyn_wa>指针指向该结构 ***动态字段赋值 暂时用hard code DEFINE m_add_value. ASSIGN COMPONENT &1 OF STRUCTURE <dyn_wa> TO <dyn_field>. <dyn_field> = &2. UNASSIGN <dyn_field>. END-OF-DEFINITION. m_add_value 'MATNR' '物料编码'. m_add_value 'SPRAS' '语言代码'. m_add_value 'MAKTX' '物料名称'. APPEND <dyn_wa> TO <dyn_table>. ENDFORM. " write_data_to_dyntable *&---------------------------------------------------------------------* *& Form show_alv_data *&---------------------------------------------------------------------* FORM show_alv_data. LOOP AT it_structure INTO wa_structure.. ls_fieldcat-fieldname = wa_structure-fieldname. ls_fieldcat-seltext_m = wa_structure-reptext. APPEND ls_fieldcat to gt_fieldcat. CLEAR ls_fieldcat. ENDLOOP. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING it_fieldcat = gt_fieldcat TABLES t_outtab = <dyn_table> 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.