I am working on a requirement where I need to download some data into Excel or CSV format. I have a custom button in the BSP page and i wrote the below code in the associated event handler.
"lv_output is already filled with the data. "Convert the string into xstring. CALL FUNCTION 'SCMS_STRING_TO_XSTRING' EXPORTING text = lv_output mimetype = 'APPLICATION/MSEXCEL' IMPORTING buffer = lv_x_output. "Find the length of the xls file. lv_xls_len = XSTRLEN( lv_x_output ). response->set_header_field( name = 'Content-Type' value = 'APPLICATION/MSEXCEL' ). "Some Browsers have caching problems when loading Excel Format response->delete_header_field( name = if_http_header_fields=>cache_control ). response->delete_header_field( name = if_http_header_fields=>expires ). response->delete_header_field( name = if_http_header_fields=>pragma ). "Start Excel viewer either in the Browser or as a separate window response->set_header_field( name = 'Content-Disposition' value = 'attachment; filename=export.xls' ). response->set_data( data = lv_x_output length = lv_xls_len ). navigation->response_complete( ).
(Thanks to SDN forums and blogs for the above code)
The issue i am facing:
Instead of opening the excel file, the content of the excel is getting displayed in the same window. Can you kindly reply if you have any solution or suggestions to 'open' the excel instead of getting displayed in the window of the browser.
"create the url CONCATENATE runtime->application_url '/' lv_guid_32 'xls' INTO gv_display_url. "upload the content into application server cache cl_http_server=>server_cache_upload( url = gv_display_url response = response ).
(gv_display_url is a static attribute declared in the corresponding implementation class).
"inside java script window.open("<%=>zl_***_impl=>gv_display_url%>").focus();
This method worked for a few times. But most of the time, the window is getting closed immediately without opening the excel. When i checked in ICM Monitor (Tx:SMICM, Goto->HTTP Server Cache), i was able to see and open the excel file, which means excel file got created and saved in the application server cache successfully.
Thanks and regards,
Arun N K.