Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
arnabdatta3
Participant
14,598
Hi folks,

I know this is a very old topic, but I didn't find easy solution for this. My solution is simple to output the internal table data to XLSX file. Just choose the file name by F4 help, make sure you give the extension 'xlsx'.

The solution is based on the built in functions of cl_salv_table class. I hope this will help you. Please let me know your comments and suggestions. Please ignore the naming conventions and untidiness of the code. 🙂

 

Cheers! Happy coding! 🙂

 

Code:
*&---------------------------------------------------------------------*
*& Report ZARNAB_XLSX
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zarnab_xlsx.

TYPES:
BEGIN OF ty_s_cline,
line TYPE c LENGTH 1024, " Line of type Character
END OF ty_s_cline .

DATA: gr_table TYPE REF TO cl_salv_table, " Basis Class for Simple Tables
gt_outtab TYPE STANDARD TABLE OF sflight, " Flight
lr_functions TYPE REF TO cl_salv_functions_list, " Generic and User-Defined Functions in List-Type Tables
lr_layout TYPE REF TO cl_salv_layout, " Settings for Layout
lv_xml_type TYPE salv_bs_constant,
lv_xml TYPE xstring,
gt_srctab TYPE STANDARD TABLE OF ty_s_cline,
gt_len TYPE i, " Len of type Integers
lv_file_xlsx TYPE string,
lv_file TYPE localfile, " Local file for upload/download
ls_key TYPE salv_s_layout_key, " Layout Key
lr_content TYPE REF TO cl_salv_form_element. " General Element in Design Object

PARAMETERS: p_file TYPE localfile. " Local file for upload/download

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

CALL FUNCTION 'F4_FILENAME'
IMPORTING
file_name = lv_file.

p_file = lv_file.
lv_file_xlsx = lv_file.

START-OF-SELECTION.

SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_outtab UP TO 100 ROWS.

TRY.

cl_salv_table=>factory(
IMPORTING
r_salv_table = gr_table
CHANGING
t_table = gt_outtab ).

CATCH cx_salv_msg. "#EC NO_HANDLER

ENDTRY.

lr_functions = gr_table->get_functions( ).

lr_functions->set_all( abap_true ).

lr_layout = gr_table->get_layout( ).

ls_key-report = sy-repid.

lr_layout->set_key( ls_key ).

lr_layout->set_save_restriction( if_salv_c_layout=>restrict_user_independant ).

gr_table->set_top_of_list( lr_content ).

gr_table->set_end_of_list( lr_content ).

* gr_table->display( ).

lv_xml_type = if_salv_bs_xml=>c_type_xlsx. "if_salv_bs_xml=>c_type_mhtml.

lv_xml = gr_table->to_xml( xml_type = lv_xml_type ).

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_xml
* APPEND_TO_TABLE = ' '
IMPORTING
output_length = gt_len
TABLES
binary_tab = gt_srctab.

CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
bin_filesize = gt_len
filename = lv_file_xlsx
filetype = 'BIN'
CHANGING
data_tab = gt_srctab
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
not_supported_by_gui = 22
error_no_gui = 23
OTHERS = 24.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ELSE. " ELSE -> IF sy-subrc <> 0

MESSAGE 'Excel file downloaded!' TYPE 'S'.

ENDIF. " IF sy-subrc <> 0
8 Comments
abo
Active Contributor
If you just want to download the table with no fancy tricks, this is indeed the way to go.

There is also a blogpost by Łukasz Pęgiel
arnabdatta3
Participant
Thanks Andrea!
Juwin
Active Contributor
shais
Participant
Long time (less than a month, I guess) since the last post regarding downloading to Excel in ABAP...
arnabdatta3
Participant
0 Kudos
I think the solution is simpler and will help some people. Thanks for your comment, which helped a lot of people.
martin_schrode
Explorer
0 Kudos

Thank you very much for this report. It works as expected. One question, though: It currently displays field labels instead of field names. Is there a way to change this?

Jaime-Andrade
Explorer
0 Kudos

Hi arnabdatta3,

Could you help me with a doubt . I am testing your code and help me with a problem that I have .

But I need suppress the first line of column heading before download. How could I Do this?

Thanks for your help.

oleg_sukharenko
Explorer
0 Kudos

Straightforward and helpful code.

Thank you!

Labels in this area