Situation: Sometimes, we need to upload/ download customer config table to other system.
Now this is the sample ABAP code .
FIELD-SYMBOLS:
<fs_tab_fil> TYPE STANDARD TABLE,
<fs_tab_db> TYPE STANDARD TABLE.
PARAMETERS upload RADIOBUTTON GROUP g1 .
PARAMETERS download RADIOBUTTON GROUP g1 .
PARAMETERS filepath TYPE string.
PARAMETERS table TYPE string LOWER CASE.
DATA:
answer TYPE char1,
litab_tb TYPE REF TO data,
litab_tb2 TYPE REF TO data,
lc_tab_fst TYPE char1.
CREATE DATA litab_tb TYPE STANDARD TABLE OF (table) WITH NON-UNIQUE DEFAULT KEY.
CREATE DATA litab_tb2 TYPE STANDARD TABLE OF (table) WITH NON-UNIQUE DEFAULT KEY.
ASSIGN litab_tb->* TO <fs_tab_fil>.
ASSIGN litab_tb2->* TO <fs_tab_db>.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR filepath.
DATA gt_tab TYPE STANDARD TABLE OF file_table .
DATA li TYPE i.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
CHANGING
file_table = gt_tab
rc = li.
IF sy–subrc <> 0.
MESSAGE ‘open file path error.’ TYPE ‘E’.
ELSE.
READ TABLE gt_tab INTO filepath INDEX 1.
ENDIF.
AT SELECTION-SCREEN.
IF filepath IS INITIAL.
SET CURSOR FIELD ‘FILEPATH’.
MESSAGE ‘please input the required FILEPATH’ TYPE ‘E’.
ENDIF.
IF table IS INITIAL.
SET CURSOR FIELD ‘TABLE’.
MESSAGE ‘please input the required TABLE’ TYPE ‘E’.
ENDIF.
IF upload IS NOT INITIAL.
lc_tab_fst = table+0(1).
IF lc_tab_fst <> ‘Z’
AND lc_tab_fst <> ‘Y’.
CALL FUNCTION ‘POPUP_TO_CONFIRM’
EXPORTING
text_question = ‘OPERATE SAP SYSTEM TABLE? IF NOT,KILLE THIS PROCESS’(a02)
IMPORTING
answer = answer
EXCEPTIONS
text_not_found = 1
OTHERS = 2.
IF answer <> ‘1’.
MESSAGE ‘OPERAT CANCELED’ TYPE ‘E’.
LEAVE LIST-PROCESSING.
ENDIF.
ENDIF.
ENDIF.
SELECT COUNT(*)
FROM dd02l
WHERE tabname = table
AND as4local = ‘A’.
IF sy–subrc <> 0.
MESSAGE ‘TABLE IS NOT EXITED.’ TYPE ‘E’.
ENDIF.
START-OF-SELECTION.
* UPLOAD
CONCATENATE filepath ‘\’ table ‘.TXT’ INTO filepath.
IF upload IS NOT INITIAL.
SELECT *
INTO TABLE <fs_tab_db>
FROM (table).
“+++++++++++++++++++++++++++++++++beg
TYPE-POOLS:truxs.
DATA gitab_file TYPE truxs_t_text_data.
CALL FUNCTION ‘GUI_UPLOAD’
EXPORTING
filename = filepath
“has_field_separator = ‘X’
dat_mode = ‘X’
TABLES
data_tab = gitab_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
OTHERS = 17.
IF sy–subrc <> 0 .
MESSAGE ‘UPLOAD failed’ TYPE ‘S’ DISPLAY LIKE ‘E’.
LEAVE LIST-PROCESSING.
ELSE.
DELETE gitab_file INDEX 1.
DELETE gitab_file INDEX 1.
DELETE gitab_file INDEX 1.
ENDIF.
CALL FUNCTION ‘GUI_DOWNLOAD’
EXPORTING
filename = filepath
write_field_separator = ‘X’
dat_mode = ‘X’
“TRUNC_TRAILING_BLANKS = ‘X’
TABLES
data_tab = gitab_file
“data_tab = <fs_tab_db>
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
OTHERS = 22.
IF sy–subrc <> 0.
MESSAGE ‘download failed’ TYPE ‘S’ DISPLAY LIKE ‘E’.
LEAVE LIST-PROCESSING.
ENDIF.
“+++++++++++++++++++++++++++++++END
CALL FUNCTION ‘GUI_UPLOAD’
EXPORTING
filename = filepath
has_field_separator = ‘X’
dat_mode = ‘X’
TABLES
data_tab = <fs_tab_fil>
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.
APPEND LINES OF <fs_tab_fil> TO <fs_tab_db>.
DELETE ADJACENT DUPLICATES FROM <fs_tab_db> COMPARING ALL FIELDS.
MODIFY (table) FROM TABLE <fs_tab_db>.
IF sy–subrc <> 0.
MESSAGE ‘MODIFY FAILED’ TYPE ‘S’ DISPLAY LIKE ‘E’.
LEAVE LIST-PROCESSING.
ELSE.
MESSAGE ‘MODIFY SUCCESSFUL’ TYPE ‘S’.
ENDIF.
* download
ELSE.
“+++++++++++++++++++++++++++++++++beg
DATA lt_title TYPE STANDARD TABLE OF dfies.
DATA wa_tmp LIKE LINE OF gitab_file.
DATA wa_line1 LIKE LINE OF gitab_file.
DATA wa_line2 LIKE LINE OF gitab_file.
DATA wa_line3 LIKE LINE OF gitab_file. “JAPANESE
DATA wa_title TYPE dfies.
DATA ls_name TYPE ddobjname.
ls_name = table.
CALL FUNCTION ‘DDIF_FIELDINFO_GET’
EXPORTING
tabname = ls_name
langu = ‘E’
TABLES
dfies_tab = lt_title
* FIXED_VALUES =
EXCEPTIONS
not_found = 1
internal_error = 2
OTHERS = 3.
IF sy–subrc <> 0.
MESSAGE ID sy–msgid TYPE sy–msgty NUMBER sy–msgno
WITH sy–msgv1 sy–msgv2 sy–msgv3 sy–msgv4.
ENDIF.
LOOP AT lt_title INTO wa_title.
CONCATENATE wa_title–fieldname cl_abap_char_utilities=>horizontal_tab INTO wa_tmp.
CONCATENATE wa_line1 wa_tmp INTO wa_line1.
CONCATENATE wa_title–fieldtext cl_abap_char_utilities=>horizontal_tab INTO wa_tmp.
CONCATENATE wa_line2 wa_tmp INTO wa_line2.
ENDLOOP.
* JAPANESE
CLEAR lt_title.
CALL FUNCTION ‘DDIF_FIELDINFO_GET’
EXPORTING
tabname = ls_name
langu = ‘J’
TABLES
dfies_tab = lt_title
* FIXED_VALUES =
EXCEPTIONS
not_found = 1
internal_error = 2
OTHERS = 3.
IF sy–subrc <> 0.
MESSAGE ID sy–msgid TYPE sy–msgty NUMBER sy–msgno
WITH sy–msgv1 sy–msgv2 sy–msgv3 sy–msgv4.
ENDIF.
LOOP AT lt_title INTO wa_title.
CONCATENATE wa_title–scrtext_l cl_abap_char_utilities=>horizontal_tab INTO wa_tmp.
CONCATENATE wa_line3 wa_tmp INTO wa_line3.
ENDLOOP.
“+++++++++++++++++++++++++++++++++end
SELECT *
INTO TABLE <fs_tab_db>
FROM (table).
CALL FUNCTION ‘GUI_DOWNLOAD’
EXPORTING
filename = filepath
write_field_separator = ‘X’
dat_mode = ‘X’
“TRUNC_TRAILING_BLANKS = ‘X’
TABLES
data_tab = <fs_tab_db>
“data_tab = <fs_tab_db>
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
OTHERS = 22.
IF sy–subrc <> 0.
MESSAGE ‘download failed’ TYPE ‘S’ DISPLAY LIKE ‘E’.
LEAVE LIST-PROCESSING.
ENDIF.
“+++++++++++++++++++++++++++BEG
CALL FUNCTION ‘GUI_UPLOAD’
EXPORTING
filename = filepath
“has_field_separator = ‘X’
dat_mode = ‘X’
TABLES
data_tab = gitab_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
OTHERS = 17.
IF sy–subrc <> 0 .
MESSAGE ‘UPLOAD failed’ TYPE ‘S’ DISPLAY LIKE ‘E’.
LEAVE LIST-PROCESSING.
ENDIF.
INSERT wa_line1 INTO gitab_file INDEX 1.
INSERT wa_line2 INTO gitab_file INDEX 1.
INSERT wa_line3 INTO gitab_file INDEX 1.
CALL FUNCTION ‘GUI_DOWNLOAD’
EXPORTING
filename = filepath
write_field_separator = ‘X’
dat_mode = ‘X’
filetype = ‘DAT’
“TRUNC_TRAILING_BLANKS = ‘X’
TABLES
data_tab = gitab_file
“data_tab = <fs_tab_db>
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
OTHERS = 22.
IF sy–subrc <> 0.
MESSAGE ‘download failed’ TYPE ‘S’ DISPLAY LIKE ‘E’.
LEAVE LIST-PROCESSING.
ENDIF.
“+++++++++++++++++++++++++++END
ENDIF.
This post is come from www.hot583.com, you can share/use it with the post original link for free.
But pay attention of any risk yourself.
微信原文《The End》
I really like your writing style..Its so easily understandable. You can visit my blog and then maybe you can share your thought about mine.