*--------------------------------------------------------------------* * REPORT ZDEMO_EXCEL20 * Demo for method zcl_excel_worksheet-bind_alv: * export data from ALV (CL_GUI_ALV_GRID) object to excel *--------------------------------------------------------------------* REPORT ZDEMO_EXCEL20. *----------------------------------------------------------------------* * CLASS lcl_handle_events DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* class lcl_handle_events definition. public section. methods: on_user_command for event added_function of cl_salv_events importing e_salv_function. endclass. "lcl_handle_events DEFINITION *----------------------------------------------------------------------* * CLASS lcl_handle_events IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* class lcl_handle_events implementation. method on_user_command. perform user_command." using e_salv_function text-i08. endmethod. "on_user_command endclass. "lcl_handle_events IMPLEMENTATION *--------------------------------------------------------------------* * DATA DECLARATION *--------------------------------------------------------------------* DATA: lo_excel TYPE REF TO zcl_excel, lo_worksheet TYPE REF TO zcl_excel_worksheet, lo_alv type ref to cl_gui_alv_grid, lo_salv type ref to cl_salv_table, gr_events type ref to lcl_handle_events, lr_events type ref to cl_salv_events_table, gt_sbook type table of sbook, gt_listheader type slis_t_listheader, wa_listheader like line of gt_listheader. data: l_path type string, " local dir lv_workdir type string, lv_file_separator type c. constants: lv_default_file_name type string value '20_BindAlv.xlsx'. *--------------------------------------------------------------------* *START-OF-SELECTION *--------------------------------------------------------------------* START-OF-SELECTION. * get data * ------------------------------------------ SELECT * INTO TABLE gt_sbook[] FROM sbook UP TO 10 ROWS. * Display ALV * ------------------------------------------ try. cl_salv_table=>factory( exporting list_display = abap_false importing r_salv_table = lo_salv changing t_table = gt_sbook[] ). catch cx_salv_msg . endtry. TRY. CALL METHOD lo_salv->SET_SCREEN_STATUS EXPORTING REPORT = sy-repid PFSTATUS = 'ALV_STATUS' SET_FUNCTIONS = lo_salv->c_functions_all. catch cx_salv_msg . ENDTRY. lr_events = lo_salv->get_event( ). create object gr_events. set handler gr_events->on_user_command for lr_events. lo_salv->display( ). *&---------------------------------------------------------------------* *& Form USER_COMMAND *&---------------------------------------------------------------------* * ALV user command *--------------------------------------------------------------------* FORM USER_COMMAND . IF sy-ucomm = 'EXCEL'. * get save file path cl_gui_frontend_services=>directory_browse( exporting initial_folder = l_path changing selected_folder = l_path ). IF l_path is initial. cl_gui_frontend_services=>get_sapgui_workdir( changing sapworkdir = lv_workdir ). l_path = lv_workdir. ENDIF. cl_gui_frontend_services=>get_file_separator( changing file_separator = lv_file_separator ). concatenate l_path lv_file_separator lv_default_file_name into l_path. * export file to save file path perform export_to_excel. ENDIF. ENDFORM. " USER_COMMAND *--------------------------------------------------------------------* * FORM EXPORT_TO_EXCEL *--------------------------------------------------------------------* * This subroutine is principal demo session *--------------------------------------------------------------------* FORM EXPORT_TO_EXCEL. * create zcl_excel_worksheet object CREATE OBJECT lo_excel. lo_worksheet = lo_excel->get_active_worksheet( ). * get ALV object from screen call function 'GET_GLOBALS_FROM_SLVC_FULLSCR' IMPORTING e_grid = lo_alv. * build list header wa_listheader-typ = 'H'. wa_listheader-info = sy-title. append wa_listheader to gt_listheader. wa_listheader-typ = 'S'. wa_listheader-info = 'Created by: ABAP2XLSX Group'. append wa_listheader to gt_listheader. wa_listheader-typ = 'A'. wa_listheader-info = 'Project hosting at https://cw.sdn.sap.com/cw/groups/abap2xlsx'. append wa_listheader to gt_listheader. * write to excel using method Bin_ALV CALL METHOD lo_worksheet->BIND_ALV EXPORTING * I_DOCUMENT_URL = SPACE " excel template * I_XLS = 'X' " create in xls format? I_SAVE_PATH = l_path IO_ALV = lo_alv IT_LISTHEADER = gt_listheader I_TOP = 2 I_LEFT = 1 * I_COLUMNS_HEADER = 'X' * I_COLUMNS_AUTOFIT = 'X' * I_FORMAT_COL_HEADER = * I_FORMAT_SUBTOTAL = * I_FORMAT_TOTAL = EXCEPTIONS MISS_GUIDE = 1 EX_TRANSFER_KKBLO_ERROR = 2 FATAL_ERROR = 3 INV_DATA_RANGE = 4 DIM_MISMATCH_VKEY = 5 DIM_MISMATCH_SEMA = 6 ERROR_IN_SEMA = 7 others = 8 . IF SY-SUBRC <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. ENDFORM. "EXPORT_TO_EXCEL