ABAP——批量下载表数据
效果:
代码:
*&---------------------------------------------------------------------* *& Report ZPPRTEST *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* REPORT ZPPRTEST. TABLES: dd02l. DATA: go_struct TYPE REF TO cl_abap_structdescr, go_table TYPE REF TO cl_abap_tabledescr, gi_data TYPE REF TO data. FIELD-SYMBOLS: <gi_data> TYPE STANDARD TABLE. SELECT-OPTIONS: s_tabnm FOR dd02l-tabname. PARAMETERS: p_path LIKE rlgrap-filename DEFAULT 'Z:\' OBLIGATORY. AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path. " 获取档案路径 PERFORM get_path. START-OF-SELECTION. PERFORM get_table. END-OF-SELECTION. FORM get_path. DATA: l_finit TYPE string, " default file path l_path TYPE string, " file path l_len TYPE i. " file path length l_finit = 'Z:\'. " default CALL METHOD cl_gui_frontend_services=>directory_browse EXPORTING window_title = 'Find Output Path' initial_folder = l_finit CHANGING selected_folder = l_path EXCEPTIONS cntl_error = 1 error_no_gui = 2 not_supported_by_gui = 3 OTHERS = 4. *最後一個字元不為'\',補上'\' IF l_path IS NOT INITIAL. l_len = strlen( l_path ) - 1. IF l_path+l_len(1) NE '\'. p_path = l_path && |\\|. ELSE. p_path = l_path. ENDIF. ENDIF. ENDFORM. FORM get_table. DATA: BEGIN OF lt_tab OCCURS 0, tabname TYPE tabname, END OF lt_tab. SELECT tabname INTO TABLE lt_tab FROM dd02l WHERE tabname IN s_tabnm. IF lt_tab[] IS NOT INITIAL. LOOP AT lt_tab. PERFORM download_data USING lt_tab-tabname. ENDLOOP. ENDIF. ENDFORM. FORM download_data USING in_name. DATA: l_path TYPE string. * Dynamically getting the line type of the specified table "go_struct ?= cl_abap_typedescr=>describe_by_name( 'T134' ) . go_struct ?= cl_abap_typedescr=>describe_by_name( in_name ) . TRY. CALL METHOD cl_abap_tabledescr=>create EXPORTING p_line_type = go_struct p_table_kind = 'S' RECEIVING p_result = go_table. CATCH cx_sy_table_creation . *MESSAGE e000 WITH text-004. ENDTRY. * creating internal table of table type just created CREATE DATA gi_data TYPE HANDLE go_table. * Assigning the internal table to field symbol ASSIGN gi_data->* TO <gi_data>. ** get the data into the internal table from db table CLEAR <gi_data>. SELECT * INTO TABLE <gi_data> FROM (in_name). CONCATENATE p_path in_name '.xls' INTO l_path. CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING filename = l_path write_field_separator = 'X' confirm_overwrite = 'X' "存在则覆盖 TABLES data_tab = <gi_data> 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 NE 0. * MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno * WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. * ENDIF. ENDFORM.
落霞与孤鹜齐飞,秋水共长天一色