2022 Nov 16 6:21 AM
Hi,
I'm trying to use GUI_UPLOAD to read some data from a excel file, if file was opened , the Fm will return 12(UNKNOWN_DP_ERROR).
Unless, I close the file, it can get the file content normally.
Is there a way to get the contents of the file when it is opened?
" load file
cl_gui_frontend_services=>gui_upload(
exporting
filename = iv_full_path
filetype = 'BIN'
importing
filelength = lv_filesize
changing
data_tab = lt_bin_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
others = 17 ).
2024 May 06 10:03 AM
REPORT csv.
* Reading data from Microsoft CSV *
PARAMETERS: p_fname TYPE string OBLIGATORY,
tb_name TYPE tabname16 OBLIGATORY,
date_f(10) TYPE c MODIF ID abc DEFAULT 'MM/DD/YYYY', "date format
deli(1) TYPE c MODIF ID abc DEFAULT ';', "delimiter
dec_f TYPE string MODIF ID abc DEFAULT '1,234,567.89', "decimal format
w_h_line AS CHECKBOX MODIF ID abc DEFAULT 'X'. "with_header_line
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
IF screen-group1 = 'ABC'.
screen-input = 0.
ENDIF.
MODIFY SCREEN.
ENDLOOP.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.
PERFORM file_open_dialog CHANGING p_fname.
START-OF-SELECTION.
DATA: lf_line TYPE string.
DATA: lt_config TYPE mmpur_mgrp2pgrp_t,
gt_itab TYPE truxs_t_text_data,
ls_file_line LIKE LINE OF gt_itab,
lv_filename TYPE string,
lv_filename_usr TYPE localfile,
l_table_count TYPE i,
data_ref TYPE REF TO data.
DATA : d_entry TYPE REF TO data.
FIELD-SYMBOLS:
<t_entry> TYPE table,
<line> TYPE any.
CREATE DATA d_entry TYPE STANDARD TABLE OF (tb_name).
ASSIGN d_entry->* TO <t_entry>.
FIELD-SYMBOLS:
<line2> TYPE any .
CREATE DATA data_ref TYPE (tb_name).
ASSIGN data_ref->* TO <line2>.
DATA:tb_data TYPE STANDARD TABLE OF str_table,
gt_split TYPE TABLE OF string,
gt_variable TYPE TABLE OF string,
tb_header TYPE TABLE OF string,
it_data TYPE STANDARD TABLE OF data_ref,
count TYPE i,
index TYPE i VALUE 1,
header TYPE string,
value TYPE string.
DATA : variable TYPE string,
in_month TYPE string,
in_day TYPE string,
out_month(2),
out_day(2).
DATA : variable_tmp LIKE variable,
TYPE_of_value LIKE dd01v-datatype,
is_decimal(1) TYPE c,
is_negative(1) TYPE c.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = p_fname
filetype = 'ASC'
has_field_separator = deli
read_by_line = 'X'
TABLES
data_tab = gt_itab
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
OTHERS = 17.
IF sy-subrc NE 0.
RETURN.
ENDIF.
IF lines( gt_itab ) = 0 .
WRITE / ` Excel file is empty!`.
RETURN.
ENDIF.
LOOP AT gt_itab INTO ls_file_line.
lf_line = ls_file_line.
* split record into fields
IF sy-tabix = 1.
SPLIT ls_file_line AT deli INTO TABLE tb_header.
DESCRIBE TABLE tb_header LINES count.
ELSE.
"SPLIT ls_file_line AT deli INTO TABLE gt_split.
CALL FUNCTION 'RSDS_CONVERT_CSV'
EXPORTING
i_data_sep = deli
i_esc_char = '"'
i_record = lf_line
i_field_count = 9999
IMPORTING
e_t_data = gt_split
EXCEPTIONS
escape_no_close = 1
escape_improper = 2
conversion_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE 'Error importing file' TYPE 'E'.
ENDIF.
index = 1.
WHILE index <= count.
READ TABLE gt_split INDEX index TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
header = tb_header[ index ].
CONDENSE header.
value = gt_split[ index ].
CONDENSE value.
IF value IS NOT INITIAL.
TRY.
variable = CONV string( value ).
" check date format MM/DD/YYYY and conv it
FIND REGEX '^\d{1,2}\/\d{1,2}\/\d{4}$' IN variable .
IF sy-subrc = 0.
SPLIT variable AT '/' INTO TABLE gt_variable.
in_day = gt_variable[ 1 ].
in_month = gt_variable[ 2 ].
out_month = |{ in_month ALPHA = IN }|.
out_day = |{ in_day ALPHA = IN }|.
DATA(date_formated) = |{ gt_variable[ 3 ] }| & |{ out_day }| & |{ out_month }|.
value = CONV date( date_formated ).
ENDIF.
" check date format MM/DD/YYYY and conv it
"check value if is number
CLEAR : variable_tmp , TYPE_of_value , is_decimal.
variable_tmp = variable .
REPLACE ALL OCCURRENCES OF SUBSTRING ',' IN variable_tmp WITH space.
REPLACE ALL OCCURRENCES OF SUBSTRING '.' IN variable_tmp WITH space.
IF sy-subrc = 0.
is_decimal = 'X'.
ENDIF.
REPLACE ALL OCCURRENCES OF SUBSTRING '-' IN variable_tmp WITH space.
IF sy-subrc = 0.
is_negative = 'X'.
ENDIF.
CALL FUNCTION 'NUMERIC_CHECK'
EXPORTING
string_in = variable_tmp
IMPORTING
string_out = variable_tmp
htype = TYPE_of_value.
IF TYPE_of_value = 'NUMC'.
IF is_decimal = 'X'.
REPLACE ALL OCCURRENCES OF SUBSTRING ',' IN value WITH space.
IF is_negative <> 'X'.
REPLACE ALL OCCURRENCES OF SUBSTRING '.' IN value WITH ','.
ENDIF.
ELSE.
REPLACE ALL OCCURRENCES OF SUBSTRING ',' IN value WITH space.
ENDIF.
ENDIF.
"check value if is number
<line2>-(header) = value.
IF <line2>-(header) <> value.
WRITE / ` ERROR : line` && ` => ` && index && ` column` && ` => ` && header.
WRITE / ` Error during registration of characteristic ` && value && ` to data element ` && <line2>-(header) .
RETURN.
ENDIF.
CATCH cx_root.
WRITE / ` ERROR : line` && ` => ` && index && ` column` && ` => ` && header.
WRITE / ` ERROR : Incompatible data element`.
RETURN.
ENDTRY.
ENDIF.
ENDIF.
index = index + 1.
ENDWHILE.
APPEND INITIAL LINE TO <t_entry> ASSIGNING FIELD-SYMBOL(<line1>).
<line1> = CORRESPONDING #( <line2> ).
CLEAR : <line2>.
ENDIF.
ENDLOOP.
LOOP AT <t_entry> ASSIGNING <line>.
INSERT (tb_name) FROM <line>.
IF sy-subrc = 0.
l_table_count = l_table_count + 1 .
ENDIF.
ENDLOOP.
COMMIT WORK AND WAIT.
WRITE / ' Query OK, ' && l_table_count && ' rows affected' .
FORM file_open_dialog CHANGING cp_fname.
DATA: lt_filetable TYPE filetable,
lv_rc TYPE i.
cl_gui_frontend_services=>file_open_dialog(
EXPORTING
default_extension = 'csv'
file_filter = '(*.csv)|*.csv|'
multiselection = abap_false
CHANGING
file_table = lt_filetable
rc = lv_rc
EXCEPTIONS
OTHERS = 1 ).
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
TRY.
cp_fname = lt_filetable[ 1 ]-filename.
CATCH cx_root.
ENDTRY.
ENDFORM.
"Reading data from Microsoft CSV *