Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

using cl_gui_frontend_services=>file_save_dialog to save internal table to xlsx file.

danielpray805
Explorer
0 Likes
9,442

Hi,

I'm currently working on saving my internal table to a local excel file.

Assume my internal table is 'IT_FINAL' and here are the codes.

DATA: GT_FIELDS TYPE TABLE OF STRING,
 GS_FIELDS TYPE STRING.
DATA: FILENAME TYPE STRING,
 PATH TYPE STRING,
 FULLPATH TYPE STRING.
*header variables
GS_FIELDS = 'a, b, c, d, e, f, g, h, i, j'.
APPEND GS_FIELDS TO GT_FIELDS.

     CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG
 EXPORTING
 WINDOW_TITLE = 'Save'
 FILE_FILTER = 'EXCEL FILES (*.XLS)|*.XLS|EXCEL FILES (*.XLSX)|*.XLSX|'
 INITIAL_DIRECTORY = 'C:\'
 CHANGING
 FILENAME = FILENAME
 PATH = PATH
 FULLPATH = FULLPATH.

* Download to Excel
 CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
 EXPORTING
 FILENAME = FULLPATH
 WRITE_FIELD_SEPARATOR = ABAP_TRUE
 APPEND = 'X'
 FIELDNAMES = GT_FIELDS
 CHANGING
 DATA_TAB = IT_FINAL
 EXCEPTIONS
 OTHERS = 1.

I believe this should work fine, but I got a blank excel sheet somehow. I've tried SAP_CONVERT_TO_XLS_FORMAT FM as well, but all these don't seem to work for me. Any suggestion would be appreciated.

Thanks.

1 ACCEPTED SOLUTION
Read only

abo
Active Contributor
6,881

There's a nice blogpost by jagdish.patil which explains how to do it using standard classes. I have reworked his example because it had issues on an older system.

Another option, if you need something fancier, is to use abap2xlsx.

2 REPLIES 2
Read only

abo
Active Contributor
6,882

There's a nice blogpost by jagdish.patil which explains how to do it using standard classes. I have reworked his example because it had issues on an older system.

Another option, if you need something fancier, is to use abap2xlsx.

Read only

Sandra_Rossi
Active Contributor
6,881

There's no relationship between filtering the list of files which have the extension .XLS or .XLSX shown in the Windows dialog of FILE_SAVE_DIALOG and creating an Excel file in format XLS or XLSX.

Concerning the method GUI_DOWNLOAD, the parameter FIELDNAMES doesn't exist.

GUI_DOWNLOAD can only download an internal table if you indicate a specific FILETYPE value like 'DAT' and the internal table will be written to a text file with tab-separated fields (that Excel displays in columns). Concerning your code, GUI_DOWNLOAD probably returns an exception, but you can't know which one if you only indicate OTHERS (alone, it intercepts all without knowing which one happens).

I advise to search the Web for examples how to create an XLSX file (abap2xlsx, ALV utility classes, etc.)