动态ALV显示的行列,解决部分报表需求。
运行截图如下:
选择屏幕:
ALV输出效果1:
ALV输出效果2::
ABAP代码:
1 *&---------------------------------------------------------------------* 2 *& Report ZCHENH068 3 *& 4 *&---------------------------------------------------------------------* 5 *& 6 *& 7 *&---------------------------------------------------------------------* 8 9 REPORT zchenh068. 10 DATA: wa_layo TYPE lvc_s_layo. 11 DATA: wa_fcat TYPE lvc_s_fcat, 12 it_fcat TYPE lvc_t_fcat. 13 14 DATA it_scol TYPE lvc_t_scol. 15 16 DATA: g_grid TYPE REF TO cl_gui_alv_grid. 17 18 FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE, 19 <dyn_wa>, 20 <dyn_field>. 21 22 DATA: dy_table TYPE REF TO data, 23 dy_line TYPE REF TO data. 24 25 DATA: colname(10), 26 lv_col(2), 27 lv_row(2), 28 lv_title TYPE string. 29 30 * 列数 31 PARAMETERS: col TYPE i. 32 PARAMETERS: row TYPE i. 33 34 CLEAR: it_fcat. 35 36 * 根据条件动态生成列 37 DO col TIMES. 38 lv_col = lv_col + 1. 39 CONCATENATE 'COL' lv_col INTO colname. 40 CONCATENATE '第' lv_col '列抬头描述' INTO lv_title. 41 PERFORM frm_add_fcat USING: 42 colname 'C' lv_title '100'. 43 ENDDO. 44 45 * 根据it_fact生成动态表 46 CALL METHOD cl_alv_table_create=>create_dynamic_table 47 EXPORTING 48 it_fieldcatalog = it_fcat 49 IMPORTING 50 ep_table = dy_table. 51 52 ASSIGN dy_table->* TO <dyn_table>. 53 54 CREATE DATA dy_line LIKE LINE OF <dyn_table>. 55 56 ASSIGN dy_line->* TO <dyn_wa>. 57 58 * 给表字段赋值 59 60 * 相当于给内表放入row行数据 61 DO row TIMES. 62 * 相当于给内表放入col列数据 63 CLEAR: lv_col. 64 lv_row = lv_row + 1. 65 DO col TIMES. 66 lv_col = lv_col + 1. 67 CONCATENATE 'COL' lv_col INTO colname. 68 ASSIGN COMPONENT colname OF STRUCTURE <dyn_wa> TO <dyn_field>. 69 * CONCATENATE '第' lv_row '行数据' '第' lv_col '列数据' INTO <dyn_field>. 70 CONCATENATE '行,列坐标:(' lv_row',' lv_col ')' INTO <dyn_field>. 71 ENDDO. 72 APPEND <dyn_wa> TO <dyn_table>. 73 ENDDO. 74 75 CLEAR: wa_layo. 76 wa_layo-zebra = 'X'. 77 wa_layo-cwidth_opt = 'X'. 78 79 CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' 80 EXPORTING 81 i_callback_program = sy-repid 82 is_layout_lvc = wa_layo 83 it_fieldcat_lvc = it_fcat 84 TABLES 85 t_outtab = <dyn_table> 86 EXCEPTIONS 87 program_error = 1 88 OTHERS = 2. 89 IF sy-subrc <> 0. 90 MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno 91 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 92 ENDIF. 93 94 *&---------------------------------------------------------------------* 95 *& Form frm_add_fcat 96 *&---------------------------------------------------------------------* 97 * text 98 *----------------------------------------------------------------------* 99 * -->VALUE1 text 100 * -->VALUE2 text 101 * -->VALUE3 text 102 * -->VALUE4 text 103 *----------------------------------------------------------------------* 104 FORM frm_add_fcat USING value1 value2 value3 value4. 105 wa_fcat-fieldname = value1. 106 wa_fcat-inttype = value2. 107 wa_fcat-reptext = value3. 108 wa_fcat-intlen = value4. 109 APPEND wa_fcat TO it_fcat. 110 CLEAR: wa_fcat. 111 ENDFORM. "frm_add_fcat