Skip to Content
Technical Articles
Author's profile photo Kallol Chakraborty

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

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. 🙂

 

N.B:  MS_EXCEL_OLE_STANDARD_DAT is an internal function module that is not released.

 

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

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

Assigned tags

      4 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sercan Kucukdemirci
      Sercan Kucukdemirci

      Ah finally another excel thread, i am going to grab my popcorn ! 🙂

      Author's profile photo Vicenç Xavier Lozano
      Vicenç Xavier Lozano

      You are using a non released SAP function module.

      No one can confirm it will be in your system tomorrow. Or maybe it will be there, but it will be used to create popcorn for Sercan.

      I will not press the "like" button, nor I will share it. Nothing LinkedIn related too, sorry.

      But I commented it. And I will suggest an improvement to the entry: put a warning on it about the non-released status of the function module.

      https://github.com/sapmentors/abap2xlsx is the way to go (in my not-so-humble opinion), sorry.

      Author's profile photo Kallol Chakraborty
      Kallol Chakraborty
      Blog Post Author

      Yes, MS_EXCEL_OLE_STANDARD_DAT is an internal function module that is not released but it serves the purpose(basic output) if you have it in your system that's why is post is written.

      The blog post is updated now. Thanks.

       

      Author's profile photo Paul Hardy
      Paul Hardy

      I would like the second the motion that ABAP2XLSX is the way to go here. The OLE technique takes a very long time.

      As background the reason for the first comment on this blog is that every single month for the last ten years someone has posted a blog about how to upload/download from Excel to ABAP and most of the authors have not heard of ABAP2XLSX.

      A common argument against ABAP2XLSX is "my company does not allow open source ABAP products" despite allowing the use of the internet, and smartphones, and cars, all of which are crawling with open source software.

      The other common argument is that ABAP2XLSX is far too complicated to use, and I don't agree with that either.