Tuesday, 08 June 2021 13:15

Easy way to download internal table data to an Excel file in the Presentation Server

Written by Kallol Chakraborty
Rate this item
(0 votes)
Source https://blogs.sap.com/2021/06/09/easy-way-to-download-internal-table-data-to-an-excel-file-in-the-presentation-server/
“© 2020. SAP SE or an SAP affiliate company. All rights reserved.” “Used with permission of SAP SE”

Introduction

In certain cases, you need to download internal table data in Excel in the Presentation Server. I have tried different FMs but they were not fulfilling my requirements properly. Some of them are complex to implement also. So, I am writing this blog post describing an easy way.

The code mentioned below can be simply copied & changing the internal table & the download path will help to achieve the output in an Excel file in the Presentation Server.

Solution

Please follow the below steps

  1. Create a type for the Column names of the internal table.
     TYPES : BEGIN OF ty_struct, col_name(30), END OF ty_struct, ty_columns TYPE STANDARD TABLE OF ty_struct WITH EMPTY KEY.​
  2. Declare the necessary internal tables.
    DATA : lt_columns TYPE ty_struct, lt_temp TYPE dfies_tab.
  3. Fetch the column names of the DDIC structure using the FM: ‘DDIF_FIELDINFO_GET’. If you are not using a DDIC structure then you will have to populate the headers explicitly. For populating headers explicitly please check this Link.
    TRY. CALL FUNCTION 'DDIF_FIELDINFO_GET' EXPORTING tabname = 'ZSTRUCTURE_NAME' langu = sy-langu TABLES dfies_tab = lt_temp EXCEPTIONS not_found = 1 internal_error = 2 OTHERS = 3. IF sy-subrc = 0. lt_columns = CORRESPONDING #( lt_temp MAPPING col_name = fieldtext ). ENDIF. CATCH cx_root. "Please write your own exception handling code here ENDTRY.​​
  4. Call the FM: ‘MS_EXCEL_OLE_STANDARD_DAT’. Pass both the tables namely DATA table  COLUMN NAME (lt_columns) table & then the file download path.
    IF gt_data_table IS NOT INITIAL. CALL FUNCTION 'MS_EXCEL_OLE_STANDARD_DAT' EXPORTING file_name = p_download TABLES data_tab = gt_data_table fieldnames = lt_columns EXCEPTIONS file_not_exist = 1 filename_expected = 2 communication_error = 3 ole_object_method_error = 4 ole_object_property_error = 5 invalid_pivot_fields = 7 download_problem = 8 OTHERS = 9. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ELSE. MESSAGE |File downloaded successfully| TYPE 'S'. ENDIF. ENDIF. ​
  5. That’s it. ?

If you like this blog post, please like, share & comment.

Also, please don’t forget to endorse me on LinkedIn. ?

Continue reading here
Read 61 times

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.