如何根据运行时动态ALV布局变化创建动态内表结构

2021-11-08 00:43发布


          点击此处--->   EasySAP.com 群内免费提供SAP练习系统(在群公告中)

加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)


在“导出文件”按钮上单击 ALV 输出的应用程序工具栏上的 ALV 输出应下载到文件中。

那个按钮的OK CODE可以假设OK_Download。

通过调用功能模块REUSE_ALV_GRID_LAYOUT_INFO_GET,返回当前屏幕布局和字段目录。

*Function module that returns dynamic layout and field catalog

CALL FUNCTION 'REUSE_ALV_GRID_LAYOUT_INFO_GET'
 IMPORTING
   es_layout     = layout          "dynamic layout is returned in this parameter
   et_fieldcat   = fieldcat        "dynamic field catalog is returned in this parameter
   et_sort       = sort
   es_variant    = variant
 EXCEPTIONS
   no_infos      = 1
   program_error = 2
   OTHERS        = 3.
IF sy-subrc <> 0.
 MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
         WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
*Field Symbols declaration for creating dynamic internal table
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
                          <dyn_wa>   ,
                          <dyn_field>.
DATA: dy_table      TYPE REF TO data,
     ifc           TYPE lvc_t_fcat ,
     xfc           TYPE lvc_s_fcat ,
     count         TYPE i          ,
     count1        TYPE i          ,
     index         TYPE i          ,
     dy_line       TYPE REF TO data.
DATA counter   TYPE i.
DATA: line   TYPE string       ,
     list    LIKE TABLE OF line.
DATA: idetails           TYPE abap_compdescr_tab,
     xdetails           TYPE abap_compdescr    .
DATA: ref_table_des TYPE REF TO cl_abap_structdescr.
*Type created to append header in the dynamic int table
TYPES: BEGIN OF it_head,
       col1    TYPE string,
       col2    TYPE string,
       col3    TYPE string,
       col4    TYPE string,
       col5    TYPE string,
      END     OF it_head.
*Internal table created of type declared to append header line
DATA: t_head TYPE STANDARD TABLE OF it_head WITH HEADER LINE.
*File path names declaration
DATA: v_path      TYPE rlgrap-filename,
     v_pathname TYPE string                 .
*Deleting columns from field cat that are not in the current layout display
DELETE fieldcat WHERE no_out = 'X'.
*To change column headings in the download file dynamically
LOOP AT fieldcat INTO fieldcat1.
 IF fieldcat1-fieldname = 'VKBUR'.
   fieldcat1-seltext_l    = 'SOff.'.
 ENDIF.
 IF fieldcat1-fieldname = 'KTGRM'.
   fieldcat1-seltext_l    = 'AAG'.
 ENDIF.
 IF fieldcat1-fieldname = 'VRGAR'.
   fieldcat1-seltext_l    = 'Rec.Typ'.
 ENDIF.
 IF fieldcat1-fieldname   = 'RBELN'.
   fieldcat1-seltext_l = 'Ref.Doc.'.
 ENDIF.
 IF fieldcat1-fieldname   = 'RPOSN'.
   fieldcat1-seltext_l     = 'Ref.Item'.
 ENDIF.
 IF fieldcat1-fieldname    = 'BUKRS'.
   fieldcat1-seltext_l       = 'CoCd'.
 ENDIF.
 IF fieldcat1-fieldname    = '_WAERS'.
   fieldcat1-seltext_l       = 'Curr.'.
   fieldcat1-seltext_m    = 'Curr.'.
   fieldcat1-seltext_s      = 'Curr.'.
 ENDIF.
 IF fieldcat1-fieldname   = 'VKORG'.
   fieldcat1-seltext_l      = 'SOrg'.
 ENDIF.
 IF fieldcat1-fieldname   = 'BZIRK'.
   fieldcat1-seltext_l      = 'Sls Dist.'.
 ENDIF.
 IF fieldcat1-fieldname   = 'LAND1'.
   fieldcat1-seltext_l      = 'Ctry'.
 ENDIF.
 IF fieldcat1-fieldname   = 'KUNWE'.
   fieldcat1-seltext_l      = 'Ship-To-Pty'.
 ENDIF.
 IF fieldcat1-fieldname   = 'AUART'.
   fieldcat1-seltext_l      = 'Sls Doc Ty'.
 ENDIF.
 MODIFY fieldcat FROM fieldcat1.
ENDLOOP.
*Looping at field cat internal table to populate another field cat to be passed
* In method used below for creating final dynamic internal table

LOOP AT fieldcat INTO fieldcat1.
 CLEAR xfc.
 xfc-fieldname            = fieldcat1-fieldname.
 xfc-datatype              = fieldcat1-datatype.
 xfc-intlen                    = fieldcat1-intlen.
 IF fieldcat1-fieldname = 'PERIO'.
   xfc-fieldname      = fieldcat1-fieldname.
   xfc-datatype        = 'STRING'.
   xfc-intlen         = '8'.
 ENDIF.
 IF fieldcat1-fieldname = 'HZDAT'.
   xfc-fieldname            = fieldcat1-fieldname.
   xfc-datatype              = 'STRING'.
   xfc-intlen                = '10'.
 ENDIF.
 IF fieldcat1-fieldname = 'BUDAT'.
   xfc-fieldname           = fieldcat1-fieldname.
   xfc-datatype             = 'STRING'.
   xfc-intlen               = '10'.
 ENDIF.
 IF fieldcat1-fieldname = 'FADAT'.
   xfc-fieldname            = fieldcat1-fieldname.
   xfc-datatype              = 'STRING'.
   xfc-intlen                = '10'.
 ENDIF.
 IF fieldcat1-fieldname = 'ALTPERIO'.
   xfc-fieldname          = fieldcat1-fieldname.
   xfc-datatype            = 'STRING'.
   xfc-intlen              = '8'.
 ENDIF.
