![](/cvo_files/imgs_sap_abap/logos/logo_cvo_academy_it.png)
✒️ABAP Los archivos locales y el download de datos
ABAP Los archivos locales y el download de datos
![ABAP Los archivos locales y el download de datos ABAP Los archivos locales y el download de datos](https://www.cvosoft.com/cvo_files/imgs_sap_abap/og-images/198.jpg)
Data Recording in Local Files
To store data from internal tables in our programs into files located on the PC, we will utilize the GUI_DOWNLOAD method. Let's examine some of the parameters of the method:
- FILENAME: contains the directory and name of the file to be downloaded to the PC.
- FILETYPE: contains the type or extension of the file being downloaded to the PC. The supported types or extensions are:
-
BIN: for binary files. Specify the file's length in the BIN_FILESIZE parameter. The internal table used must have a single field of type X.
-
ASC: for ASCII files.
-
DAT: for Excel files. Columns are separated using tabs, and lines are separated with line breaks.
-
WK1: for Excel and Lotus files.
- WRITE_FIELD_SEPARATOR: indicates whether a file separator will be generated and, if so, what it will be.
- DATA_TAB: stores the file's content. This table must have the same structure as the file.
This method also defines exceptions with descriptions covering each possible error during method execution. Upon code execution, a message will confirm the successful data transfer and specify the transferred byte count.
In some ABAP programs, DOWNLOAD and WS_DOWNLOAD functions may be observed. However, it is not recommended to use these functions as they are currently considered obsolete by SAP.
In the dialog window that appears, select the 'Pattern for ABAP Objects' option if you want to generate the structure of a class method or the 'CALL FUNCTION' option if you want to generate the structure of a function method.
If you wish to generate the structure of a method from the CALL_GUI_FRONTEND_SERVICES class, after selecting 'Pattern for ABAP Objects' press continue. Then, while keeping the 'METHOD CALL' option selected, insert the name of the CALL_GUI_FRONTEND_SERVICES class into the 'Class Interface' field. Press F4 to search for the needed method within the method structure field.
E.g
Input_file
21567432A23071990Jaime Gomez
31987623I15061988Jorge Gomez
27982134A02121996Angelica Ramirez
*&---------------------------------------------------------------------*
*& Report ZTEST_ABAP_JEGA_22
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_abap_jega_22.
*--------------------------------------------------------------------*
* Data Declaration
*--------------------------------------------------------------------*
TYPES: BEGIN OF s_country_file,
country_name(20) TYPE c, "Country
country_language(20) TYPE c, "Language
END OF s_country_file.
DATA: gt_country_file TYPE STANDARD TABLE OF s_country_file,
gs_country_file LIKE LINE OF gt_country_file.
*--------------------------------------------------------------------*
* SCREEN SELECTION
*--------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK z1 WITH FRAME TITLE TEXT-001.
*Input file
PARAMETERS: p_path LIKE rlgrap-filename.
SELECTION-SCREEN END OF BLOCK z1.
*--------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.
*--------------------------------------------------------------------*
* Open the file selection window
PERFORM open_file_selection_window.
*--------------------------------------------------------------------*
START-OF-SELECTION.
*--------------------------------------------------------------------*
* Load the input file
PERFORM load_input_file.
* Save the output file
PERFORM save_output_file.
*&---------------------------------------------------------------------*
*& Form OPEN_FILE_SELECTION_WINDOW
*&---------------------------------------------------------------------*
* Open the file selection window and update the input path
*--------------------------------------------------------------------*
FORM open_file_selection_window.
DATA: lt_filetable TYPE filetable,
lw_file_table TYPE file_table,
lv_rc TYPE i.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
default_filename = '*'
CHANGING
rc = lv_rc
file_table = lt_filetable.
READ TABLE lt_filetable INDEX 1 INTO lw_file_table.
IF sy-subrc EQ 0.
MOVE lw_file_table TO p_path.
ENDIF.
ENDFORM. " OPEN_FILE_SELECTION_WINDOW
*&---------------------------------------------------------------------*
*& Form LOAD_INPUT_FILE
*&---------------------------------------------------------------------*
* Load the input file data from the specified path
*--------------------------------------------------------------------*
FORM load_input_file.
DATA: lv_file TYPE string.
IF p_path IS INITIAL.
MESSAGE s000(z_prueba) WITH TEXT-003.
ELSE.
lv_file = p_path.
* Open the input file for reading
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = lv_file
filetype = 'ASC'
has_field_separator = ''
CHANGING
data_tab = gt_country_file[]
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
not_supported_by_gui = 17
error_no_gui = 18
OTHERS = 19.
IF sy-subrc NE 0.
MESSAGE e000(z_prueba) WITH TEXT-004.
ENDIF.
IF gt_country_file[] IS INITIAL.
MESSAGE s000(z_prueba) WITH TEXT-005.
ENDIF.
ENDIF.
ENDFORM. " LOAD_INPUT_FILE
*&---------------------------------------------------------------------*
*& Form SAVE_OUTPUT_FILE
*&---------------------------------------------------------------------*
* Save the data to the local file using the specified path
*--------------------------------------------------------------------*
FORM save_output_file.
* Save the data to the local file
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
filename = 'C:UsersgatomDownloadsTestFolderoutput_file.txt'
filetype = 'ASC'
CHANGING
data_tab = gt_country_file[]
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 NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM. " SAVE_OUTPUT_FILE
 
 
 
Sobre el autor
Publicación académica de Jaime Eduardo Gomez Arango, en su ámbito de estudios para la Carrera Consultor ABAP.
Jaime Eduardo Gomez Arango
Profesión: Ingeniero de Sistemas y Computación - España - Legajo: SW34C
✒️Autor de: 149 Publicaciones Académicas
🎓Egresado de los módulos:
- Carrera Consultor en SAP Fiori
- Carrera Consultor ABAP Nivel Avanzado
- Carrera Consultor ABAP Nivel Inicial
Disponibilidad Laboral: FullTime
Presentación:
Ingeniero de sistemas y computación con 8 años de experiencia el desarrollo frontend & backend (react/node) y en cloud (aws), actualmente desarrollando habilidades en sap btp, ui5, abap y fiori.
Certificación Académica de Jaime Gomez