Skip to Content

Simple Function Module to export any internal table to MS Excel

I searched SDN but could not find a ready to use simple function module to download any internal table in Microsoft Excel so decided to create one.

This function module was created using SAP 4.6c. It is very simple to use all you need to do is pass a filename+path, table or structure name and the internal table.

Function module details:

Import parameters


Tables parameters




Source code

FUNCTION z_db001_itab_to_msexcel. *"---------------------------------------------------------------------- *"*"Local interface: *" IMPORTING *" VALUE(I_FILENAME) TYPE RLGRAP-FILENAME *" VALUE(I_TABNAME) TYPE DFIES-TABNAME *" VALUE(I_DESC_TYPE) TYPE CHAR1 DEFAULT 'M' *" TABLES *" I_TB_DATA *" EXCEPTIONS *" ERROR_EXCEL_OLE *" ERROR_GET_DICTIONARY_INFO *"---------------------------------------------------------------------- * REMARK: certain data elements may cause the function * EXCEL_OLE_STANDARD_DAT to crash. * Known data elements to crash function: PS_STUFE * Workaround for PS_STUFE: use a data element with type NUMC instead * of INT * * I have not looked into this problem so be careful when using this * function * declarations DATA: l_filename LIKE rlgrap-filename, wa_x030l LIKE x030l, tb_dfies TYPE STANDARD TABLE OF dfies, wa_dfies TYPE dfies, BEGIN OF tb_flditab OCCURS 0, * field description fldname(40) TYPE c, END OF tb_flditab. * get table/structure field info CALL FUNCTION 'GET_FIELDTAB' EXPORTING langu = sy-langu only = space tabname = i_tabname withtext = 'X' IMPORTING header = wa_x030l TABLES fieldtab = tb_dfies EXCEPTIONS internal_error = 01 no_texts_found = 02 table_has_no_fields = 03 table_not_activ = 04. * check result CASE sy-subrc. WHEN 0. * copy fieldnames LOOP AT tb_dfies INTO wa_dfies. CASE i_desc_type. WHEN 'F'. tb_flditab-fldname = wa_dfies-fieldname. WHEN 'S'. tb_flditab-fldname = wa_dfies-scrtext_s. WHEN 'M'. tb_flditab-fldname = wa_dfies-scrtext_m. WHEN 'L'. tb_flditab-fldname = wa_dfies-scrtext_l. WHEN OTHERS. * use fieldname tb_flditab-fldname = wa_dfies-fieldname. ENDCASE. APPEND tb_flditab. * clear variables CLEAR: wa_dfies. ENDLOOP. WHEN OTHERS. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-subrc raising error_get_dictionary_info. ENDCASE. * export (and open) file to MS-Excel CALL FUNCTION 'EXCEL_OLE_STANDARD_DAT' EXPORTING file_name = i_filename data_sheet_name = 'Data' TABLES data_tab = i_tb_data fieldnames = tb_flditab EXCEPTIONS file_not_exist = 1 filename_expected = 2 communication_error = 3 ole_object_method_error = 4 ole_object_property_error = 5 invalid_filename = 6 invalid_pivot_fields = 7 download_problem = 8 OTHERS = 9. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 raising error_excel_ole. ENDIF. ENDFUNCTION.
1 Comment
You must be Logged on to comment or reply to a post.
  • Hi,
      This gives an insight into where there are a lot of requirements which users need when the data is to be downloaded to Excel and this helps each individual to Explore further.