Skip to Content

Hello community,

the function group TXXL (Interface to Excel list viewer), in the package SGRP (Graphic ABAP Development), is an oldie – but an an goldie.

Here a tiny function module to export any SAP table in Excel with the FM XXL_SIMPLE_API, all you must know is the name of the table – and a where and sort condition, if you want. The execution of this FM opens Excel instance and shows the SAP table in Excel.

Function Z_GET_TABLE_AS_EXCEL.

*”———————————————————————-

*”*”Lokale Schnittstelle:

*”  IMPORTING

*”     VALUE(I_TABLENAME) TYPE  DD02L-TABNAME

*”     VALUE(I_OPTIONS) TYPE  CHAR1024 DEFAULT SPACE

*”     VALUE(I_SORT) TYPE  CHAR1024 DEFAULT SPACE

*”  EXCEPTIONS

*”      DIM_MISMATCH_DATA

*”      FILE_OPEN_ERROR

*”      FILE_WRITE_ERROR

*”      INV_WINSYS

*”      INV_XXL

*”———————————————————————-

   “-Variables———————————————————–

     Data dref Type Ref To Data.

     Data tabledescr_ref Type Ref To cl_abap_tabledescr.

     Data descr_ref Type Ref To cl_abap_structdescr.

     Data comp_descr Type abap_compdescr.

     Field-Symbols <fs> Type Standard Table.

     Data t_vkey Type Table Of gxxlt_v With Header Line.

     Data t_online Type Table Of gxxlt_o.

     Data t_print Type Table Of gxxlt_p.

   “-Main—————————————————————-

     Create Data dref Type Standard Table Of (I_TABLENAME).

     Assign dref->* To <fs>.

     tabledescr_ref ?= cl_abap_typedescr=>describe_by_data( <fs> ).

     descr_ref ?= tabledescr_ref->get_table_line_type( ).

     Select * From (I_TABLENAME) Into Table <fs> Where (I_OPTIONS)

       Order By (I_SORT).

     Loop At descr_ref->components Into comp_descr.

       t_vkeycol_no = sytabix.

       t_vkeycol_name = comp_descrNAME.

       Append t_vkey.

     EndLoop.

     Call Function ‘XXL_SIMPLE_API’

       Tables

         COL_TEXT = t_vkey

         DATA = <fs>

         ONLINE_TEXT = t_online

         PRINT_TEXT = t_print

       Exceptions

         DIM_MISMATCH_DATA = 1

         FILE_OPEN_ERROR = 2

         FILE_WRITE_ERROR = 3

         INV_WINSYS = 4

         INV_XXL = 5

         Others = 6.

     Case sysubrc.

       When 1. Raise DIM_MISMATCH_DATA.

       When 2. Raise FILE_OPEN_ERROR.

       When 3. Raise FILE_WRITE_ERROR.

       When 4. Raise INV_WINSYS.

       When 5. Raise INV_XXL.

     EndCase.

EndFunction.

“-End——————————————————————-

Enjoy the 41 lines of code.

Cheers

Stefan

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

  1. Manuel Alejandro Ramírez Rodríguez

    Thanks Stefan,

    Here is another way to do so.


    ****************************************************************

    DATAlt_but000 TYPE STANDARD TABLE OF but000.
    TYPESxmlline(1024) TYPE x.
    DATAlt_xml TYPE STANDARD TABLE OF xmlline.

    SELECT * FROM but000 INTO TABLE lt_but000 UP TO 1000 ROWS.
    CALL TRANSFORMATION id  SOURCE data_node = lt_but000  RESULT XML lt_xml.
    CALL METHOD cl_gui_frontend_services=>gui_download
       EXPORTING
         filename = ‘C:\SAP\table_but000.xml’
         filetype = ‘BIN’
       CHANGING
         data_tab = lt_xml.

    ****************************************************************


    Regards

    (0) 

Leave a Reply