在SAP的标准报表中虽然有标准的内表内容下载到本地的功能,此功能是将全部的内表数据下载到本地,想要手动选择下载的数据,就需要使用
下载函数,SAP提供了多种下载函数,本文采用的是 'GUI_DOWNLOAD'进行下载。
实例代码如下:
DATA :BEGIN OF gs_itab , matnr TYPE mara-matnr, mtart TYPE mara-mtart, matkl TYPE mara-matkl, meins TYPE mara-meins, zflag TYPE flag, END OF gs_itab. DATA : gt_itab LIKE TABLE OF gs_itab . FIELD-SYMBOLS : <fs_itab> LIKE gs_itab. DATA: gt_field TYPE lvc_t_fcat WITH HEADER LINE, ls_layout TYPE lvc_s_layo. DATA: lr_grid TYPE REF TO cl_gui_alv_grid. "ALV 回车事件用到 DEFINE fieldcat. gt_field-col_pos = &1. gt_field-fieldname = &2. gt_field-scrtext_l = &3. APPEND gt_field. END-OF-DEFINITION. START-OF-SELECTION. SELECT mara~matnr mara~mtart mara~matkl mara~meins FROM mara INTO CORRESPONDING FIELDS OF TABLE gt_itab UP TO 100 ROWS. fieldcat '' 'MATNR' '物料'. fieldcat '' 'MTART' '物料类型'. fieldcat '' 'MATKL' '物料组'. fieldcat '' 'MEINS' '基本单位'. ls_layout-cwidth_opt = 'X'. ls_layout-box_fname = 'ZFLAG'. "设置多选行 ls_layout-zebra = 'X'. ls_layout-cwidth_opt = 'X'. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' EXPORTING i_callback_pf_status_set = 'SET_STATUS' i_callback_user_command = 'USER_COMMAND' i_callback_program = sy-cprog is_layout_lvc = ls_layout it_fieldcat_lvc = gt_field[] TABLES t_outtab = gt_itab. * EXCEPTIONS * PROGRAM_ERROR = 1 * OTHERS = 2 . IF sy-subrc <> 0. * Implement suitable error handling here ENDIF. FORM user_command USING ucomm LIKE sy-ucomm selfield TYPE slis_selfield. CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR' IMPORTING e_grid = lr_grid. CALL METHOD lr_grid->check_changed_data. selfield-refresh = 'X'. selfield-row_stable = 'X'. selfield-col_stable = 'X'. DATA gt_data LIKE TABLE OF gs_itab. CHECK sy-subrc = 0. CASE ucomm. WHEN '&AWSL'. PERFORM frm_download_excel. ENDCASE. ENDFORM. FORM set_status USING rt_extab TYPE slis_t_extab. DATA : fcode TYPE TABLE OF sy-ucomm . SET PF-STATUS 'STANDARD' EXCLUDING fcode . ENDFORM. " SHOW_ALV FORM frm_download_excel . DATA : BEGIN OF gs_data, matnr TYPE mara-matnr, mtart TYPE mara-mtart, matkl TYPE mara-matkl, meins TYPE mara-meins, END OF gs_data. DATA gt_data LIKE TABLE OF gs_data. LOOP AT gt_itab INTO gs_itab WHERE zflag = 'X'. CLEAR gs_data. MOVE-CORRESPONDING gs_itab TO gs_data. APPEND gs_data TO gt_data. CLEAR gs_data. ENDLOOP. DATA:BEGIN OF t_fieldnames OCCURS 0, name TYPE char20, END OF t_fieldnames. t_fieldnames-name = '物料'. APPEND t_fieldnames. t_fieldnames-name = '物料类型'. APPEND t_fieldnames. t_fieldnames-name = '物料组'. APPEND t_fieldnames. t_fieldnames-name = '基本单位'. APPEND t_fieldnames. DATA: fullpath TYPE string, filename TYPE string, path TYPE string, user_action TYPE i, encoding TYPE abap_encoding. CALL METHOD cl_gui_frontend_services=>file_save_dialog EXPORTING window_title = '保存文件' default_extension = 'xls' default_file_name = '物料信息表.XLS' with_encoding = 'X' file_filter = '(电子表格EXCEL)' " initial_directory = 'D:\TEXT\1234.xls' * prompt_on_overwrite = 'X' CHANGING filename = filename path = path fullpath = fullpath user_action = user_action file_encoding = encoding EXCEPTIONS cntl_error = 1 error_no_gui = 2 not_supported_by_gui = 3 invalid_default_file_name = 4 OTHERS = 5. IF sy-subrc <> 0. * Implement suitable error handling here ENDIF. CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING filename = fullpath filetype = 'DAT' "IMPORTING " FILELENGTH = length TABLES data_tab = gt_data fieldnames = t_fieldnames 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. * Implement suitable error handling here ENDIF. ENDFORM.
需要额外配置GUI状态,添加下载功能按钮‘&AWSL’如图所示
函数的局限性:只能生成.xls格式。但当FILETYPE为DAT和ASC时,生成的文件用EXCEL打开时会弹出对话框提示"文件的格式与文件扩展名指定的格式不一致,打开文件前请验证文件没有损坏且来源可信,是否立即打开该文件"。
原因: 以DAT和ASC格式存的表格其实是TXT格式,与EXCEL格式不一致,将扩展名改为TXT,就不会报错了。