Skip to Content

How to export excel file with template

Sometimes we may need to export a document in the form of a spreadsheet (MS Excel).

But often the problem of the automatic display by MS Excel.

Before export the data. we can create MS Excel templates and upload to SAP then run template later.

1) Prepare the MS Excel template


2) Upload to SAP with transaction OAOR


     Class Type -> OT


3) Click tab Create then expand all and double click to Table template


4) Chose MS Excel Template


5) Fill Description (Use in ABAP Program)


6) Check MS Excel template


7) ABAP Program


  perform genarate_excel_with_template tables lt_excel

                                        using ‘ExcelTest’



*&      Form  genarate_excel_with_template


form genarate_excel_with_template tables pt_excel    type standard table

                                   using pv_template type bds_propva

                                         pv_file     type localfile.

   data: l_iref_template    type ref to cl_bds_document_set,

         l_oref_container   type ref to cl_gui_custom_container,

         l_iref_control     type ref to i_oi_container_control,

         l_iref_error       type ref to i_oi_error,

         l_iref_document    type ref to i_oi_document_proxy,

         l_iref_spreadsheet type ref to i_oi_spreadsheet,

         l_retcode          type soi_ret_string.

   data: lt_signature       type sbdst_signature,

         lw_signature       type bapisignat,

         lt_uri             type sbdst_uri,

         lw_uri             type bapiuri,

         lt_sheet           type soi_sheets_table,

         lw_sheet           type soi_sheets.

   data: lt_fields          type standard table of rfc_fields,

         lv_last_row        type i,

         lv_last_col        type i.

   call method c_oi_container_control_creator=>get_container_control


       control = l_iref_control

       retcode = l_retcode.

   check l_retcode = c_oi_errors=>ret_ok.

   call method l_iref_control->init_control


       r3_application_name      = pv_template

       inplace_enabled          = ‘X’

       inplace_scroll_documents = ‘X’

       parent                   = l_oref_container


       retcode                  = l_retcode.

   check l_retcode = c_oi_errors=>ret_ok.

   create object l_iref_template.

   lw_signatureprop_name  = ‘DESCRIPTION’.

   lw_signatureprop_value = pv_template.

   append lw_signature to lt_signature.

   refresh lt_uri.

   call method l_iref_template->get_with_url


       classname                = ‘SOFFICEINTEGRATION’

       classtype                = ‘OT’

       object_key               = ‘SOFFICEINTEGRATION’


       uris                     = lt_uri

       signature                = lt_signature


       nothing_found            = 1

       error_kpro               = 2

       internal_error           = 3

       parameter_error          = 4

       not_authorized           = 5

       not_allowed              = 6.

   clear lw_uri.

   read table lt_uri into lw_uri index 1.

   check sysubrc = 0.

   call method l_iref_control->get_document_proxy


       document_type            = ‘Excel.Sheet’


       document_proxy           = l_iref_document

       retcode                  = l_retcode.

   check l_retcode = c_oi_errors=>ret_ok.

   call method l_iref_document->open_document


       document_url = lw_uriuri

       open_inplace = ‘X’


       retcode      = l_retcode.

   check l_retcode = c_oi_errors=>ret_ok.

   free l_iref_error.

   call method l_iref_document->get_spreadsheet_interface


       error           = l_iref_error

       sheet_interface = l_iref_spreadsheet.

   call method l_iref_spreadsheet->get_sheets


       sheets          = lt_sheet

       error           = l_iref_error.

   check l_iref_error->error_code = c_oi_errors=>ret_ok.

   clear lw_sheet.

   read table lt_sheet into lw_sheet index 1.

   check sysubrc = 0.

   call method l_iref_spreadsheet->select_sheet


       name            = lw_sheetsheet_name


       error           = l_iref_error.

   check l_iref_error->error_code = c_oi_errors=>ret_ok.

   refresh lt_fields.

   call function ‘DP_GET_FIELDS_FROM_TABLE’


       data   = pt_excel

       fields = lt_fields.

   lv_last_row = lines( pt_excel ).

   lv_last_col = lines( lt_fields ).

   call method l_iref_spreadsheet->set_selection


       left    = 1

       top     = 2

       rows    = lv_last_row

       columns = lv_last_col.

   call method l_iref_spreadsheet->insert_range


       columns = lv_last_col

       rows    = lv_last_row

       name    = pv_template.

   call method l_iref_spreadsheet->insert_one_table


       data_table   = pt_excel[]

       fields_table = lt_fields

       rangename    = pv_template.

   call method l_iref_document->save_as


       file_name    = pv_file.

   call method l_iref_document->release_document


       retcode = l_retcode.

   free: l_iref_spreadsheet,


   call method l_iref_control->release_all_documents.

   call method l_iref_control->destroy_control.

   message ‘Generate Excel file’ type ‘S’.

endform.                    ” genarate_excel_with_template

You must be Logged on to comment or reply to a post.
    • Hi  Budtawan Vu 

      This article looks interesting, I'm not an SAP ABAP developer, just a SAP user that specifies new functions and tools for our SAP .
      I have one question that seems like it may be related to this somehow.

      Our development team has exported an excel with many lines for the user, the idea is that the user will modify some of the values in that excel then upload that excel back into SAP to update those new values in SAP. that works OK.

      my question is,  Is it possible to export an excel that already includes working formulas in the excel.

      e.g. if the exported excel contains a column with Quantity and user inputs a Price in each cell in excel,  then if such formualrs are actively working in the excel then it could immediately calculate the total amount = QTY x Price  and show this value  in the excel before the user uploads the new price data to SAP.

      The idea is,  that this would allow the user to do some basic and easy sanity checking on the data before uploading

      Of course the user could add in the formulas himself after the download but this is more tricky, because the real formulas used are quite complicated and the users are, lets say, very busy with other tasks and not skilled with writing excel formulas

      ... any ideas that I could pass on to our development team, so far they haven't found a way to add in working formulas ?



  • Hello Budtawan,

    would that example work in background, if not what would you suggest to read a excel template fill it and export in background mode?



  • Hi,

    many thanks for this coding!

    I have implemented it into our system but while processing

       call method l_iref_spreadsheet->insert_one_table


           data_table   = pt_excel[]

           fields_table = lt_fields

           rangename    = pv_template.

    a short dump with system_failure is provided.

    This is raised whith the message: CPIC-CALL: 'ThSAPOCMINIT' : cmRc=17 thRc=223

    Do you have any idea why this dosn't work?`

  • /