Skip to Content
0
Former Member
Mar 07, 2012 at 01:28 PM

Issue in downloading data into Excel/CSV in Web UI

364 Views

Hi gurus,

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.

Even 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.

I tried to create url of the excel file using the below code and used javascript window.open("url") in the corresponding bsp page, to the open the excel.

Event Handler

"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).

View.htm

"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.

Kindly help.

Thanks and regards,

Arun N K.