HI,
First of all create a button in the ALV toolbar to export to excel sheet.
In the user command, for the function code for this export button, say &EXP, implement it like this.
(Assuming you are using classic ALV)
DATA: file_path TYPE string,
ret_var TYPEi.
file_path = p_file.
IF r_ucomm = '&EXP'.
DATA ref1 TYPEREFTO cl_gui_alv_grid.
CALLFUNCTION'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = ref1.
CALLMETHOD ref1->check_changed_data.
* Delete if file already exists in the system.
CALLMETHOD cl_gui_frontend_services=>file_delete
EXPORTING
filename = file_path
CHANGING
rc = ret_var
EXCEPTIONS
file_delete_failed = 1
cntl_error = 2
error_no_gui = 3
file_not_found = 4
access_denied = 5
unknown_error = 6
not_supported_by_gui = 7
wrong_parameter = 8
OTHERS = 9.
IF sy-subrc = 5.
MESSAGE i398(00) WITHtext-013text-014. "Check if file is already open or locked.
EXIT.
ENDIF.
CLEAR i_export.
* Populate header data
* Populate line details.
LOOPAT i_data INTO wa_data.
* Do formatting as required with the data.
................
MOVE-CORRESPONDING wa_data TO wa_export.
ENDLOOP.
* Convert to excel file and send it to the local system
CALLFUNCTION'SAP_CONVERT_TO_XLS_FORMAT'
EXPORTING
i_filename = p_file
TABLES
i_tab_sap_data = i_export.
ENDIF.