Skip to Content
avatar image
Former Member

How to download data into XLSX file in ABAP ECC 6.0 using FM: GUI_DOWNLOAD or any other FunctionModules

Hi ABAP Exterts,

Any one help on this , there is a requirement to download the data into XLSX file only.


I am able to to download the data into XLS file using the FM: GUI_DOWNLOAD in SAP ECC 6.0and also opening the file . But when i download the data into XLSX file . I am able to download the data into GUI_DOWNLOAD. But while opening the file i am getting the below mentioned error.

'Excel cannot open the file 'filename.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file'

Ok buttom is coming not able to open the file.

When i use the FM: SAP_CONVERT_TO_XLS_FORMAT to download the XLS it self it is going abap DUMP that file type error.

Any one help any Function Module or how to download data through abap programing into XLSX file.

Thanks.

Murali.

Message was edited by: Matthew Billingham - adjusted wording...

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

5 Answers

  • Mar 26, 2014 at 09:42 AM

    Hi Murali,

    as far as i know GUI_DOWNLOAD is not used to download data in xlsx format, but there are some methods through which you can achieve this, please see below threads which can help you

    Happy New year SCN and Happy New Excel with abap2xlsx

    abap2xlsx - ABAP Development - SCN Wiki

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 26, 2014 at 03:17 PM

    Moderator message: using words like "urgent" "asap" is not permitted on this site. If you use these words, you risk your message being rejected. If you're in a hurry, this is probably not what you want.

    I note there are several hours between responses, so it doesn't look like it's that urgent.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 17, 2014 at 10:35 AM

    If you want to download your ALV to an .XLXS format file, with all the formatting intact, please try the following code. I have tried to create a custom Function module Z_EXCEL_DOWNLOAD_TEST wherein I pass all the callback variables, the final ALV internal table and all the ALV details like field catalog, sort, layout, event list and so on, along with the concatenated file path and file name in I_FILE. I have used a custom class ZCL_SALVEXPORT_XML which is a copy of the standard class CL_SALV_EXPORT_XML_DIALOG, with few modifications in Method: EXECUTE and Method: DOWNLOAD. The methods where I have modified the code are also shown at the end.

    ****************Global declarations***********************
    TYPE-POOLS: slis,
    kkblo.

    TYPES: t_fccls(1) TYPE c OCCURS 0.


    TYPES: BEGIN OF ty_nosum,
    fieldname TYPE kkblo_fieldname, "Field name
    tabname TYPE kkblo_tabname, "Table name
    fieldname_dep TYPE kkblo_fieldname, "Field name
    tabname_dep TYPE kkblo_tabname, "Table name
    index TYPE sy-index, "Index
    END OF ty_nosum.

    TYPES: t_nosum TYPE TABLE OF ty_nosum INITIAL SIZE 0 WITH NON-UNIQUE DEFAULT KEY.

    TYPES: BEGIN OF ty_dep_field,
    s_dep_fieldcat TYPE kkblo_fieldcat, "Field cat
    t_ref_fieldcat TYPE kkblo_t_fieldcat, "Field cat
    END OF ty_dep_field.

    TYPES: BEGIN OF ty_ref_field,
    s_ref_fieldcat TYPE kkblo_fieldcat, "Field cat
    s_dep_fieldcat TYPE kkblo_fieldcat, "Field cat
    END OF ty_ref_field.

    TYPES: t_dep_field TYPE ty_dep_field OCCURS 0. "
    TYPES: t_ref_field TYPE ty_ref_field OCCURS 0.

    TYPES: BEGIN OF ty_memory,
    object(20) TYPE c, "Object
    memcount(4) TYPE c, "count
    END OF ty_memory.
    *ALV Customer Wrapper
    TYPES: BEGIN OF ty_type_header_info,
    tabname TYPE kkblo_tabname, "Table name
    fieldname TYPE kkblo_fieldname, "Field name
    sequence TYPE i, "Sequence
    field TYPE char80, "Field
    pagno TYPE sypagno, "Page num
    linno TYPE sy-linno, "Current Line in List
    colno TYPE sy-colno, "Current List Column
    END OF ty_type_header_info,
    t_type_header_info TYPE SORTED TABLE OF ty_type_header_info
    WITH UNIQUE KEY tabname fieldname.

    TYPES: BEGIN OF ty_type_row_info,
    tabname TYPE kkblo_tabname, "Table name
    tabindex TYPE syindex, "Index
    field TYPE char80, "Field name
    pagno TYPE sypagno, "Page num
    linno TYPE sy-linno, "Current Line in List
    colno TYPE sy-colno, "Current List Column
    END OF ty_type_row_info,
    t_type_row_info TYPE SORTED TABLE OF ty_type_row_info
    WITH UNIQUE KEY tabname tabindex.

    TYPES: BEGIN OF ty_type_sel_header,
    tabname TYPE kkblo_tabname, "table name
    fieldname TYPE kkblo_fieldname, "field name
    END OF ty_type_sel_header,
    t_type_sel_header TYPE SORTED TABLE OF ty_type_sel_header
    WITH UNIQUE KEY tabname fieldname.

    TYPES: BEGIN OF ty_type_sel_row,
    tabname TYPE kkblo_tabname,
    tabindex TYPE syindex,
    END OF ty_type_sel_row,
    t_type_sel_row TYPE SORTED TABLE OF ty_type_sel_row
    WITH UNIQUE KEY tabname tabindex.

    TYPES: BEGIN OF ty_type_sel_cell,
    tabname TYPE kkblo_tabname,
    tabindex TYPE syindex,
    fieldname TYPE kkblo_fieldname,
    END OF ty_type_sel_cell.


    TYPES: BEGIN OF ty_type_selections,
    t_row_info TYPE t_type_row_info,
    t_header_info TYPE t_type_header_info,
    t_selected_headers TYPE t_type_sel_header,
    t_selected_rows TYPE t_type_sel_row,
    s_current_cell TYPE ty_type_sel_cell,
    set_headers TYPE sap_bool,
    set_rows TYPE sap_bool,
    set_current_cell TYPE sap_bool,
    END OF ty_type_selections.


    TYPES: BEGIN OF ty_stack3,
    r_grid TYPE REF TO cl_gui_alv_grid, "Y6DK050176
    r_salv_adapter TYPE REF TO if_salv_adapter,
    s_selections TYPE ty_type_selections,
    r_print_state TYPE REF TO if_alv_print_parameter_proxy, "Y6DK075821
    suppress_empty_data TYPE abap_bool,
    END OF ty_stack3.
    TYPES: t_stack3 TYPE ty_stack3 OCCURS 0.

    TYPES: BEGIN OF ty_stack,
    i_callback_program TYPE sy-repid,
    i_callback_user_command TYPE kkblo_formname,
    i_callback_top_of_page TYPE kkblo_formname,
    i_callback_top_of_list TYPE kkblo_formname,
    i_callback_end_of_page TYPE kkblo_formname,
    i_callback_end_of_list TYPE kkblo_formname,
    i_callback_pf_status_set TYPE kkblo_formname,
    i_callback_fieldcat_save TYPE kkblo_formname,
    i_callback_layout_save TYPE kkblo_formname,
    i_tabname TYPE kkblo_tabname,
    i_tabname_slave TYPE kkblo_tabname,
    is_keyinfo TYPE kkblo_keyinfo,
    is_layout TYPE kkblo_layout,
    it_fieldcat TYPE kkblo_t_fieldcat,
    i_fctype(1) TYPE c,
    it_excluding TYPE kkblo_t_extab,
    it_special_groups TYPE kkblo_t_sp_group,
    it_sort TYPE kkblo_t_sortinfo,
    it_except_qinfo TYPE kkblo_t_qinfo,
    is_sel_hide TYPE kkblo_sel_hide,
    t_columns TYPE kkblo_t_columns,
    t_filter TYPE kkblo_t_filter,
    s_lprint TYPE kkblo_lprint,
    print_line_break TYPE char1,
    t_isetab TYPE kkblo_seldis,
    t_fccls TYPE t_fccls,
    t_sfinfo TYPE kkblo_t_sfinfo,
    t_ref_field TYPE t_ref_field,
    t_dep_field TYPE t_dep_field,
    flg_default_status(1) TYPE c,
    flg_selections TYPE char1,
    flg_allow_no_selection TYPE char1,
    flg_input TYPE char1,
    flg_called TYPE char1,
    flg_popup TYPE char1,
    flg_endlist TYPE char1,
    listtype TYPE char1,
    actcols TYPE i,
    is_list TYPE char1,
    END OF ty_stack.
    TYPES: t_stack TYPE ty_stack OCCURS 0.

    TYPES: BEGIN OF ty_stack2,
    button_col1(20) TYPE c,
    button_col2(20) TYPE c,
    button_col3(20) TYPE c,
    button_col4(20) TYPE c,
    button_col5(20) TYPE c,
    s_header TYPE kkblo_header,
    t_sfinfo_slave TYPE kkblo_t_sfinfo,
    t_suminfo TYPE kkblo_t_sfinfo,
    t_sfsave TYPE kkblo_t_sfinfo,
    t_nosum TYPE t_nosum,
    window_title TYPE rsmpe-tittext, " Item_selection
    flg_scroll_to_line(1) TYPE c, " Item_selection
    flg_top_of_page(1) TYPE c,
    flg_subtot(1) TYPE c,
    flg_output_in_progress(1) TYPE c,
    flg_grouplevels(1) TYPE c,
    flg_groupedit(1) TYPE c,
    flg_calculate_totals(1) TYPE c,
    sumlevel TYPE sy-tabix,
    memory_stack1 TYPE ty_memory,
    memory_stack2 TYPE ty_memory,
    memory_outtab TYPE ty_memory,
    t_colmark TYPE kkblo_t_colmark,
    t_rowmark TYPE kkblo_t_rowmark,
    it_event_exit TYPE kkblo_t_event_exit,
    t_groups TYPE kkblo_t_grouplevels,
    t_filter_groups TYPE kkblo_t_grouplevels,
    s_subtot_options TYPE kkblo_subtot_options,
    flg_append_mode(1) TYPE c,
    is_reprep_id TYPE kkblo_reprep_id,
    i_callback_before_line_output TYPE kkblo_formname,
    i_callback_after_line_output TYPE kkblo_formname,
    i_callback_foreign_top_of_page TYPE kkblo_formname,
    i_callback_foreign_end_of_page TYPE kkblo_formname,
    i_callback_list_modify TYPE kkblo_formname,
    i_callback_top_of_coverpage TYPE kkblo_formname,
    i_callback_end_of_coverpage TYPE kkblo_formname,
    i_callback_grouplevel_change TYPE kkblo_formname,
    i_callback_item_data_expand TYPE kkblo_formname,
    i_callback_header_transport TYPE kkblo_formname,
    i_callback_reprep_sel_modify TYPE kkblo_formname,
    i_callback_subtotal_text TYPE kkblo_formname,
    END OF ty_stack2.
    TYPES: t_stack2 TYPE TABLE OF ty_stack2 INITIAL SIZE 0.


    ** *--- iTables
    DATA: it_stack TYPE t_stack WITH HEADER LINE.
    DATA: it_stack2 TYPE t_stack2 WITH HEADER LINE.
    ***ALV Customer Wrapper
    DATA: it_stack3 TYPE t_stack3 WITH HEADER LINE.



    CONSTANTS: BEGIN OF c_con_listtype,
    simple(1) TYPE c VALUE 'S',
    hier_sequ(1) TYPE c VALUE 'H',
    matrix(1) TYPE c VALUE 'M',
    END OF c_con_listtype.


    DATA: gv_callback_program1 TYPE sy-repid,
    gv_callback_user_command1 TYPE kkblo_formname,
    gv_callback_top_of_page1 TYPE kkblo_formname,
    gv_callback_top_of_list1 TYPE kkblo_formname,
    gv_callback_end_of_page1 TYPE kkblo_formname,
    gv_callback_end_of_list1 TYPE kkblo_formname,
    gv_callback_pf_status_set1 TYPE kkblo_formname,
    gv_callback_layout_save1 TYPE kkblo_formname,
    gv_callback_fieldcat_save1 TYPE kkblo_formname,
    gv_callback_grouplevel_change1 TYPE kkblo_formname,
    gv_callback_before_line_otput1 TYPE kkblo_formname,
    gv_callback_after_line_otput1 TYPE kkblo_formname,
    gv_callback_foreign_top_of_pg1 TYPE kkblo_formname,
    gv_callback_foreign_end_of_pg1 TYPE kkblo_formname,
    gv_callback_list_modify1 TYPE kkblo_formname,
    gv_callback_top_of_coverpge1 TYPE kkblo_formname,
    gv_callback_end_of_coverpge1 TYPE kkblo_formname,
    gv_callback_item_data_expand1 TYPE kkblo_formname,
    gv_callback_header_transport1 TYPE kkblo_formname,
    gv_callback_reprep_sel_modify1 TYPE kkblo_formname,
    gv_callback_subtotal_text1 TYPE kkblo_formname,
    gv_layout1 TYPE kkblo_layout,
    gv_fieldcat1 TYPE kkblo_t_fieldcat,
    gv_fctype1 TYPE c,
    gv_event_exit1 TYPE kkblo_t_event_exit,
    gv_excluding1 TYPE kkblo_t_extab,
    gv_special_groups1 TYPE kkblo_t_sp_group,
    gv_sort1 TYPE kkblo_t_sortinfo,
    gv_filter1 TYPE kkblo_t_filter,
    gv_except_qinfo1 TYPE slis_t_qinfo_alv,
    gv_sel_hide1 TYPE slis_sel_hide_alv,
    gv_reprep_id1 TYPE slis_reprep_id,
    gv_screen_start_column1 TYPE i,
    gv_salv_adapter1 TYPE REF TO if_salv_adapter,
    gv_suppress_empty_data1 TYPE sap_bool.

    FUNCTION z_excel_download_test.
    *"----------------------------------------------------------------------
    *"*"Local Interface:
    *" IMPORTING
    *" REFERENCE(I_BYPASSING_BUFFER) TYPE CHAR01 OPTIONAL
    *" REFERENCE(I_BUFFER_ACTIVE) OPTIONAL
    *" REFERENCE(I_CALLBACK_PROGRAM) TYPE SY-REPID OPTIONAL
    *" REFERENCE(I_CALLBACK_USER_COMMAND) OPTIONAL
    *" REFERENCE(I_CALLBACK_TOP_OF_PAGE) OPTIONAL
    *" REFERENCE(I_CALLBACK_TOP_OF_LIST) OPTIONAL
    *" REFERENCE(I_CALLBACK_END_OF_PAGE) OPTIONAL
    *" REFERENCE(I_CALLBACK_END_OF_LIST) OPTIONAL
    *" REFERENCE(I_CALLBACK_PF_STATUS_SET) OPTIONAL
    *" REFERENCE(I_CALLBACK_LAYOUT_SAVE) OPTIONAL
    *" REFERENCE(I_CALLBACK_FIELDCAT_SAVE) OPTIONAL
    *" REFERENCE(I_CALLBACK_GROUPLEVEL_CHANGE) OPTIONAL
    *" REFERENCE(I_CALLBACK_BEFORE_LINE_OUTPUT) OPTIONAL
    *" REFERENCE(I_CALLBACK_AFTER_LINE_OUTPUT) OPTIONAL
    *" REFERENCE(I_CALLBACK_FOREIGN_TOP_OF_PAGE) OPTIONAL
    *" REFERENCE(I_CALLBACK_FOREIGN_END_OF_PAGE) OPTIONAL
    *" REFERENCE(I_CALLBACK_LIST_MODIFY) OPTIONAL
    *" REFERENCE(I_CALLBACK_TOP_OF_COVERPAGE) OPTIONAL
    *" REFERENCE(I_CALLBACK_ITEM_DATA_EXPAND) OPTIONAL
    *" REFERENCE(I_CALLBACK_END_OF_COVERPAGE) OPTIONAL
    *" REFERENCE(I_CALLBACK_HEADER_TRANSPORT) OPTIONAL
    *" REFERENCE(I_CALLBACK_REPREP_SEL_MODIFY) OPTIONAL
    *" REFERENCE(I_CALLBACK_SUBTOTAL_TEXT) OPTIONAL
    *" REFERENCE(I_TABNAME) TYPE SLIS_TABNAME OPTIONAL
    *" REFERENCE(I_FCTYPE) DEFAULT 'R'
    *" REFERENCE(IT_EXCEPT_QINFO) TYPE SLIS_T_QINFO_ALV OPTIONAL
    *" REFERENCE(IS_SEL_HIDE) TYPE SLIS_SEL_HIDE_ALV OPTIONAL
    *" REFERENCE(IS_REPREP_ID) TYPE SLIS_REPREP_ID OPTIONAL
    *" REFERENCE(I_SCREEN_START_COLUMN) DEFAULT 0
    *" REFERENCE(I_OO_ALV) OPTIONAL
    *" REFERENCE(IR_SALV_ADAPTER) TYPE REF TO IF_SALV_ADAPTER OPTIONAL
    *" REFERENCE(I_SUPPRESS_EMPTY_DATA) TYPE SAP_BOOL DEFAULT
    *" ABAP_FALSE
    *" REFERENCE(I_FILE) TYPE STRING OPTIONAL
    *" REFERENCE(IT_FIELDCAT) TYPE SLIS_T_FIELDCAT_ALV OPTIONAL
    *" REFERENCE(IS_LAYOUT) TYPE SLIS_LAYOUT_ALV OPTIONAL
    *" REFERENCE(IT_SORT) TYPE SLIS_T_SORTINFO_ALV OPTIONAL
    *" REFERENCE(IT_FILTER) TYPE SLIS_T_FILTER_ALV OPTIONAL
    *" REFERENCE(IT_EXCLUDING) TYPE SLIS_T_EXTAB OPTIONAL
    *" REFERENCE(IT_SPECIAL_GROUPS) TYPE SLIS_T_SP_GROUP_ALV OPTIONAL
    *" REFERENCE(IS_KEYINFO) TYPE SLIS_KEYINFO_ALV OPTIONAL
    *" REFERENCE(IT_EVENT_EXIT) TYPE SLIS_T_EVENT_EXIT OPTIONAL
    *" REFERENCE(IS_LIST_SCROLL) TYPE SLIS_LIST_SCROLL OPTIONAL
    *" REFERENCE(IS_PRINT) TYPE SLIS_PRINT_ALV OPTIONAL
    *" REFERENCE(IS_LINEINFO) TYPE SLIS_LINEINFO OPTIONAL
    *" REFERENCE(I_REPID) TYPE SYREPID OPTIONAL
    *" TABLES
    *" T_OUTTAB TYPE STANDARD TABLE OPTIONAL
    *"----------------------------------------------------------------------


    *********** Local data declarations******************


    DATA: gv_list TYPE char1.
    DATA: gv_fcat_complete TYPE sap_bool.
    DATA: gv_buffer_active TYPE char01,
    wa_variant TYPE disvariant.
    DATA: gv_controller TYPE REF TO cl_salv_export_c8r.

    DATA: gv_xml TYPE xstring.
    DATA: gv_s_xml_choice TYPE if_salv_bs_xml=>s_type_xml_choice.
    DATA: gv_t_xml_choice TYPE if_salv_bs_xml=>t_type_xml_choice.
    DATA: gv_param_lvc TYPE if_salv_export=>s_type_param_lvc.
    DATA: gv_data TYPE REF TO data.

    DATA: gv_result_data TYPE REF TO cl_salv_ex_result_data_table.

    DATA: gv_flavour TYPE string.
    DATA: gv_version TYPE string.
    DATA: it_dummy_keyinfo TYPE kkblo_keyinfo.

    CONSTANTS: c_save TYPE char1 VALUE 'A'.

    *** * move correspondingly data ALV -> Listtool

    CALL FUNCTION 'REUSE_ALV_TRANSFER_DATA'
    EXPORTING
    it_fieldcat = it_fieldcat
    is_layout = is_layout
    it_sort = it_sort
    it_filter = it_filter
    it_excluding = it_excluding
    it_special_groups = it_special_groups
    is_keyinfo = is_keyinfo
    it_event_exit = it_event_exit
    is_list_scroll = is_list_scroll
    is_print = is_print
    is_lineinfo = is_lineinfo
    it_except_qinfo = it_except_qinfo
    IMPORTING
    et_fieldcat = gv_fieldcat1
    es_layout = gv_layout1
    et_sort = gv_sort1
    et_filter = gv_filter1
    et_excluding = gv_excluding1
    et_special_groups = gv_special_groups1
    es_keyinfo = it_dummy_keyinfo
    et_event_exit = gv_event_exit1
    et_except_qinfo = gv_except_qinfo1
    EXCEPTIONS
    OTHERS = 1.

    IF sy-subrc EQ 0 .

    MOVE c_save TO gv_layout1-save.
    MOVE abap_true TO gv_layout1-default.


    IF NOT gv_layout1-save IS INITIAL.
    wa_variant-report = i_repid.
    ENDIF.


    CASE gv_layout1-save.
    WHEN 'A' OR 'U'.

    MOVE-CORRESPONDING wa_variant TO gv_layout1-s_variant.
    WHEN abap_true OR space.

    MOVE-CORRESPONDING wa_variant TO gv_layout1-s_variant.

    ENDCASE.


    *** First call of print preview/Excel download optimize headline
    IF NOT i_oo_alv IS INITIAL.
    gv_list = space.
    ELSE.
    gv_list = abap_true.
    ENDIF.
    gv_callback_program1 = i_callback_program.
    gv_callback_user_command1 = i_callback_user_command .
    gv_callback_top_of_page1 = i_callback_top_of_page.
    gv_callback_top_of_list1 = i_callback_top_of_list .
    gv_callback_end_of_page1 = i_callback_end_of_page.
    gv_callback_end_of_list1 = i_callback_end_of_list .
    gv_callback_pf_status_set1 = i_callback_pf_status_set .
    gv_callback_layout_save1 = i_callback_layout_save .
    gv_callback_fieldcat_save1 = i_callback_fieldcat_save .
    gv_callback_grouplevel_change1 = i_callback_grouplevel_change .
    gv_callback_before_line_otput1 = i_callback_before_line_output .
    gv_callback_after_line_otput1 = i_callback_after_line_output .
    gv_callback_foreign_top_of_pg1 = i_callback_foreign_top_of_page.
    gv_callback_foreign_end_of_pg1 = i_callback_foreign_end_of_page.
    gv_callback_list_modify1 = i_callback_list_modify .
    gv_callback_top_of_coverpge1 = i_callback_top_of_coverpage .
    gv_callback_end_of_coverpge1 = i_callback_end_of_coverpage .
    gv_callback_item_data_expand1 = i_callback_item_data_expand .
    gv_callback_header_transport1 = i_callback_header_transport .
    gv_callback_reprep_sel_modify1 = i_callback_reprep_sel_modify .
    gv_callback_subtotal_text1 = i_callback_subtotal_text .
    gv_fctype1 = i_fctype .
    gv_sel_hide1 = is_sel_hide .
    gv_reprep_id1 = is_reprep_id .
    gv_screen_start_column1 = i_screen_start_column .
    gv_salv_adapter1 = ir_salv_adapter .
    gv_suppress_empty_data1 = i_suppress_empty_data .

    PERFORM globals_push TABLES it_stack
    it_stack2
    it_stack3
    USING i_tabname "#EC *
    space
    it_dummy_keyinfo
    c_con_listtype-simple
    gv_list
    i_oo_alv.


    IF it_stack3-r_salv_adapter IS BOUND.
    gv_fcat_complete = abap_true.
    ENDIF.

    CLEAR gv_buffer_active.
    gv_buffer_active = i_buffer_active.
    CALL FUNCTION 'K_KKB_FIELDCAT_COMPLETE'
    EXPORTING
    i_refresh_buffer = i_bypassing_buffer "neu
    i_buffer_active = gv_buffer_active
    * i_lensave = l_lensave
    i_tabname = it_stack-i_tabname
    i_fcat_complete = gv_fcat_complete
    CHANGING
    cs_layout = it_stack-is_layout
    ct_fieldcat = it_stack-it_fieldcat
    c_input = it_stack-flg_input
    EXCEPTIONS
    OTHERS = 0.
    IF sy-subrc NE 0.
    ENDIF.

    * new api: fieldcat has not been completed
    IF it_stack3-r_salv_adapter IS BOUND.
    CALL METHOD it_stack3-r_salv_adapter->complete_metadata
    CHANGING
    t_fieldcatalog = it_stack-it_fieldcat.
    ENDIF.

    * ... ITS
    DATA: gv_its TYPE sap_bool.
    CALL FUNCTION 'GUI_IS_ITS'
    IMPORTING
    return = gv_its.

    * ... JavaGUI
    DATA: gv_java TYPE sap_bool.
    CALL FUNCTION 'GUI_HAS_JAVABEANS'
    IMPORTING
    return = gv_java.

    DATA: gv_gui_type TYPE i.

    IF gv_java EQ space
    AND gv_its EQ space.
    gv_gui_type = zcl_salvexport_xml=>c_gui_type_windows.

    DATA: gv_appl TYPE i.

    CALL FUNCTION 'ALV_CHECK_PC_APP'
    EXPORTING
    application_type = 1
    IMPORTING
    pc_application = gv_appl.
    ELSE.
    gv_appl = 0. "ne Excel/Lotus
    ENDIF.

    IF gv_appl EQ 2.
    CALL FUNCTION 'ALV_START_LOTUS123'
    EXPORTING
    * IT_FIELDCAT =
    it_fieldcat_kkblo = it_stack-it_fieldcat
    lotus = abap_true
    TABLES
    it_data = t_outtab
    EXCEPTIONS
    lotus123_failure = 1
    OTHERS = 2.
    IF sy-subrc NE 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.

    ELSE. "Appl 1 or 0



    gv_t_xml_choice = zcl_salvexport_xml=>execute( gui_type = gv_gui_type ).

    READ TABLE gv_t_xml_choice INTO gv_s_xml_choice INDEX 1.
    IF sy-subrc NE 0.
    CHECK 1 = 2 .
    ENDIF.


    CREATE OBJECT gv_controller
    EXPORTING
    t_choice = gv_t_xml_choice.



    CALL FUNCTION 'LVC_TRANSFER_FROM_KKBLO'
    EXPORTING
    * I_TECH_COMPLETE =
    * I_STRUCTURE_NAME =
    it_fieldcat_kkblo = it_stack-it_fieldcat
    it_sort_kkblo = it_stack-it_sort
    it_filter_kkblo = it_stack-t_filter
    is_layout_kkblo = it_stack-is_layout
    IMPORTING
    et_fieldcat_lvc = gv_param_lvc-t_fcat
    et_sort_lvc = gv_param_lvc-t_sort
    et_filter_lvc = gv_param_lvc-t_filt
    es_layout_lvc = gv_param_lvc-s_layo
    TABLES
    it_data = t_outtab
    EXCEPTIONS
    it_data_missing = 1
    OTHERS = 2.
    IF sy-subrc <> 0.
    * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

    GET REFERENCE OF t_outtab[] INTO gv_data.

    IF cl_salv_bs_a_xml_base=>get_version( ) EQ if_salv_bs_xml=>version_25 OR
    cl_salv_bs_a_xml_base=>get_version( ) EQ if_salv_bs_xml=>version_26.



    gv_result_data = cl_salv_ex_util=>factory_result_data_table(

    r_data = gv_data
    s_layout = gv_param_lvc-s_layo
    t_fieldcatalog = gv_param_lvc-t_fcat
    t_sort = gv_param_lvc-t_sort
    t_filter = gv_param_lvc-t_filt
    ).


    CASE cl_salv_bs_a_xml_base=>get_version( ).
    WHEN if_salv_bs_xml=>version_25.
    gv_version = if_salv_bs_xml=>version_25.
    WHEN if_salv_bs_xml=>version_26.
    gv_version = if_salv_bs_xml=>version_26.
    ENDCASE.

    gv_flavour = if_salv_bs_c_tt=>c_tt_xml_flavour_export.

    CALL METHOD cl_salv_bs_tt_util=>if_salv_bs_tt_util~transform
    EXPORTING
    xml_type = gv_s_xml_choice-xml_type
    xml_version = gv_version
    r_result_data = gv_result_data
    xml_flavour = gv_flavour
    gui_type = if_salv_bs_xml=>c_gui_type_gui
    IMPORTING
    xml = gv_xml.


    ELSE.
    gv_controller->execute(
    EXPORTING
    t_fcat = gv_param_lvc-t_fcat
    t_sort = gv_param_lvc-t_sort
    t_filt = gv_param_lvc-t_filt
    s_layo = gv_param_lvc-s_layo
    IMPORTING
    e_xml = gv_xml
    CHANGING
    r_data = gv_data
    ).
    ENDIF.

    *...Filedownload
    zcl_salvexport_xml=>download(
    EXPORTING
    s_xml_choice = gv_s_xml_choice
    xml = gv_xml
    i_filename = i_file
    ).


    ENDIF. "appl 0,1 or 2
    ENDIF.

    1. ENDFUNCTION.

    **************LIST of Subroutines in the code**************

    *----------------------------------------------------------------------*
    ***INCLUDE LZ_EXCELDOWNLOAD_TESTF01.
    *----------------------------------------------------------------------*
    *&---------------------------------------------------------------------*
    *& Form GLOBALS_PUSH
    *&---------------------------------------------------------------------*
    * text
    *----------------------------------------------------------------------*
    * -->P_GT_STACK text
    * -->P_GT_STACK2 text
    * -->P_GT_STACK3 text
    * -->P_I_TABNAME text
    * -->P_SPACE text
    * -->P_DUMMY_KEYINFO text
    * -->P_CON_LISTTYPE_SIMPLE text
    * -->P_L_LIST text
    *----------------------------------------------------------------------*
    FORM globals_push TABLES rt_stack STRUCTURE it_stack
    rt_stack2 STRUCTURE it_stack2
    rt_stack3 STRUCTURE it_stack3
    USING r_master TYPE kkblo_tabname
    r_slave TYPE kkblo_tabname
    r_keyinfo TYPE kkblo_keyinfo
    r_listtype TYPE c
    value(i_list) TYPE char1
    i_oo_alv.



    INSERT rt_stack INDEX 1.
    INSERT rt_stack2 INDEX 1.
    *ALV Customer Wrapper
    INSERT rt_stack3 INDEX 1.

    CLEAR rt_stack-flg_endlist.
    CLEAR rt_stack-t_filter.
    CLEAR rt_stack-t_columns.
    CLEAR rt_stack-s_lprint.
    CLEAR rt_stack-t_isetab.
    CLEAR rt_stack-t_fccls.
    CLEAR rt_stack-t_sfinfo.
    CLEAR rt_stack-t_ref_field.
    CLEAR rt_stack-t_dep_field.
    CLEAR rt_stack-flg_selections.
    CLEAR rt_stack-flg_called.
    CLEAR rt_stack-flg_endlist.
    CLEAR rt_stack-flg_endlist.
    CLEAR rt_stack-listtype.
    CLEAR rt_stack-actcols.

    CLEAR rt_stack2-flg_top_of_page.
    CLEAR rt_stack2-s_header.
    CLEAR rt_stack2-s_subtot_options.
    CLEAR rt_stack2-t_suminfo.
    CLEAR rt_stack2-t_sfsave.
    CLEAR rt_stack2-t_sfinfo_slave.
    CLEAR rt_stack2-t_nosum.
    CLEAR rt_stack2-t_groups.
    CLEAR rt_stack2-flg_subtot.
    CLEAR rt_stack2-flg_grouplevels.
    CLEAR rt_stack2-flg_groupedit.
    CLEAR rt_stack2-flg_subtot.
    CLEAR rt_stack2-flg_calculate_totals.
    CLEAR rt_stack2-flg_output_in_progress.
    CLEAR rt_stack2-sumlevel.
    CLEAR rt_stack2-memory_stack1.
    CLEAR rt_stack2-memory_stack2.
    CLEAR rt_stack2-memory_outtab.
    CLEAR rt_stack2-t_colmark.
    CLEAR rt_stack2-t_rowmark.
    CLEAR rt_stack2-it_event_exit.
    CLEAR rt_stack2-flg_append_mode.

    CLEAR rt_stack3.

    rt_stack-i_callback_program = gv_callback_program1 .
    rt_stack-i_callback_user_command = gv_callback_user_command1 .
    rt_stack-i_callback_top_of_page = gv_callback_top_of_page1 .
    rt_stack-i_callback_top_of_list = gv_callback_top_of_list1 .
    IF r_listtype NE c_con_listtype-hier_sequ.
    rt_stack-i_callback_end_of_page = gv_callback_end_of_page1 .
    ELSE.
    CLEAR rt_stack-i_callback_end_of_page.
    ENDIF.
    rt_stack-i_callback_end_of_list = gv_callback_end_of_list1 .
    rt_stack-i_callback_pf_status_set = gv_callback_pf_status_set1 .
    rt_stack-i_callback_fieldcat_save = gv_callback_fieldcat_save1 .
    rt_stack-i_callback_layout_save = gv_callback_layout_save1 .
    rt_stack2-i_callback_list_modify = gv_callback_list_modify1 .
    rt_stack2-i_callback_grouplevel_change =
    gv_callback_grouplevel_change1 .
    rt_stack2-i_callback_before_line_output =
    gv_callback_before_line_otput1 .
    rt_stack2-i_callback_after_line_output =
    gv_callback_after_line_otput1 .
    rt_stack2-i_callback_foreign_top_of_page =
    gv_callback_foreign_top_of_pg1 .
    rt_stack2-i_callback_foreign_end_of_page =
    gv_callback_foreign_end_of_pg1 .
    rt_stack2-i_callback_top_of_coverpage =
    gv_callback_top_of_coverpge1 .
    rt_stack2-i_callback_end_of_coverpage =
    gv_callback_end_of_coverpge1 .
    IF r_listtype EQ c_con_listtype-hier_sequ.
    rt_stack2-i_callback_item_data_expand =
    gv_callback_item_data_expand1 .
    ELSE.
    CLEAR rt_stack2-i_callback_item_data_expand.
    ENDIF.
    rt_stack2-i_callback_header_transport =
    gv_callback_header_transport1 .
    rt_stack2-i_callback_reprep_sel_modify =
    gv_callback_reprep_sel_modify1 .
    IF r_listtype NE c_con_listtype-hier_sequ.
    rt_stack2-i_callback_subtotal_text =
    gv_callback_subtotal_text1 .
    ELSE.
    CLEAR rt_stack2-i_callback_subtotal_text.
    ENDIF.
    rt_stack-i_tabname = r_master .
    rt_stack-i_tabname_slave = r_slave .
    rt_stack-is_keyinfo = r_keyinfo .
    rt_stack-listtype = r_listtype .
    rt_stack-is_layout = gv_layout1 .
    rt_stack-it_fieldcat = gv_fieldcat1 .
    rt_stack-it_excluding = gv_excluding1 .
    rt_stack-it_except_qinfo = gv_except_qinfo1 .

    PERFORM add_default_tooltips.
    SORT rt_stack-it_except_qinfo BY type value.

    rt_stack-i_fctype = gv_fctype1 .
    IF NOT gv_callback_pf_status_set1 EQ space.
    rt_stack-flg_default_status = ' ' .
    ELSE.
    rt_stack-flg_default_status = 'X' .
    ENDIF.
    rt_stack-it_special_groups = gv_special_groups1 .
    rt_stack-it_sort = gv_sort1 .
    rt_stack-t_filter = gv_filter1 .
    rt_stack2-it_event_exit = gv_event_exit1 .
    rt_stack-is_sel_hide = gv_sel_hide1 .
    rt_stack2-is_reprep_id = gv_reprep_id1 .

    IF NOT gv_screen_start_column1 IS INITIAL.
    rt_stack-flg_popup = 'X'.
    ELSE.
    rt_stack-flg_popup = ' '.
    ENDIF.

    rt_stack-is_list = i_list.

    ***ALV Customer Wrapper
    IF i_oo_alv IS NOT INITIAL.
    rt_stack3-r_grid ?= i_oo_alv.
    ENDIF.

    rt_stack3-r_salv_adapter = gv_salv_adapter1.
    rt_stack3-suppress_empty_data = gv_suppress_empty_data1.

    ENDFORM. " GLOBALS_PUSH

    *&---------------------------------------------------------------------*
    *& Form ADD_DEFAULT_TOOLTIPS
    *&---------------------------------------------------------------------*
    * text
    *----------------------------------------------------------------------*
    * --> p1 text
    * <-- p2 text
    *----------------------------------------------------------------------*
    FORM add_default_tooltips .
    * set a standard info for emphasized columns if info for this color has not been set
    DATA: wa_qinfo TYPE lvc_s_qinf.

    READ TABLE it_stack-it_except_qinfo TRANSPORTING NO FIELDS
    WITH KEY type = cl_salv_tooltip=>c_type_color
    value = '4'.
    IF sy-subrc NE 0.
    wa_qinfo-type = cl_salv_tooltip=>c_type_color.
    wa_qinfo-value = '4'.
    wa_qinfo-text = 'Highlighted'(001)." text-301.
    APPEND wa_qinfo TO it_stack-it_except_qinfo.
    ENDIF.
    ENDFORM. " ADD_DEFAULT_TOOLTIPS

    ***Method EXECUTE***

    METHOD execute.


    TYPES: BEGIN OF ty_range,
    sign TYPE tvarv_sign, " sign
    option TYPE tvarv_opti, " option
    low TYPE tvarv_val, " low value
    high TYPE tvarv_val, " high value
    END OF ty_range.

    DATA : it_range TYPE STANDARD TABLE OF ty_range,
    wa_range TYPE ty_range.

    FIELD-SYMBOLS:
    <ls_xml_choice> TYPE if_salv_bs_xml=>s_type_xml_choice.

    DATA:
    lr_data TYPE REF TO data,
    exit TYPE char01,
    lt_xml_choice TYPE if_salv_bs_xml=>t_type_xml_choice.

    CREATE DATA lr_data TYPE if_salv_bs_xml=>s_type_xml_choice.
    ASSIGN lr_data->* TO <ls_xml_choice>.

    *...get available Speadsheet Formats
    lt_xml_choice = zcl_salvexport_xml=>get_gui_spreadsheet_formats( ).
    IF zcl_salvexport_xml=>gui_type IS INITIAL.
    zcl_salvexport_xml=>gui_type = gui_type.
    ENDIF.
    IF zcl_salvexport_xml=>gui_type NE zcl_salvexport_xml=>c_gui_type_windows.
    DELETE lt_xml_choice WHERE xml_type EQ if_salv_bs_xml=>c_type_xxl.
    ENDIF.



    CLEAR wa_range.
    REFRESH it_range[].
    wa_range-sign = 'I'. " sign
    wa_range-option = 'CP'. " option
    wa_range-low = 'Excel*Office*XLSX*' . " low value
    APPEND wa_range TO it_range.
    CLEAR wa_range.

    DELETE lt_xml_choice WHERE text NOT IN it_range.

    IF lt_xml_choice IS NOT INITIAL.
    READ TABLE lt_xml_choice ASSIGNING <ls_xml_choice> INDEX 1.
    IF sy-subrc = 0.
    <ls_xml_choice>-gui_type = if_salv_bs_xml=>c_gui_type_gui.
    APPEND <ls_xml_choice> TO value.
    ENDIF.

    ENDIF.
    IF <ls_xml_choice> IS ASSIGNED.
    UNASSIGN <ls_xml_choice>.
    ENDIF.
    REFRESH it_range[].

    ENDMETHOD.

    ******Method DOWNLOAD***

    METHOD download.

    DATA:
    l_frontend TYPE lvc_front,
    l_default_extension TYPE string,
    l_initial_directory TYPE string,
    l_mode TYPE flag,
    l_length TYPE i,
    l_default_file_name TYPE string,
    l_ok TYPE c,
    * l_length TYPE i,
    l_xml_stream TYPE xml_rawdata.

    DATA: s_title TYPE string,
    s_mask TYPE char255,
    s_mask1 TYPE string,
    l_user_action TYPE i.

    *... get default extension
    CALL METHOD cl_alv_bds=>create_mask_for_filefilter
    EXPORTING
    i_frontend = s_xml_choice-frontend
    IMPORTING
    e_default_extension = l_default_extension
    CHANGING
    c_mask = s_mask.

    s_mask1 = s_mask.

    *... determine PC appl.: only possible in Windows GUI due to REGEX calls
    IF zcl_salvexport_xml=>gui_type
    EQ zcl_salvexport_xml=>c_gui_type_windows.
    DATA: l_application TYPE string.

    l_application = cl_salv_bs_xml_utils=>get_pc_application(
    s_xml_choice-frontend ).
    ELSE.
    IF s_xml_choice-frontend EQ cl_alv_bds=>mc_mhtml_frontend.
    l_default_extension = cl_alv_bds=>mc_excel_extension.
    ENDIF.
    ENDIF.

    CLEAR l_default_file_name.
    CONCATENATE i_filename l_default_extension INTO l_default_file_name.

    *... call File download Dialog and download file


    IF NOT l_default_file_name IS INITIAL.
    CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
    buffer = xml
    IMPORTING
    output_length = l_length
    TABLES
    binary_tab = l_xml_stream.

    CALL METHOD cl_gui_frontend_services=>gui_download
    EXPORTING
    bin_filesize = l_length
    filetype = 'BIN'
    filename = l_default_file_name
    CHANGING
    data_tab = l_xml_stream
    EXCEPTIONS
    OTHERS = 1.
    IF sy-subrc <> 0.
    * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    ELSE.
    sy-subrc = 1.
    ENDIF.

    ENDMETHOD.





    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 26, 2014 at 10:01 AM

    If you really need to export to .xlsx with all the formatting and stuff, use abap2xlsx as mentioned. If all you need is the table form output (headers / rows), use .csv instead. XLSX integration is very weak in ABAP and you need 3rd party tools in most cases. There is one - openxml 4 sap, you can google it

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 26, 2014 at 12:35 PM

    Hi,

    Please specify the source that you are trying to to download from. If you are downloading already uploaded document it will just save as it is if you use gui download. If you are exporting from standard ALV, most of new ALV report has Export to XLSX option by selecting 'Spreadsheet'. If your program is custom ALV report use CL_SALV_TABLE for ALV display and use to_xml method to get xlsx data.

    Thanks,

    Shajahan.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Murali, as already mentioned, xlsx is a different format. When you save a file from SAP, it does not matter what kind of extension you put to the file, you can even name it filename.abc. Once you are using GUI_DOWNLOAD and your data are not binary (BIN), but a text data (ASC), SAP will save the file in the same way it would do if you named the extension .csv or .xls.

      When excel opens .xls, it knows it could have been in a simple .csv format and it is able to open it either way (it will even open the file if you open .abc file directly from excel).

      XLSX however is a different approach and when excel opens such a file, it expects the file to be a zipped xmls (just try to rename any .xlsx you have to .zip and unzip that file). So it has to be in binary format when exported from SAP. And this is the difference, why you can't just simply select data in your ABAP into a internal table and send it to .xlsx. The same reason is you can't simply rename files that worked under .xls to .xlsx, because excel will not be able to open such a file.

      If the customer needs to export some kind of data to both formats, you need to implement different logic for both. Nabheet already mentioned you one class to check. Other opportunities (abap2xlsx, openxml4abap) were also mentioned. You need to go one of those ways.