Technical Articles
Tip: How To Download An SAP Table in Excel Easily
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_vkey–col_no = sy–tabix.
t_vkey–col_name = comp_descr–NAME.
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 sy–subrc.
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.
Thanks Stefan,
Here is another way to do so.
****************************************************************
DATA: lt_but000 TYPE STANDARD TABLE OF but000.
TYPES: xmlline(1024) TYPE x.
DATA: lt_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
Hello Alejandro,
thank you very much for this cool tip. 🙂
Cheers
Stefan