二维码

[动态alv] 一个简单的alv报表的行列换位的例子

Twilight发表于 2014-08-01 15:09Twilight 最后回复于 2014-08-01 15:09 [复制链接] 4871 0

选择屏幕(任意输入一个表名,我们以VBAK为例)
output transposed 1.png
执行效果
output transposed 2.png

程序代码:
  1. * Type pools declaration for ALV
  2. TYPE-POOLS: slis.
  3. *Declarations for ALV, dynamic table and col no for transpose
  4. DATA:    l_col    TYPE sy-tabix,
  5.          l_structure   TYPE REF TO data,
  6.          l_dyntable    TYPE REF TO data,
  7.          wa_lvc_cat  TYPE lvc_s_fcat,
  8.          lt_lvc_cat  TYPE lvc_t_fcat,
  9.          lt_fieldcatalogue     TYPE slis_t_fieldcat_alv,
  10.          wa_fieldcat TYPE slis_fieldcat_alv,
  11.          lt_fieldcat TYPE slis_t_fieldcat_alv,
  12.          lt_layout   TYPE slis_layout_alv.
  13. *Field symbols declarations
  14. FIELD-SYMBOLS :
  15.   <header>    TYPE ANY,
  16.   <dynheader> TYPE ANY,
  17.   <dyndata>   TYPE ANY,
  18.   <ls_table>      TYPE ANY,
  19.   <dynamictable>      TYPE STANDARD TABLE,
  20.   <it_table> TYPE STANDARD TABLE.
  21. *Input the name of the table
  22. PARAMETERS p_table TYPE dd02l-tabname OBLIGATORY.
  23. *Initialization event
  24. INITIALIZATION.
  25. *Start of selection event
  26. START-OF-SELECTION.
  27. * Create internal table of dynamic type
  28.   CREATE DATA l_dyntable TYPE STANDARD TABLE OF (p_table)
  29.                        WITH NON-UNIQUE DEFAULT KEY.
  30.   ASSIGN l_dyntable->* TO <it_table>.
  31. *select statement to select data from the table as input into
  32. *our dynamic internal table.
  33. *Here i have restricted only till 5 rows.
  34. *You can set a variable and give no of rows to be fetched
  35. *The variable can be set in your select statement
  36.   SELECT * INTO CORRESPONDING FIELDS OF TABLE <it_table>
  37.                 FROM (p_table) UP TO 5 ROWS.
  38. *Fieldcatalogue definitions
  39.   wa_lvc_cat-fieldname = 'COLUMNTEXT'.
  40.   wa_lvc_cat-ref_table = 'LVC_S_DETA'.
  41.   APPEND wa_lvc_cat TO lt_lvc_cat.
  42.   wa_fieldcat-fieldname = 'COLUMNTEXT'.
  43.   wa_fieldcat-ref_tabname = 'LVC_S_DETA'.
  44.   wa_fieldcat-key  = 'X'..
  45.   APPEND wa_fieldcat TO lt_fieldcat.
  46.   DESCRIBE TABLE <it_table>.
  47.   DO sy-tfill TIMES.
  48. *   For each line, a column 'VALUEx' is created in the fieldcatalog
  49. *   Build Fieldcatalog
  50.     WRITE sy-index TO wa_lvc_cat-fieldname LEFT-JUSTIFIED.
  51.     CONCATENATE 'VALUE' wa_lvc_cat-fieldname
  52.            INTO wa_lvc_cat-fieldname.
  53.     wa_lvc_cat-ref_field = 'VALUE'.
  54.     wa_lvc_cat-ref_table = 'LVC_S_DETA'.
  55.     APPEND wa_lvc_cat TO lt_lvc_cat.
  56. *   Build Fieldcatalog
  57.     CLEAR wa_fieldcat.
  58.     wa_fieldcat-fieldname = wa_lvc_cat-fieldname.
  59.     wa_fieldcat-ref_fieldname = 'VALUE'.
  60.     wa_fieldcat-ref_tabname = 'LVC_S_DETA'.
  61.     APPEND wa_fieldcat TO lt_fieldcat.
  62.   ENDDO.
  63. * Create dynamic internal table
  64.   CALL METHOD cl_alv_table_create=>create_dynamic_table
  65.     EXPORTING
  66.       it_fieldcatalog = lt_lvc_cat
  67.     IMPORTING
  68.       ep_table        = l_dyntable.
  69.   ASSIGN l_dyntable->* TO <dynamictable>.
  70. * Create structure as structure of the internal table
  71.   CREATE DATA l_structure LIKE LINE OF <dynamictable>.
  72.   ASSIGN l_structure->* TO <header>.
  73. * Create structure = structure of the internal table
  74.   CREATE DATA l_structure LIKE LINE OF <it_table>.
  75.   ASSIGN l_structure->* TO <ls_table>.
  76. * Create field catalog from our table structure
  77.   CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
  78.     EXPORTING
  79.       i_structure_name       = p_table
  80.     CHANGING
  81.       ct_fieldcat            = lt_fieldcatalogue
  82.     EXCEPTIONS
  83.       inconsistent_interface = 1
  84.       program_error          = 2
  85.       OTHERS                 = 3.
  86.   IF sy-subrc <> 0.
  87.     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
  88.             WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  89.   ENDIF.
  90.   DESCRIBE TABLE lt_fieldcatalogue.
  91. * Fill the internal to display <dynamictable>
  92.   DO sy-tfill TIMES.
  93.     IF sy-index = 1.
  94.       READ TABLE lt_fieldcatalogue INTO wa_fieldcat INDEX 1.
  95.     ENDIF.
  96. *   For each field of it_table
  97.     ASSIGN COMPONENT 1 OF STRUCTURE <header> TO <dynheader>.
  98.     IF sy-subrc NE 0. EXIT .ENDIF.
  99.     READ TABLE lt_fieldcatalogue INTO wa_fieldcat INDEX sy-index.
  100. *   Fill 1st column
  101.     <dynheader> = wa_fieldcat-seltext_m.
  102.     IF <dynheader> IS INITIAL.
  103.       <dynheader> = wa_fieldcat-fieldname.
  104.     ENDIF.
  105. *Filling the other columns
  106.     LOOP AT <it_table> INTO <ls_table>.
  107.       l_col = sy-tabix + 1.
  108.       ASSIGN COMPONENT sy-index OF STRUCTURE <ls_table> TO <dyndata>.
  109.       IF sy-subrc NE 0. EXIT .ENDIF.
  110.       ASSIGN COMPONENT l_col OF STRUCTURE <header> TO
  111. <dynheader>.
  112.       IF sy-subrc NE 0. EXIT .ENDIF.
  113.       WRITE <dyndata> TO <dynheader> LEFT-JUSTIFIED.
  114.     ENDLOOP.
  115.     APPEND <header> TO <dynamictable>.
  116.   ENDDO.
  117. *Layout for ALV output
  118.   lt_layout-zebra = 'X'.
  119.   lt_layout-no_colhead = 'X'..
  120.   lt_layout-colwidth_optimize ='X'.
  121.   lt_layout-window_titlebar = 'ALV GRID TRANSPOSED'.
  122. *ALV Grid output for display
  123.   CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
  124.     EXPORTING
  125.       is_layout   = lt_layout
  126.       it_fieldcat = lt_fieldcat
  127.     TABLES
  128.       t_outtab    = <dynamictable>.
复制代码
回复

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表