Skip to content

Instantly share code, notes, and snippets.

@viksingh
Last active Aug 29, 2015
Embed
What would you like to do?
CSV_Trial
REPORT ZCSV.
DATA:
gt_excel_data TYPE TRUXS_T_TEXT_DATA.
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.
PARAMETERS: p_file TYPE localfile OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b2 .
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
DATA: lt_files TYPE filetable,
lv_count TYPE i,
lw_file TYPE file_table,
lo_gui TYPE REF TO cl_gui_frontend_services.
CREATE OBJECT lo_gui.
lo_gui->file_open_dialog(
EXPORTING
initial_directory = 'C:\'
file_filter = '*'
CHANGING
file_table = lt_files
rc = lv_count ).
IF lv_count <> 0. "there should only be one record in this case
LOOP AT lt_files INTO lw_file.
p_file = lw_file-filename.
EXIT.
ENDLOOP.
ENDIF.
START-OF-SELECTION.
************************************************************************
*-------* Read file contents ------------------------------------------*
************************************************************************
PERFORM read_file USING p_file
CHANGING gt_excel_data.
FORM read_file USING p_file TYPE localfile
CHANGING c_excel_data TYPE TRUXS_T_TEXT_DATA.
DATA : lw_struct TYPE ztest_struc,
lt_tab type standard table of ztest_struc.
DATA: lt_file TYPE truxs_t_text_data,
ls_file LIKE LINE OF lt_file.
DATA: lv_path TYPE string,
lv_data TYPE string.
*
lv_path = p_file.
DATA: lo_csv TYPE REF TO cl_rsda_csv_converter.
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = lv_path
filetype = 'ASC'
has_field_separator = abap_true
CHANGING
data_tab = c_excel_data
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 <> 0.
* Display error message "Error reading file."
MESSAGE i347(zz).
LEAVE LIST-PROCESSING.
ENDIF.
* Get fields from CSV file
CALL FUNCTION 'TEXT_CONVERT_CSV_TO_SAP'
EXPORTING
I_FIELD_SEPERATOR = ','
i_line_header = ' '
i_tab_raw_data = c_excel_data
TABLES
i_tab_converted_data = lt_tab
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc NE 0.
refresh lt_tab.
ENDIF.
loop at lt_tab into lw_struct.
WRITE:/ lw_struct-test1,
lw_struct-test2,
lw_struct-test3.
endloop.
* CALL METHOD cl_rsda_csv_converter=>create
** EXPORTING
** i_delimiter = C_DEFAULT_DELIMITER
** i_separator = C_DEFAULT_SEPARATOR
* RECEIVING
* r_r_conv = lo_csv
.
* LOOP AT c_excel_data INTO lv_data.
*
* CALL METHOD lo_csv->csv_to_structure
* EXPORTING
* i_data = lv_data
* IMPORTING
* e_s_data = lw_struct.
*
* WRITE:/ lw_struct-test1,
* lw_struct-test2,
* lw_struct-test3.
*
* ENDLOOP.
ENDFORM. "read_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment