cancel
Showing results for 
Search instead for 
Did you mean: 

Saving internal data to Excel 2010 works but warning appears trying to open file

former_member210148
Participant
0 Kudos

Good day, everyone.  I'm typing this a second time because the first time around, for whatever reason, the system removed my explanation of the issue and only kept the code I posted in the same message!  Not sure why that happened, but I'll post the code as a follow-up reply this time.

Background:  I'm a Web Dynpro for ABAP newbie (just took the fundamentals class two weeks ago) and I'm having an issue that I've been unable to resolve even after three solid hours of Googling for a solution this morning.  I'm using a seemingly-common solution for saving internal data to an Excel file, and it does work.  However, when I open the file in Excel, I get the following warning pop-up:

"The file you are trying to open, 'account_list.xls', is in a different format than specified by the file extension.  Verify that the file is not corrupted and is from a trusted source before opening the file.  Do you want to open the file now?"

If I click 'Yes', the file does open and the data looks good, but obviously I'd like to save it correctly so that the customer doesn't get that warning pop-up.

As I said, I spent a lot of time searching for a solution, and while I found a lot of posts that seemed related, nothing solved the issue.  I've tried changing the filename extension to .xlsx (since we're using Office 2010) as well as the mime type values (see the commented-out values in the code) but it didn't work.

The data I'm saving, by the way, is in an internal table and is just a few simple string fields.  I don't need any fancy Excel formatting or anything like that -- just a straight dump into Excel.  I do understand there's a different between .xls and xlsx formats, but that's the extent of my knowledge there.  I apologize if there IS a post with a fix somewhere out there that I might've missed.

Thanks in advance!

Dave

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member210148
Participant
0 Kudos

The code:

  DATA:
    str                TYPE string,
    xstr               TYPE xstring,
    w_write_headers_sw TYPE boolean VALUE abap_true,
    w_filename         TYPE string.

  DATA lo_nd_hierarchy TYPE REF TO if_wd_context_node.
  DATA lt_hierarchy    TYPE wd_this->elements_hierarchy.

  FIELD-SYMBOLS:
    <a1> LIKE LINE OF lt_hierarchy,
    <a2> TYPE ty_grant_detail.

* navigate from <CONTEXT> to <HIERARCHY> via lead selection
  lo_nd_hierarchy = wd_context->get_child_node( name = wd_this->wdctx_hierarchy ).
  lo_nd_hierarchy->get_static_attributes_table( IMPORTING table = lt_hierarchy ).

* Build the default file name.
  CONCATENATE 'account_list_' sy-datum '_' sy-uzeit '.xls'
         INTO w_filename.

* Start building our Excel row lines.
  LOOP AT lt_hierarchy ASSIGNING <a1>.
    CONCATENATE str
                <a1>-parent_row_key "Grant
                <a1>-id             "Sponsored Program
                cl_abap_char_utilities=>newline
           INTO str
           SEPARATED BY cl_abap_char_utilities=>horizontal_tab.
  ENDLOOP.

  CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
    EXPORTING
      text   = str
    IMPORTING
      buffer = xstr.

  CALL METHOD cl_wd_runtime_services=>attach_file_to_response
    EXPORTING
      i_filename  = w_filename
      i_content   = xstr
      i_mime_type = 'EXCEL'.
*      i_mime_type = 'application/vnd.ms-excel'.
*      i_mime_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'.