Excel Download of Generic Internal Table Data

light111Excel download of  Generic Internal Table Data that contains any fields like GUID,CHAR,NUMBER, …


Sometimes you can download internal table data to an EXCEL File to a location. But i faced a challenge while downloading an internal table that contains GUID fields. When we see the excel the GUID field values are totally meshed up while other fields values such as CHAR/STRING/DATE/NUMBER are fine. 

Then i tried to download the table rows with GUID values from SE16N and found that the excel looks good. The GUIDs and other field values appear correctly. The point is while the table involves GUID values (RAW) in nature then if you download as ASCII file then the problem occurs. So in such a case first convert the table data into XSTRING and then convert this XSTRING data to BINARY and then download this BINAY formatted data as a BIN file with .XLSX format and then your excel looks really cool. 

The below code snippet:   it_data is a standard table and generic in nature 

Method Definition-

class-methods DOWNLOAD_EXCEL
importing IT_DATA type STANDARD TABLE .

Method Implementation-

METHOD DOWNLOAD_EXCEL.

DATA:
lv_length TYPE i,
lv_xml TYPE xstring,
lv_sname TYPE tabname, 

lv_file  TYPE string value ‘C:/MYDOWNLOAD.DATA.XLSX’ 
lt_fieldcatalog TYPE lvc_t_fcat,
lt_xml_stream TYPE xml_rawdata,
lr_result_data TYPE REF TO cl_salv_bs_result_data_table,
lr_data TYPE REF TO data.

* Get the structure name

lv_sname = CONV #( CAST cl_abap_structdescr( CAST cl_abap_tabledescr( cl_abap_tabledescr=>describe_by_data(it_data ) )->get_table_line_type( ) )->get_relative_name( ) ).

* Get the column header information
CALL FUNCTION ‘LVC_FIELDCATALOG_MERGE’
EXPORTING
i_structure_name = lv_sname
i_bypassing_buffer = ‘X’
CHANGING
ct_fieldcat = lt_fieldcatalog
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.


cl_salv_ex_util=>factory_result_data_table(
EXPORTING
r_data = ref  #( it_data )
t_fieldcatalog = lt_fieldcatalog
RECEIVING
r_result_data_table = lr_result_data ).

* Transform data to XSTRING
CALL METHOD cl_salv_bs_tt_util=>if_salv_bs_tt_util~transform
EXPORTING
xml_type = if_salv_bs_xml=>c_type_xlsx
xml_version = if_salv_bs_xml=>version_26
r_result_data = lr_result_data
xml_flavour = if_salv_bs_c_tt=>c_tt_xml_flavour_export
gui_type = if_salv_bs_xml=>c_gui_type_gui
IMPORTING
xml = lv_xml.

* Convert XSTRING to BINARY
CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
EXPORTING
buffer = lv_xml
IMPORTING
output_length = lv_length
TABLES
binary_tab = lt_xml_stream.

* Download Data with Binary format
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
bin_filesize = lv_length
filetype = ‘BIN’
filename = lv_file
CHANGING
data_tab = lt_xml_stream
EXCEPTIONS
OTHERS = 1.
ENDMETHOD.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s