*&---------------------------------------------------------------------*
*& Report Z_FORMA_UPLOAD_TRANSFER
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT z_forma_upload_transfer.
DATA: p_file_aux TYPE string.
TYPES: BEGIN OF s_file,
* bldat(8),
* budat(8),
* bwartwa(3),
* werks_d(4),
* lgort_d(4),
* matnr(18),
* erfmg(10),
kunnr(16),
name1(35),
END OF s_file.
DATA: BEGIN OF st_file,
* bldat(8),
* budat(8),
* bwartwa(3),
* werks_d(4),
* lgort_d(4),
* matnr(18),
* erfmg(10),
kunnr(16),
name1(35),
END OF st_file.
DATA: t_file TYPE s_file OCCURS 0,
wa_file TYPE s_file.
PARAMETERS: p_file LIKE rlgrap-filename,
p_fserv LIKE rlgrap-filename DEFAULT '.\teste_upload.txt'.
PARAMETERS: p_up RADIOBUTTON GROUP rad1,
p_dow RADIOBUTTON GROUP rad1.
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
*&---------------------------------------------------------------------*
PERFORM f4_filename.
*&---------------------------------------------------------------------*
START-OF-SELECTION.
*&---------------------------------------------------------------------*
IF p_up = 'X'.
PERFORM upload_file.
PERFORM transfer_to_server TABLES t_file.
ELSEIF p_dow = 'X'.
PERFORM download_from_server TABLES t_file.
perform escreve_ficheiro.
ENDIF.
*&---------------------------------------------------------------------*
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form f4_filename
*&---------------------------------------------------------------------*
FORM f4_filename .
CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = syst-cprog
dynpro_number = syst-dynnr
field_name = 'P_FILE'
IMPORTING
file_name = p_file.
ENDFORM. " f4_filename
*&---------------------------------------------------------------------*
*& Form upload_file
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_T_FILE text
* -->P_P_FILE text
*----------------------------------------------------------------------*
FORM upload_file.
p_file_aux = p_file.
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = p_file_aux
filetype = 'ASC'
has_field_separator = 'X'
CHANGING
data_tab = t_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 <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM. " upload_file
*&---------------------------------------------------------------------*
*& Form transfer_to_server
*&---------------------------------------------------------------------*
FORM transfer_to_server TABLES p_t_file STRUCTURE st_file.
* DELETE t_file INDEX 1.
OPEN DATASET p_fserv FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LOOP AT t_file INTO wa_file.
TRANSFER wa_file TO p_fserv.
ENDLOOP.
CLOSE DATASET p_fserv.
ENDFORM. " transfer_to_server
*&---------------------------------------------------------------------*
*& Form DOWNLOAD_FROM_sERVER
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_T_FILE text
*----------------------------------------------------------------------*
FORM download_from_server TABLES p_t_file STRUCTURE st_file.
OPEN DATASET p_fserv FOR INPUT IN TEXT MODE ENCODING DEFAULT.
DO.
READ DATASET p_fserv INTO wa_file.
IF sy-subrc = 0.
APPEND wa_file TO t_file.
ELSE.
EXIT.
ENDIF.
ENDDO.
CLOSE DATASET p_fserv.
ENDFORM. " DOWNLOAD_FROM_sERVER
*&---------------------------------------------------------------------*
*& Form escreve_ficheiro
*&---------------------------------------------------------------------*
form escreve_ficheiro .
loop at t_file into wa_file.
write:/ wa_file.
endloop.
endform. " escreve_ficheiro
Mostrar mensagens com a etiqueta upload. Mostrar todas as mensagens
Mostrar mensagens com a etiqueta upload. Mostrar todas as mensagens
quinta-feira, 6 de maio de 2010
segunda-feira, 22 de março de 2010
Upload txt/excel files to internal table (example)
DATA: file_path TYPE string.
DATA: len TYPE i.
file_path = l_file.
TRANSLATE file_path TO UPPER CASE.
len = STRLEN( file_path ) - 3.
CLEAR tabi.
IF file_path+len(3) = 'XLS'.
break slopes.
DATA t_excel TYPE alsmex_tabline OCCURS 0 WITH HEADER LINE.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = l_file
i_begin_col = 1
i_begin_row = 1
i_end_col = 2
i_end_row = 50
TABLES
intern = t_excel
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
LOOP AT t_excel.
CASE t_excel-col.
WHEN 1.
s_tabi-matnr = t_excel-value.
WHEN 2.
s_tabi-erfmg = t_excel-value.
ENDCASE.
AT END OF row.
APPEND s_tabi to tabi.
CLEAR s_tabi.
ENDAT.
ENDLOOP.
ELSE.
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = file_path
filetype = 'ASC'
has_field_separator = 'X'
CHANGING
data_tab = tabi.
ENDIF.
DATA: len TYPE i.
file_path = l_file.
TRANSLATE file_path TO UPPER CASE.
len = STRLEN( file_path ) - 3.
CLEAR tabi.
IF file_path+len(3) = 'XLS'.
break slopes.
DATA t_excel TYPE alsmex_tabline OCCURS 0 WITH HEADER LINE.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = l_file
i_begin_col = 1
i_begin_row = 1
i_end_col = 2
i_end_row = 50
TABLES
intern = t_excel
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
LOOP AT t_excel.
CASE t_excel-col.
WHEN 1.
s_tabi-matnr = t_excel-value.
WHEN 2.
s_tabi-erfmg = t_excel-value.
ENDCASE.
AT END OF row.
APPEND s_tabi to tabi.
CLEAR s_tabi.
ENDAT.
ENDLOOP.
ELSE.
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = file_path
filetype = 'ASC'
has_field_separator = 'X'
CHANGING
data_tab = tabi.
ENDIF.
Etiquetas:
excel,
internal table,
txt,
upload,
xls
sexta-feira, 19 de março de 2010
How to create a dialog to go to a local/server directory and save/read a file
this form opens a dialog to select the directory where you want to save/read a local file
*&---------------------------------------------------------------------*
*& Form f4_save_lfile
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_P_LFILE text *----------------------------------------------------------------------* FORM f4_save_lfile CHANGING p_file. DATA: l_filename TYPE string, l_path TYPE string, l_fullpath TYPE string. CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
* window_title =
default_extension = 'TXT'
* default_file_name =
* with_encoding =
* file_filter =
* initial_directory =
prompt_on_overwrite = 'X'
CHANGING
filename = l_filename
path = l_path
fullpath = l_fullpath
* user_action =
* file_encoding =
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
p_file = l_fullpath.
ENDFORM. " f4_save_lfile
To do the same but on a server file, you should use this:
*&---------------------------------------------------------------------*
*& Form f4_rfile
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_P_RFILE text *----------------------------------------------------------------------* FORM f4_rfile CHANGING p_rfile. *" VALUE(I_LOCATION_FLAG) LIKE DXFIELDS-LOCATION DEFAULT ' ' *" VALUE(I_SERVER) LIKE MSXXLIST-NAME DEFAULT '?' *" VALUE(I_PATH) LIKE DXFIELDS-LONGPATH OPTIONAL *" VALUE(FILEMASK) LIKE DXFIELDS-FILEMASK DEFAULT '*.*' *" VALUE(FILEOPERATION) LIKE DXFIELDS-FILEOPER DEFAULT 'R' *" EXPORTING DATA: l_location LIKE dxfields-location, l_server LIKE msxxlist-name, l_path LIKE dxfields-longpath, l_flag LIKE dxfields-abendflag. CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION' EXPORTING i_location_flag = 'A' * I_SERVER = '?' * I_PATH = * FILEMASK = '*.*' * FILEOPERATION = 'R' IMPORTING o_location_flag = l_location o_server = l_server o_path = l_path abend_flag = l_flag EXCEPTIONS rfc_error = 1 error_with_gui = 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.
MOVE l_path TO p_rfile.
ENDFORM. " f4_rfile
*&---------------------------------------------------------------------*
*& Form f4_save_lfile
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_P_LFILE text *----------------------------------------------------------------------* FORM f4_save_lfile CHANGING p_file. DATA: l_filename TYPE string, l_path TYPE string, l_fullpath TYPE string. CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
* window_title =
default_extension = 'TXT'
* default_file_name =
* with_encoding =
* file_filter =
* initial_directory =
prompt_on_overwrite = 'X'
CHANGING
filename = l_filename
path = l_path
fullpath = l_fullpath
* user_action =
* file_encoding =
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
p_file = l_fullpath.
ENDFORM. " f4_save_lfile
To do the same but on a server file, you should use this:
*&---------------------------------------------------------------------*
*& Form f4_rfile
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_P_RFILE text *----------------------------------------------------------------------* FORM f4_rfile CHANGING p_rfile. *" VALUE(I_LOCATION_FLAG) LIKE DXFIELDS-LOCATION DEFAULT ' ' *" VALUE(I_SERVER) LIKE MSXXLIST-NAME DEFAULT '?' *" VALUE(I_PATH) LIKE DXFIELDS-LONGPATH OPTIONAL *" VALUE(FILEMASK) LIKE DXFIELDS-FILEMASK DEFAULT '*.*' *" VALUE(FILEOPERATION) LIKE DXFIELDS-FILEOPER DEFAULT 'R' *" EXPORTING DATA: l_location LIKE dxfields-location, l_server LIKE msxxlist-name, l_path LIKE dxfields-longpath, l_flag LIKE dxfields-abendflag. CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION' EXPORTING i_location_flag = 'A' * I_SERVER = '?' * I_PATH = * FILEMASK = '*.*' * FILEOPERATION = 'R' IMPORTING o_location_flag = l_location o_server = l_server o_path = l_path abend_flag = l_flag EXCEPTIONS rfc_error = 1 error_with_gui = 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.
MOVE l_path TO p_rfile.
ENDFORM. " f4_rfile
Subscrever:
Mensagens (Atom)