1、取数表
image.png
数据如下:
image.png
2、上代码
*&---------------------------------------------------------------------*
*& Report ZGML_FS_DT
*&
*&---------------------------------------------------------------------*
*&动态内表
*&create time:2018.02.02
*&---------------------------------------------------------------------*
REPORT ZGML_FS_DT.
DATA: gt_dt01 TYPE TABLE OF ZDT01,
gw_dt01 TYPE ZDT01.
*定义ALV相关变量
DATA :gt_fieldcat TYPE lvc_t_fcat,
gw_fieldcat TYPE lvc_s_fcat.
RANGES r_month FOR gw_dt01-ZMONTH.
DATA gv_num TYPE i.
*定义动态内表相关变量
FIELD-SYMBOLS: <FT_VB> TYPE STANDARD TABLE,
<FS_VB> TYPE ANY,
<dyn_field> TYPE ANY.
DATA gv_pos TYPE I.
DATA GV_FIELDCAT TYPE C LENGTH 20.
DATA GV_MONTH TYPE C LENGTH 10.
DATA gv_index TYPE N LENGTH 2.
DATA gt_table TYPE REF TO data.
*定义宏:添加字段
DEFINE %%add_fieldcat.
CLEAR gw_fieldcat.
gv_pos = gv_pos + 1.
gw_fieldcat-COL_POS = GV_POS.
gw_fieldcat-FIELDNAME = &1.
gw_fieldcat-SCRTEXT_M = &2.
gw_fieldcat-CFIELDNAME = &3.
gw_fieldcat-OUTPUTLEN = 15.
APPEND gw_fieldcat TO gt_fieldcat.
END-OF-DEFINITION.
PARAMETERS p_year TYPE ZDT01-ZYEAR DEFAULT '2017'.
SELECT-OPTIONS s_comp FOR gw_dt01-ZCOMPANY.
SELECT-OPTIONS s_month FOR gw_dt01-ZMONTH.
START-OF-SELECTION.
PERFORM frm_get_data.
PERFORM frm_set_fieldcat.
PERFORM frm_set_dynamic_table."创建动态内表
PERFORM frm_add_data. "向动态内表增加数据
END-OF-SELECTION.
IF gt_dt01 IS NOT INITIAL.
PERFORM frm_display_data.
ELSE.
MESSAGE '没有数据' TYPE 'E'.
ENDIF.
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
FORM FRM_GET_DATA .
SELECT * FROM ZDT01
INTO TABLE gt_dt01
WHERE ZYEAR = p_year
AND ZCOMPANY IN s_comp
AND ZMONTH IN s_month.
ENDFORM. " FRM_GET_DATA
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY_DATA
*&---------------------------------------------------------------------*
FORM FRM_DISPLAY_DATA .
IF gt_dt01 IS NOT INITIAL AND <FT_VB> IS ASSIGNED.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
IT_FIELDCAT_LVC = gt_fieldcat
TABLES
T_OUTTAB = <FT_VB>
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2
.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
ENDIF.
ENDFORM. " FRM_DISPLAY_DATA
*&---------------------------------------------------------------------*
*& Form FRM_SET_FIELDCAT
*&---------------------------------------------------------------------*
FORM FRM_SET_FIELDCAT .
%%add_fieldcat 'ZCOMPANY' '公司' ''.
%%add_fieldcat 'ZYEAR' '年份' ''.
r_month[] = s_month[].
READ TABLE r_month INDEX 1.
gv_num = r_month-high - r_month-low + 1.
gv_index = r_month-low.
DO gv_num TIMES.
CONCATENATE 'MONTH_' gv_index INTO GV_FIELDCAT.
CONCATENATE gv_index '月' INTO GV_MONTH.
%%add_fieldcat GV_FIELDCAT GV_MONTH 'ZBZ'.
gv_index = gv_index + 1.
ENDDO.
%%add_fieldcat 'ZBZ' '货币单位' ''.
ENDFORM. " FRM_SET_FIELDCAT
*&---------------------------------------------------------------------*
*& Form FRM_SET_DYNAMIC_TABLE
*&---------------------------------------------------------------------*
FORM FRM_SET_DYNAMIC_TABLE .
DATA gt_table TYPE REF TO data.
DATA gw_line TYPE REF TO data.
CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING
IT_FIELDCATALOG = gt_fieldcat
IMPORTING
EP_TABLE = gt_table
* E_STYLE_FNAME =
EXCEPTIONS
GENERATE_SUBPOOL_DIR_FULL = 1
others = 2.
IF SY-SUBRC <> 0.
EXIT.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ASSIGN gt_table->* TO <FT_VB>."动态内表
CREATE DATA gw_line LIKE LINE OF <FT_VB>.
ASSIGN gw_line->* TO <FS_VB>."动态工作区
ENDFORM. " FRM_SET_DYNAMIC_TABLE
*&---------------------------------------------------------------------*
*& Form FRM_ADD_DATA
*&---------------------------------------------------------------------*
FORM FRM_ADD_DATA .
DATA lw_dt01 TYPE ZDT01.
DATA lv_fieldname TYPE C LENGTH 10.
SORT gt_dt01 BY ZCOMPANY.
LOOP AT gt_dt01 INTO lw_dt01.
MOVE-CORRESPONDING lw_dt01 TO gw_dt01.
AT NEW ZCOMPANY.
CLEAR <FS_VB>.
ASSIGN COMPONENT 'ZCOMPANY' OF STRUCTURE <FS_VB> TO <dyn_field>.
<dyn_field> = gw_dt01-zcompany.
ASSIGN COMPONENT 'ZYEAR' OF STRUCTURE <FS_VB> TO <dyn_field>.
<dyn_field> = gw_dt01-zyear.
ASSIGN COMPONENT 'ZBZ' OF STRUCTURE <FS_VB> TO <dyn_field>.
<dyn_field> = gw_dt01-zbz.
ENDAT.
CONCATENATE 'MONTH_' gw_dt01-ZMONTH INTO lv_fieldname.
ASSIGN COMPONENT lv_fieldname OF STRUCTURE <FS_VB> TO <dyn_field>.
<dyn_field> = gw_dt01-INCOME.
AT END OF ZCOMPANY.
APPEND <FS_VB> TO <FT_VB>.
ENDAT.
ENDLOOP.
ENDFORM. " FRM_ADD_DATA
3、运行结果
1.png
2.png
3.png
4.png
网友评论