* To increase the length of the customer name field
 IF fieldcat1-fieldname   = 'CUST_NAME'.
   xfc-fieldname             = fieldcat1-fieldname.
   xfc-intlen               = '35'.
 ENDIF.
*To change Net Revenue field Data type
 IF fieldcat1-fieldname = 'VVNTM'.    "Net Revenue
   xfc-fieldname            = fieldcat1-fieldname.
   xfc-datatype              = 'STRING'.
 ENDIF.
 IF fieldcat1-fieldname = 'ERLOS'.    "Revenue
   xfc-fieldname            = fieldcat1-fieldname.
   xfc-datatype              = 'STRING'.
 ENDIF.
 IF fieldcat1-fieldname = 'VVDIS'.     "Discount
   xfc-fieldname           = fieldcat1-fieldname.
   xfc-datatype             = 'STRING'.
 ENDIF.
 IF fieldcat1-fieldname = 'KWFRGR'.    "Freight
   xfc-fieldname           = fieldcat1-fieldname.
   xfc-datatype             = 'STRING'.
 ENDIF.
 APPEND xfc            TO ifc.
ENDLOOP.
CLEAR fieldcat1.
*Method called to create dynamic internal table on the basis of field catalog created above
CALL METHOD cl_alv_table_create=>create_dynamic_table
 EXPORTING
   it_fieldcatalog = ifc                     "field catalog appended above
 IMPORTING
   ep_table        = dy_table.            "Dynamic internal table which will be created
ASSIGN dy_table->* TO <dyn_table>.
*Create dynamic work area and assign to FS
CREATE DATA dy_line LIKE LINE OF <dyn_table>.
ASSIGN dy_line->* TO <dyn_wa>.
*Variables declared to insert column heading in the dynamic internal table created
CLEAR: count, index. "To find number of columns present in the dynamic field catalog displayed in a variable count
DESCRIBE TABLE ifc LINES count.
index = 1.
count1 = 1.
*Subroutine to append header line in the final download internal table
PERFORM append_heading TABLES t_head
                       USING count
                             count1
                             index
                             fieldcat
                             fieldcat1.
*moving contents to final dynamic internal table to be downloaded from the final internal table
*populated
CLEAR <dyn_wa>.
LOOP AT ep_tabx1.
 MOVE-CORRESPONDING ep_tabx1 TO <dyn_wa>.
 APPEND <dyn_wa> TO <dyn_table> .
 CLEAR <dyn_wa>.
ENDLOOP.
*Clearing work areas
CLEAR: ep_tabx1,
       <dyn_wa>.
*Function module to select path for downloading file
CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
 EXPORTING
   program_name  = syst-repid
   dynpro_number = syst-dynnr
 CHANGING
   file_name     = v_path
 EXCEPTIONS
   mask_too_long = 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.
*Assigning path value to another var to be passed in gui_download function module
v_pathname = v_path.
*If user gives the value in path var then only download will be called
IF v_pathname IS NOT INITIAL.
*To concatenate extension with file path name
 CONCATENATE v_pathname '.xls' INTO v_pathname.
*FM to download header and contents to the given path

 CALL FUNCTION 'GUI_DOWNLOAD'
   EXPORTING
     write_field_separator = 'X'
     filename              = v_pathname
   TABLES
     data_tab              = t_head.
 REFRESH t_head.
 CLEAR t_head.
 CALL FUNCTION 'GUI_DOWNLOAD'
   EXPORTING
     filename                = v_pathname
     filetype                = 'ASC'
     append                  = 'X'            "for appending Heading in the internal table to be downloaded
     write_field_separator   = 'X'
     wk1_n_format            = '22'
     wk1_n_size              = '18'
   TABLES
     data_tab                = <dyn_table>
   EXCEPTIONS
     file_write_error        = 1
     no_batch                = 2
     gui_refuse_filetransfer = 3
     invalid_type            = 4
     no_authority            = 5
     unknown_error           = 6
     header_not_allowed      = 7
     separator_not_allowed   = 8
     filesize_not_allowed    = 9
     header_too_long         = 10
     dp_error_create         = 11
     dp_error_send           = 12
     dp_error_write          = 13
     unknown_dp_error        = 14
     access_denied           = 15
     dp_out_of_memory        = 16
     disk_full               = 17
     dp_timeout              = 18
     file_not_found          = 19
     dataprovider_exception  = 20
     control_flush_error     = 21
     OTHERS                  = 22.
 IF sy-subrc <> 0.
   MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
   WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
 ENDIF.
ENDIF.
CLEAR: v_pathname, v_path.
*Subroutine to append header line in the final download internal table
FORM append_heading TABLES t_head    STRUCTURE t_head
                   USING count     TYPE i
                         count1    TYPE i
                         index     TYPE i
                         fieldcat TYPE slis_t_fieldcat_alv
                         fieldcat1 TYPE slis_fieldcat_alv.
*Created header line to be appended in final download internal table
 IF count NE 0.
   DO 1 TIMES.
     READ TABLE fieldcat INTO fieldcat1 INDEX index.
     t_head-col1 = fieldcat1-seltext_l.
     IF count    = count1.
       EXIT.
     ELSE.
       count1 = count1 + 1.
       index = index + 1.
     ENDIF.
   ENDDO.
   APPEND t_head.
   CLEAR t_head.
 ENDIF.
*Increment the count1 and Index variable for the number of columns
* you want to have your downloaded file should have headings.


赞赏支持