How To Upload And Download Excel Template in SAP

Tutorial Objectives
1. Able to upload and download formatted EXCEL template using ABAP
2. Present the user with a nice looking Excel Template
Prerequisites
SAP System Access: SAP GUI, ECC
Authorization: Developer role, TCODE: SE38

In a previous project I worked on, the user requested functionality to download a template from SAP so they can fill it with some records, and upload it back to SAP for processing. Well I think as a developer we get this request a lot, so what’s the problem?

The solution we often use involves creating an internal table structure, defining the header, and then utilizing the GUI_DOWNLOAD function module to download the template, as demonstrated below.

*&---------------------------------------------------------------------*
*& Report Z_DOWNLOAD_TEMPL
*&---------------------------------------------------------------------*
*& ABAPGeeks.com
*&---------------------------------------------------------------------*
report z_download_templ.
*& Create a structure
types:
  begin of typ_emp,
    empid   type c length 3,
    fname   type c length 10,
    lname   type c length 10,
    dob     type d,
    address type c length 50,
    city    type c length 10,
    postal  type c length 5,
    phone   type c length 10,
  end of typ_emp,
  begin of typ_hdr,
    line type c length 50,
  end of typ_hdr.

data: it_emp       type table of typ_emp,
      it_hdr       type table of typ_hdr,
      lv_file_name type string.

lv_file_name = 'd:\EmpTemplate.xlsx'. " path and filename

"Define the columns
it_hdr = value #( ( line = 'EMPID' )
                  ( line = 'FIRST_NAME' )
                  ( line = 'LAST_NAME' )
                  ( line = 'DOB' )
                  ( line = 'ADDRESS' )
                  ( line = 'CITY' )
                  ( line = 'POSTAL' )
                  ( line = 'PHONE' )  ).

"Download the internal table and the header
call function 'GUI_DOWNLOAD'
  exporting
    filename              = lv_file_name
    filetype              = 'ASC'
    write_field_separator = 'X'
  tables
    data_tab              = it_emp
    fieldnames            = it_hdr
  exceptions
    others                = 1.

message 'Template downloaded!' type 'S'.

Here’s the XLS result!

But there is no color, no formatting, just a plain XLS file, it’s kind of boring right? I want to download template that will look like this!

It’s actually super easy thanks to SAP Web Repository, now follow these steps to have a nice looking template like that!

Uploading XLS Template To SAP Web Repository

Step 1. The first step is to create the XLS template and save the file somewhere in your local folder. For this exercise, I saved the template as emp_template.

Step 2. Execute TCODE: SMW0

Step 3. Choose the Binary data for WebRFC applications and then click the find button on the toolbar.

Step 4. Click the Execute Button

Step 5. On the toolbar, click the Create button.

Step 6. Fill in the Obj. name and Description field, next click the Import Button

Step 7. Select the XLS template you saved in step 1 (emp_template).

Step 8. Fill in the transport request fields.

Step 9. Next you will see the object has been created in SAP Web Repository.

The Obj. name = Z_EMP_EMPL will be our Obj_ID in our ABAP program. Next let’s download it using ABAP.

Download XLS Template From SAP Web Repository using ABAP

Copy this source code into your program.


data:lv_objid type wwwdata-objid,
     lwa_data type wwwdatatab,
     lv_subrc type sy-subrc,
     lv_msg   type string,
     lv_file  type rlgrap-filename.

lv_objid = 'Z_EMP_TEMPL'. "This is the Obj.Name in the SAP Web repository
lv_file = 'd:/emp_templ_web.xls'.

select single
  relid,
  objid
from wwwdata
  into @data(lwa_rec)
where srtf2 = 0 "the first record in the table with the same objid
  and relid = 'MI'
  and objid = @lv_objid.

lwa_data = CORRESPONDING #( lwa_rec ).

call function 'DOWNLOAD_WEB_OBJECT'
  exporting
    key         = lwa_data
    destination = lv_file
  importing
    rc          = lv_subrc.
if lv_subrc <> 0.
endif.

MESSAGE 'Downloaded!' TYPE 'S'.

Now say good bye to that old plain template.

Share this tutorial with Fellow Geeks!

Other ABAP Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *