CSV文件是逗号分隔符格式的文件。将内表数据下载成CSV文件到本机上要先将内表中的数据通过函数SAP_CONVERT_TO_TEX_FORMAT转换成CSV格式,然后再下载到本机。
完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
REPORT z_download_csv. TYPE-POOLS: truxs. *&---------------------------------------------------------------------* *& Data Types *&---------------------------------------------------------------------* TYPES: BEGIN OF ty_spfli, carrid TYPE spfli-carrid, connid TYPE spfli-connid, countryfr TYPE spfli-countryfr, cityfrom TYPE spfli-cityfrom, airpfrom TYPE spfli-airpfrom, countryto TYPE spfli-countryto, cityto TYPE spfli-cityto, airpto TYPE spfli-airpto, END OF ty_spfli. *&---------------------------------------------------------------------* *& Data Declaration *&---------------------------------------------------------------------* DATA: gt_spfli TYPE TABLE OF ty_spfli. DATA: gt_text TYPE truxs_t_text_data. *&---------------------------------------------------------------------* *& Start-of-Selection *&---------------------------------------------------------------------* START-OF-SELECTION. SELECT carrid connid countryfr cityfrom airpfrom countryto cityto airpto UP TO 10 ROWS FROM spfli INTO TABLE gt_spfli. * 将内表数据转换成逗号分割符CSV文件 CALL FUNCTION 'SAP_CONVERT_TO_TEX_FORMAT' EXPORTING i_field_seperator = ',' " Comma seperator TABLES i_tab_sap_data = gt_spfli CHANGING i_tab_converted_data = gt_text 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. * 下载CSV文件 CALL METHOD cl_gui_frontend_services=>gui_download EXPORTING filename = 'c:\temp\test.csv' CHANGING data_tab = gt_text 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. |
运行结果:
文件内容:
用notepad打开显示如下:
上载CSV文件参照-->上载CSV文件到本机(KCD_CSV_FILE_TO_INTERN_CONVERT)
以上。
发表评论