Skip to Content
0
Oct 26, 2020 at 12:30 PM

about dynamic excel downloading as per layout on ALV.

129 Views Last edit Oct 27, 2020 at 04:49 AM 3 rev

Hello All,

I am facing an issue related to excel downloading I have created a button through which the user can download the excel as the layout they fix. For that, I am creating the FM which can download the excel dynamically. Here the problem I am facing is how to do the MOVE-CORRESPONDING between the work area and field symbol.

I just want to move the data from the work area to the field symbol and then from the field symbol to an internal table which is also a field symbol.

here is my code please help and one more thing the main internal table contains 71 fields but as per layout there are only 57 fields on output and I want to put the same data in excel.

Is there a concept that can act like MOVE-CORRESPONDING between Field symbols?

*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(GT_FIELDCAT) TYPE  SLIS_T_FIELDCAT_ALV
*"     REFERENCE(DEFAULT_FILE_PATH) TYPE  STRING
*"  TABLES
*"      IT_TAB
*"----------------------------------------------------------------------
  TYPE-POOLS :SLIS.
  FIELD-SYMBOLS : <DYN_STR>  TYPE ANY,
                  <DYN_STR1> TYPE ANY,
                  <STR_COMP> TYPE ABAP_COMPDESCR,
                  <TAB>      TYPE STANDARD TABLE,
                  <WA_TAB>   TYPE ANY,
                  <FIELD>    TYPE ANY,
                  <F_ST1>    TYPE ANY.
  FIELD-SYMBOLS : <TABLE>   TYPE STANDARD TABLE,
                  <WA_AREA> TYPE ANY.
  DATA: DATATYPE TYPE REF TO CL_ABAP_DATADESCR.
  DATA : WA_FIELDCAT LIKE LINE OF GT_FIELDCAT .
  DATA : L_STRUCTURE TYPE REF TO DATA,
         STRUC_DESC  TYPE REF TO CL_ABAP_STRUCTDESCR,
         C           TYPE STRING.
  DATA : V_VALUE TYPE STRING.
  DATA : GT_DYN_TABLE  TYPE REF TO DATA.
