Upload data to Excel template

The function EXPORT_TO_SPREADSHEET gives you a possibility to fill an existing Excel file. You can use such file as template and format it previously – setup colors, borders, etc.

Remember: the Excel file has to exist before program is executed. The data must be transferred to an existing and valid Excel file. The function EXPORT_TO_SPREADSHEET doesn’t create the file, it only transfers data.

REPORT zexcel_fill.

TYPE-POOLS:
  soi.

DATA:
  lv_file      TYPE string VALUE 'c:tmptest.xlsx',
  lv_url(256)  TYPE c,
  ls_range     TYPE soi_dimension_item,
  lt_range     TYPE soi_dimension_table,
  lt_excel     TYPE soi_generic_table,
  ls_excel     TYPE soi_generic_item.

CONCATENATE 'file://' lv_file INTO lv_url.

ls_range-row     = 1.
ls_range-column  = 1.
ls_range-rows    = 2.
ls_range-columns = 2.
APPEND ls_range TO lt_range.

ls_excel-row = '1'. ls_excel-column = '1'. ls_excel-value = 'A'. APPEND ls_excel TO lt_excel.
ls_excel-row = '1'. ls_excel-column = '2'. ls_excel-value = 'B'. APPEND ls_excel TO lt_excel.
ls_excel-row = '2'. ls_excel-column = '1'. ls_excel-value = 'C'. APPEND ls_excel TO lt_excel.
ls_excel-row = '2'. ls_excel-column = '2'. ls_excel-value = 'D'. APPEND ls_excel TO lt_excel.

CALL FUNCTION 'EXPORT_TO_SPREADSHEET'
  EXPORTING
    item_url      = lv_url
    document_type = 'Excel.Sheet'
  TABLES
    data_table    = lt_excel
    ranges        = lt_range.

WRITE: / 'OK'.

See other related notes on my website:

Scroll to Top