How To Export Excel File With Template
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.
Page 1 of 16
7) ABAP Program
Page 3 of 16
Handling Data in Excel In-place Display Using BDS
By Satyajit Mohapatra, HCL Technologies Ltd
The article demonstrates data handling in excel in-place display using BDS with the help of a program. The demo program
maintains the entries in a database table through an excel in-place display.
OVERVIEW
MS Excel is the conventional way of storing and maintaining data. Sometimes, user prefers to display report output in
specific MS Excel templates; which contain logos, user specific table formats, engineering drawings, diagrams and macros.
The data modified manually in these excel templates may be again transferred to SAP for processing.
Excel integration is required due to various reasons like avoiding user training on newly developed custom programs and
screens, use existing data templates, data integration with legacy system.
BDS (Business Document Services) is a convenient option for excel integration as user specific MS Excel templates can be
stored in it. These templates can be called in an ABAP program at runtime to display data. Also, the data modified by the
user in MS Excel can be read into ABAP program for processing.
The functionality will be demonstrated through a demo program. The program will display the content of a custom table in
excel in-place display. The user can change the non key fields displayed and the modified contents will be updated to the
table after validation.
1. Defining a B DS Class
A custom BDS class can be defined through transaction SBDSV1 as described below. An existing BDS class can be used,
unless the user wants a separate class for a specific application.
Enter the ‘Class name’, ‘Class type’ as ‘Other objects(OT)’, ‘Log Level’
as required and rest of the parameters should filled as shown below.
Design a template as per user requirement in MS Excel. You can embed all static objects/data to be displayed such as logos,
drawings, headers etc in the template, except the area, where the data will be filled at runtime.
Page 4 of 16
A sample template has been created as shown below.
Now, go to ‘Create’ tab and double click on table template. It will show a pop up to upload the MS Excel template.
Page 5 of 16
The uploaded table template can be attached to a transport request as well.
The program will maintain a custom table YSM_AGENTS, which has the following fields.
Page 6 of 16
Create a screen ‘0100’ and a custom control ‘EXCEL’ in it to display the excel document in-place. Also, activate
the BACK, EXIT, CANCEL, SAVE options in GUI status.
*&---------------------------------------------------------------------*
*& Report YSM_TEST5
*&---------------------------------------------------------------------*
*& Demo program for displaying table data in a specific excel template
*& using BDS. Also, reads the contents modified by user again into ABAP
*& program after validations and updates the table.
*&---------------------------------------------------------------------*
REPORT ysm_test5.
************************************************************************
* Data Declaration
************************************************************************
* Custom Table With 3 fields
*->AGENTID (KEY)
*->NAME
*->EMAIL
TABLES: ysm_agents.
TYPE-POOLS: soi,
sbdst.
************************************************************************
* Selection Screen
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
* User will enter the agent ids to be modified
SELECT-OPTIONS: s_agent FOR ysm_agents-agentid OBLIGATORY.
************************************************************************
* START OF SELECTION
************************************************************************
START-OF-SELECTION.
* Call Excel Inplace Display
CALL SCREEN 100. "Create a screen 100 with custom container 'EXCEL'
************************************************************************
* SCREEN LOGIC
************************************************************************
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
CASE sy-ucomm.
WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
* Close document
PERFORM f_close_document.
LEAVE TO SCREEN 0.
WHEN 'SAVE'.
* Save the modified entries into database
PERFORM f_save_document.
ENDCASE.
IF sy-subrc NE 0.
MESSAGE 'No Agent Details Found' TYPE 'E'.
ENDIF.
*&---------------------------------------------------------------------*
*& Form f_open_document
*&---------------------------------------------------------------------*
* Open the table template from BDS
*----------------------------------------------------------------------*
* --> l_clsnam Class Name in OAOR
* --> l_clstyp Class Type in OAOR
* --> l_objkey Object key in OAOR
* --> l_desc Description of the excel template in OAOR
*----------------------------------------------------------------------*
FORM f_open_document USING l_clsnam TYPE sbdst_classname
l_clstyp TYPE sbdst_classtype
l_objkey TYPE sbdst_object_key
l_desc TYPE char255.
Page 8 of 16
retcode = wf_retcode.
IF wf_retcode NE c_oi_errors=>ret_ok.
CALL METHOD c_oi_errors=>raise_message
EXPORTING
type = 'E'.
ENDIF.
IF wf_retcode NE c_oi_errors=>ret_ok.
CALL METHOD c_oi_errors=>raise_message
EXPORTING
type = 'E'.
ENDIF.
IF sy-subrc NE 0.
MESSAGE 'Error Retrieving Document' TYPE 'E'.
ENDIF.
IF wf_retcode NE c_oi_errors=>ret_ok.
CALL METHOD c_oi_errors=>show_message
EXPORTING
type = 'E'.
ENDIF.
* Open Document
CALL METHOD r_proxy->open_document
EXPORTING
document_url = locwa_uris-uri
open_inplace = abap_true
protect_document = abap_true "Protect Document initially
IMPORTING
retcode = wf_retcode.
IF wf_retcode NE c_oi_errors=>ret_ok.
Page 9 of 16
CALL METHOD c_oi_errors=>show_message
EXPORTING
type = 'E'.
ENDIF.
IF wf_retcode NE c_oi_errors=>ret_ok.
CALL METHOD c_oi_errors=>show_message
EXPORTING
type = 'E'.
ENDIF.
*&---------------------------------------------------------------------*
*& Form f_dis_table_data
*&---------------------------------------------------------------------*
* Display data in table template
*----------------------------------------------------------------------*
FORM f_dis_table_data .
IF r_error->has_failed = abap_true.
CALL METHOD r_error->raise_message
EXPORTING
type = 'E'.
ENDIF.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
Page 10 of 16
* Insert the table entries into Excel
CALL METHOD r_excel->insert_one_table
EXPORTING
fields_table = locint_fields[] "Defn of fields
data_table = int_agents[] "Data
rangename = 'AGENTS' "Range Name
IMPORTING
error = r_error
retcode = wf_retcode.
IF r_error->has_failed = abap_true.
CALL METHOD r_error->raise_message
EXPORTING
type = 'E'.
ENDIF.
*&---------------------------------------------------------------------*
*& Form f_protect_sheet
*&---------------------------------------------------------------------*
* Protect the whole sheet except the fields to edited
*----------------------------------------------------------------------*
FORM f_protect_sheet .
IF r_error->has_failed = abap_true.
CALL METHOD r_error->raise_message
EXPORTING
type = 'E'.
ENDIF.
IF r_error->has_failed = abap_true.
CALL METHOD r_error->raise_message
EXPORTING
type = 'E'.
ELSE.
* If not protected, protect the sheet
IF loc_protect NE abap_true.
CALL METHOD r_excel->protect
EXPORTING
protect = abap_true
IMPORTING
error = r_error
retcode = wf_retcode.
IF r_error->has_failed = abap_true.
CALL METHOD r_error->raise_message
EXPORTING
type = 'E'.
ENDIF.
ENDIF.
ENDIF.
Page 11 of 16
* Unprotect the range for editing
CALL METHOD r_excel->protect_range
EXPORTING
name = 'EDIT'
protect = space
IMPORTING
error = r_error
retcode = wf_retcode.
IF r_error->has_failed = abap_true.
CALL METHOD r_error->raise_message
EXPORTING
type = 'E'.
ENDIF.
IF r_error->has_failed = abap_true.
CALL METHOD r_error->raise_message
EXPORTING
type = 'E'.
ENDIF.
* Close document
IF NOT r_proxy IS INITIAL.
CALL METHOD r_proxy->close_document
IMPORTING
error = r_error
retcode = wf_retcode.
IF r_error->has_failed = abap_true.
CALL METHOD r_error->raise_message
EXPORTING
type = 'E'.
ENDIF.
ENDIF.
*&---------------------------------------------------------------------*
*& Form f_save_document
*&---------------------------------------------------------------------*
* Save the modified entries into database table
*----------------------------------------------------------------------*
FORM f_save_document .
Page 12 of 16
retcode = wf_retcode.
IF r_error->has_failed = abap_true.
CALL METHOD r_error->raise_message
EXPORTING
type = 'E'.
ENDIF.
IF r_error->has_failed = abap_true.
CALL METHOD r_error->raise_message
EXPORTING
type = 'E'.
ENDIF.
AT END OF row.
locwa_agents_mod-mandt = sy-mandt.
APPEND locwa_agents_mod TO locint_agents_mod.
CLEAR locwa_agents_mod.
ENDAT.
ENDLOOP.
* Update Table
MODIFY ysm_agents FROM TABLE locint_agents_mod.
COMMIT WORK.
IF sy-subrc EQ 0.
MESSAGE 'DATA UPDATED' TYPE 'S'.
ELSE.
MESSAGE 'DATA NOT UPDATED' TYPE 'E'.
ENDIF.
*&---------------------------------------------------------------------*
*& Form f_validate_email
*&---------------------------------------------------------------------*
* Validate the email id entered
*----------------------------------------------------------------------*
* -->l_email Email Id
*----------------------------------------------------------------------*
FORM f_validate_email USING l_email TYPE c
l_err_row TYPE i.
TYPE-POOLS:sx.
DATA: locwa_address TYPE sx_address.
* Check Email Id
locwa_address-type = 'INT'.
locwa_address-address = l_email.
Page 13 of 16
CALL FUNCTION 'SX_INTERNET_ADDRESS_TO_NORMAL'
EXPORTING
address_unstruct = locwa_address
EXCEPTIONS
error_address_type = 1
error_address = 2
error_group_address = 3
OTHERS = 4.
IF sy-subrc <> 0.
IF r_error->has_failed = abap_true.
CALL METHOD r_error->raise_message
EXPORTING
type = 'E'.
ENDIF.
* Define Range
CALL METHOD r_excel->insert_range
EXPORTING
name = l_range
rows = l_row
columns = l_column
IMPORTING
error = r_error.
IF r_error->has_failed = abap_true.
CALL METHOD r_error->raise_message
EXPORTING
type = 'E'.
ENDIF.
Page 14 of 16
Selection Screen:
Shows error message and highlights the cell to be corrected in red, if an invalid email id is entered:
Page 15 of 16
Data Changed and Successfully Saved:
Page 16 of 16