File data conversion made easy using ABAP custom utility class zcl_filedata_convert
In the last post, I talked about how to read XLSX files into an internal table and convert internal to XLSX files. In this post, I wanted to share a custom utility class that can make tasks of data conversion as simple as a method call. The class is ZCL_FILEDATA_CONVERT.
Below are a few of the code snippets showing its capabilities.
Let's start with XLSX to the internal table.
REPORT syst-repid. | |
DATA: | |
lt_itab TYPE solix_tab, | |
lt_vbak TYPE STANDARD TABLE OF vbak. | |
PARAMETER: | |
p_file TYPE string DEFAULT `C:\Users\user\Desktop\VBAK.XLSX`. | |
START-OF-SELECTION. | |
cl_gui_frontend_services=>gui_upload( EXPORTING filename = p_file " Name of file | |
filetype = 'BIN' " File Type (ASCII, Binary) | |
CHANGING data_tab = lt_itab " Transfer table for file contents | |
EXCEPTIONS OTHERS = 1 | |
). | |
IF sy-subrc <> 0. | |
RETURN. | |
ENDIF. | |
new zcl_filedata_convert( )->xlsx_2_itab( EXPORTING it_solix = lt_itab | |
IMPORTING et_data = lt_vbak | |
). | |
cl_demo_output=>display( lt_vbak ). |
It automatically handles date and time fields for you.
Internal table to XLSX
REPORT syst-repid. | |
PARAMETER: | |
p_file TYPE string DEFAULT `C:\Users\user\Desktop\VBAK1.XLSX`. | |
START-OF-SELECTION. | |
SELECT * | |
FROM vbak | |
INTO TABLE @data(lt_vbak) | |
UP TO 100 ROWS. | |
IF syst-subrc <> 0. | |
RETURN. | |
ENDIF. | |
DATA(lv_xstring) = NEW zcl_filedata_convert( )->itab_2_xlsx( lt_vbak ). | |
DATA(t_xtab) = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_xstring ). | |
cl_gui_frontend_services=>gui_download( EXPORTING bin_filesize = xstrlen( lv_xstring ) " File length for binary files | |
filename = p_file " Name of file | |
filetype = 'BIN' " File type (ASCII, binary ...) | |
CHANGING data_tab = t_xtab " Transfer table | |
EXCEPTIONS OTHERS = 1 | |
). | |
IF sy-subrc <> 0. | |
RETURN. | |
ENDIF. |
similarly converting data from and to CSV or TSV or special separated file, It handles it all.
Here is a link to source : ZCL_FILE_DATA_CONVERT
Use it as it is or modify it to suit your needs.
As always, hope you like the post and use its information in your day to day work.