一、效果
在ALV中点击实现Excel导出
二、代码实现
1.添加GUI按钮
2.se38中代码实现
REPORT ZMMR_CK_TEST2. TABLES:mara,mard,ztmm012. TYPE-POOLS:slis. ************************************************************************ * TYPES ************************************************************************ TYPES:BEGIN OF ty_download, A TYPE char5 , B TYPE char5, END OF ty_download. ************************************************************************ * DATAS ************************************************************************ DATA:WA_DOWNLOAD TYPE TY_DOWNLOAD, GT_DOWNLOAD TYPE TABLE OF TY_DOWNLOAD. DATA: wa_field TYPE slis_fieldcat_alv, gt_field TYPE slis_t_fieldcat_alv, cl_ref_grid TYPE REF TO cl_gui_alv_grid, wa_layout TYPE slis_layout_alv. DATA: BEGIN OF gt_hd OCCURS 0 , field TYPE char100, END OF gt_hd. START-OF-SELECTION. PERFORM FRM_GET_DATA. PERFORM FRM_SHOW_DATA. FORM FRM_GET_DATA."准备数据 WA_DOWNLOAD-A = 'ABCDE'. WA_DOWNLOAD-B = '12345'. APPEND WA_DOWNLOAD TO GT_DOWNLOAD. WA_DOWNLOAD-A = 'HIJKM'. WA_DOWNLOAD-B = '10203'. APPEND WA_DOWNLOAD TO GT_DOWNLOAD. ENDFORM. FORM FRM_SHOW_DATA. "ALV数据展示 REFRESH gt_field. PERFORM frm_fill_fieldcat USING:'A' 'A' ' ' ' ' ' ' ' ', 'B' 'B' ' ' ' ' ' ' ' '. wa_layout-zebra = 'X'. wa_layout-colwidth_optimize = 'X'. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING i_callback_program = sy-cprog i_callback_pf_status_set = 'FRM_STATUS_SET' i_callback_user_command = 'FRM_USER_COMMAND' is_layout = wa_layout it_fieldcat = gt_field i_default = 'X' i_save = 'X' TABLES t_outtab = GT_DOWNLOAD EXCEPTIONS program_error = 1 OTHERS = 2. ENDFORM. "FRM_SHOW_DATA FORM frm_status_set USING lt_status_name TYPE slis_t_extab. SET PF-STATUS '1001'. ENDFORM. FORM frm_user_command USING ucomm TYPE sy-ucomm rs_selfield TYPE slis_selfield. DATA: lv_msg TYPE char40. CASE ucomm. WHEN 'ZEXPORT'. "导出按钮 PERFORM frm_saveas_excel. endcase. endform. FORM frm_saveas_excel. DATA: lv_filename TYPE string, lv_path TYPE string, lv_fullpath TYPE string, lv_destination LIKE rlgrap-filename, ls_objdata LIKE wwwdatatab, lv_objid TYPE wwwdatatab-objid, lv_rc TYPE sy-subrc, lv_tabix TYPE i. CONCATENATE lv_filename '_' sy-datum '_' sy-uzeit INTO lv_filename. ********************************选取存储地点*************************** CALL METHOD cl_gui_frontend_services=>file_save_dialog EXPORTING default_extension = 'XLSX' default_file_name = lv_filename initial_directory = lv_path CHANGING filename = lv_filename path = lv_path fullpath = lv_fullpath EXCEPTIONS cntl_error = 1 error_no_gui = 2 not_supported_by_gui = 3 OTHERS = 4. "excel表头设置 APPEND 'title-A' TO gt_hd. APPEND 'title-B' TO gt_hd. CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING filename = lv_fullpath filetype = 'ASC' write_field_separator = 'X' TABLES data_tab = gt_download fieldnames = gt_hd. ENDFORM. "frm_saveas_excel FORM frm_fill_fieldcat USING u_a u_b u_c u_d u_e u_f. wa_field-seltext_m = u_a. wa_field-fieldname = u_b. wa_field-edit = u_c. wa_field-checkbox = u_d. wa_field-decimals_out = u_e. wa_field-no_zero = u_f. APPEND wa_field TO gt_field. CLEAR wa_field. ENDFORM. " FRM_FILL_FIELDCAT