*  DATA: WA_FIELDCAT TYPE   SLIS_FIELDCAT_ALV.
  DATA : GT_DYN_FCAT   TYPE LVC_T_FCAT,
         GS_FIELD_LIST TYPE STRING.
  DATA : WT_DYN_FCAT         TYPE LINE OF  LVC_T_FCAT  .
  TYPES : BEGIN OF TY_STR_HDR,
            FIELD_NAME TYPE CHAR20,
          END OF TY_STR_HDR.
  DATA  GIT_HEADER_TEXT    TYPE STANDARD TABLE OF TY_STR_HDR.
  DATA:GREF_TABLE_RESULT TYPE REF TO CL_ABAP_TABLEDESCR,  " Table
       GS_COMPONENTS     TYPE STANDARD TABLE OF ABAP_COMPONENTDESCR
                          WITH KEY NAME,
       LS_COMPONENTS     LIKE LINE OF GS_COMPONENTS.
  DATA : LR_STRUCT_RESULT TYPE REF TO CL_ABAP_STRUCTDESCR,
         LR_TABLE_RESULT  TYPE REF TO  CL_ABAP_TABLEDESCR.
  FIELD-SYMBOLS: <FS_DYNAMIC_TAB> TYPE STANDARD TABLE .
  FIELD-SYMBOLS : <LFS_STRUCT> TYPE ANY,
                  <WA_TAB1>    TYPE ANY..
  DATA: LRT_TABLE  TYPE REF TO DATA,
        LRS_STRUCT TYPE REF TO DATA.


  DATA:LV_DATA_KIND TYPE ABAP_TYPEKIND.
  DATA : STR  TYPE STRING .
  DATA : STR1 TYPE STRING .
  DATA : STR2 TYPE STRING .

  """"""""""""""""""""""""""""""""""""""""""main code """"""""""""""""""""""""""""""""""""""""""""""
  CREATE DATA L_STRUCTURE LIKE LINE OF IT_TAB.
  ASSIGN L_STRUCTURE->* TO <DYN_STR>.
  STRUC_DESC ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( <DYN_STR> ).
  ASSIGN STRUC_DESC->COMPONENTS TO <TAB> .
  """"""""""""""""""""""""""""""dynamic structure """""""""""""""""""""""""""""""""""""""""""""""""""
  LOOP AT  GT_FIELDCAT INTO WA_FIELDCAT .
    MOVE-CORRESPONDING WA_FIELDCAT TO WT_DYN_FCAT .
    APPEND WT_DYN_FCAT TO GT_DYN_FCAT .
    CLEAR : WT_DYN_FCAT , WA_FIELDCAT .
  ENDLOOP.
  """"""""""""""""""""""""""""""""""""""""""""field catalogue """"""""""""""""""""""""""""""""""""""
  CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
    EXPORTING
      I_STYLE_TABLE             = 'X'
      IT_FIELDCATALOG           = GT_DYN_FCAT
    IMPORTING
      EP_TABLE                  = GT_DYN_TABLE
    EXCEPTIONS
      GENERATE_SUBPOOL_DIR_FULL = 1
      OTHERS                    = 2.
  """"""""""""""""""""""""""""""""""""""""dynamic table creation""""""""""""""""""""""""""""""""""

  """""""""""""""""""""""""""""""""'header table creation """"""""""""""""""""""""""""""""""""""""

  LOOP AT  GT_DYN_FCAT INTO DATA(LS_FIELDS).
    CONCATENATE LS_FIELDS-FIELDNAME GS_FIELD_LIST INTO GS_FIELD_LIST SEPARATED BY SPACE.
    LS_COMPONENTS-NAME = LS_FIELDS-FIELDNAME.
    SELECT SINGLE ROLLNAME FROM DD03L INTO ( LS_FIELDS-ROLLNAME ) WHERE FIELDNAME = LS_FIELDS-FIELDNAME .
    IF LS_FIELDS-ROLLNAME IS INITIAL .
      DATATYPE ?= CL_ABAP_DATADESCR=>DESCRIBE_BY_DATA(  LS_FIELDS-FIELDNAME ).
      STR  = DATATYPE->ABSOLUTE_NAME.
      SPLIT STR AT '=' INTO STR1 STR2 .
      IF STR2 IS NOT INITIAL .
        LS_FIELDS-ROLLNAME = STR2 .
      ENDIF .
    ENDIF .
    LS_COMPONENTS-TYPE ?= CL_ABAP_ELEMDESCR=>DESCRIBE_BY_NAME( LS_FIELDS-ROLLNAME ) .
    APPEND LS_COMPONENTS TO GS_COMPONENTS.
    APPEND LS_FIELDS-SCRTEXT_M TO GIT_HEADER_TEXT.
  ENDLOOP.
  TRY .
      LR_STRUCT_RESULT = CL_ABAP_STRUCTDESCR=>CREATE( P_COMPONENTS = GS_COMPONENTS ).
      LR_TABLE_RESULT =  CL_ABAP_TABLEDESCR=>CREATE( P_LINE_TYPE  = LR_STRUCT_RESULT ).
    CATCH CX_SY_STRUCT_CREATION.    "
    CATCH CX_SY_TABLE_CREATION.
  ENDTRY.
  CREATE DATA: LRT_TABLE  TYPE HANDLE LR_TABLE_RESULT.
  CREATE DATA: LRS_STRUCT  TYPE HANDLE LR_STRUCT_RESULT.
  DATA LR_REF TYPE REF TO DATA .

  ASSIGN LRT_TABLE->* TO <FS_DYNAMIC_TAB>.
  ASSIGN LRS_STRUCT->* TO <WA_TAB1>.
  DATA : TBX TYPE I .
  DATA : FLAG TYPE C .
  FIELD-SYMBOLS : <VALUE> TYPE ANY .
  FIELD-SYMBOLS : <VALUE_FROM> TYPE ANY .
  """"""""""""""""""""""""""""""""""""filling the internal table """"""""""""""""""""""""""""""""""""""""""""""""""





  LOOP AT  IT_TAB .
    LOOP AT  GT_FIELDCAT INTO WA_FIELDCAT . """""""""""""""field catalog
      ASSIGN COMPONENT WA_FIELDCAT-FIELDNAME OF STRUCTURE
      IT_TAB TO <VALUE>.
      ASSIGN COMPONENT WA_FIELDCAT-FIELDNAME OF STRUCTURE
      <WA_TAB1> TO <VALUE_FROM>.
      MOVE <VALUE> TO <VALUE_FROM>.
    ENDLOOP.
    APPEND <WA_TAB1> TO <FS_DYNAMIC_TAB>.
  ENDLOOP.

""""""""""""""""""""""""""''this code is removed because i was testing above code  in following code i just data to work area 
      ASSIGN COMPONENT 'FIELDNAME' OF STRUCTURE <WA_TAB> TO <FIELD>.
      IF <FIELD> IS ASSIGNED.
        CONCATENATE 'IT_TAB-'<FIELD> INTO STR1 .
        CONCATENATE '<WA_TAB1>-'WA_FIELDCAT-FIELDNAME  INTO STR2 .
        CONDENSE STR2 .
        ASSIGN STR2 TO <VALUE>.
        ASSIGN COMPONENT <FIELD> OF STRUCTURE IT_TAB TO <VALUE>
       .
*        ASSIGN  <F_ST1>  TO <VALUE> .
        UNASSIGN <F_ST1>.
        CONDENSE V_VALUE .
*        IF ( tbx <> NO ).
*          CONCATENATE it_attach v_value  c_tab INTO
*          it_attach .
*        ELSE .
*          CONCATENATE it_attach v_value INTO it_attach ." it_string .
      ENDIF .




  CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
    EXPORTING
      FILENAME                = DEFAULT_FILE_PATH
      FILETYPE                = 'ASC'
      WRITE_FIELD_SEPARATOR   = 'X'
      FIELDNAMES              = GIT_HEADER_TEXT
    CHANGING
      DATA_TAB                = <FS_DYNAMIC_TAB>
    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
      NOT_SUPPORTED_BY_GUI    = 22
      ERROR_NO_GUI            = 23
      OTHERS                  = 24.

ENDFUNCTION.