上载和下载CSV文件
sap中把txt 、excel 、文件上载到内表中,txt和csv速度最快。
excel文件导出的csv是用,分隔符分隔的,如果单元格的文本中就有逗号,这样会和分隔符逗号混淆,最好abap产生csv文件模板,代码如下:
REPORT ztest_bapi. TYPE-POOLS truxs. TYPES: BEGIN OF ty_csv, name1 TYPE char10, name2 TYPE char10, name3 TYPE char10, name4 TYPE char10, END OF ty_csv. DATA: w_filename TYPE string, w_file_path TYPE string, w_full_path TYPE string, lt_tabout TYPE STANDARD TABLE OF ty_csv, ls_tabout TYPE ty_csv. DATA: lt_title TYPE truxs_t_text_data WITH HEADER LINE. constants: c_tab type c value cl_abap_char_utilities=>HORIZONTAL_TAB. FIELD-SYMBOLS <fs> TYPE ANY. ls_tabout-name1 = '物料1'. ls_tabout-name2 = '物料2'. ls_tabout-name3 = '物料3'. ls_tabout-name4 = '物料4'. APPEND ls_tabout TO lt_tabout. CALL METHOD cl_gui_frontend_services=>file_save_dialog EXPORTING default_extension = 'CSV' default_file_name = '*' file_filter = 'CSV文件(*.CSV)' CHANGING filename = w_filename path = w_file_path fullpath = w_full_path 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. CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT' * EXPORTING * I_FIELD_SEPERATOR = ';' * I_LINE_HEADER = * I_FILENAME = * I_APPL_KEEP = ' ' TABLES i_tab_sap_data = lt_tabout CHANGING I_TAB_CONVERTED_DATA = lt_title[] EXCEPTIONS CONVERSION_FAILED = 1 OTHERS = 2 . IF sy-subrc <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. *--更改分隔符 为tab键 LOOP AT lt_title ASSIGNING <fs> . REPLACE ALL OCCURRENCES OF ';' IN <fs> WITH c_tab. ENDLOOP. *----下载模板 CALL METHOD cl_gui_frontend_services=>gui_download EXPORTING * bin_filesize = filename = w_filename ** filetype = 'DAT' * codepage = '8404' * ignore_cerr = ABAP_TRUE * replacement = '#' CHANGING data_tab = lt_title[] 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 not_supported_by_gui = 22 error_no_gui = 23 OTHERS = 24. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF.