Function Module To Upload Data From
Excel File Into Two Internal Tables
SDN Community Contribution
(This is not an official SAP document.)
Disclaimer & Liability Notice
This document may discuss sample coding or other information that does not include SAP official interfaces
and therefore is not supported by SAP. Changes made based on this information are not supported and can
be overwritten during an upgrade.
SAP will not be held liable for any damages caused by using or misusing the information, code or methods
suggested in this document, and anyone using these methods does so at his/her own risk.
SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of
this technical article or code sample, including any liability resulting from incompatibility between the content
within this document and the materials and services offered by SAP. You agree that you will not hold, or seek
to hold, SAP responsible or liable with respect to the content of this document.
© 2005 SAP AG
1
Function Module To Upload Data From
Excel File Into Two Internal Tables
Applies To:
SAP 4.6 C
SAP 4.7 EE
Summary
To upload data from two excel sheets of an excel workbook into two internal tables.
While developing conversion objects, batch data communications & data transfers from legacy to different
SAP R/3 systems, we came through a requirement. The requirement was:
Requirement:
There will be 2 excel sheets in an excel workbook.
•
Configuration Sheet
•
Data Sheet
Configuration Sheet contains user validation logics according to Sales Area (For Sales & Distribution) and
Plant (For Material Management). Contents of this sheet only changes along with Sales Area or Plant.
Data Sheet contains data required to create a Material/Sales Document.
Requirement Analysis:
Two internal tables with different structures are created.
IT_CONFIG
IT_DATA
Upload the data present in Configuration Sheet to IT_CONFIG and Data Sheet to IT_DATA. Now validate the
IT_DATA internal table contents with respect to the user defined validation logics present in IT_CONFIG. Also
create another internal table with structure same as IT_DATA to hold the data which are failed to validate.
Remove the failed records from IT_DATA and then submit the contents to BAPI.
Scenario/Example of Usage: One simple scenario / example of usage is while creating a material, if we need to create 100 materials for
one sales area, take sales area in first sheet and 100 materials in second sheet which is also called as data
© 2005 SAP AG
2
Function Module To Upload Data From
Excel File Into Two Internal Tables
sheet. No need of placing sales area 100 times just based on first sheet configuration pick the data from
second sheet.
For these types of similar scenarios as per requirement, we can use this function module.
Technical Description:
I used this function module for creating material using Business Application Programming Interface.
For creating a specific type of material there is a need to define some validation expressions on the first sheet
called as ‘Configuration Sheet’ and data for that is maintained in the second sheet also called as ‘Data Sheet’.
Based on the logic of success/failure for the material creation the data will be passed from second sheet to a
final internal table, so there is need of interacting two sheets at a time rather going for two input file, as I mean
it increase the performance of my object.
So this function module can be used for any scenarios mostly come across in data transfer or data
conversion.
Moreover, we can do validations on different table entries maintained in the file. To be more specific used to
check the tables entries/values which user has given or extracted from select statements.
Likewise we can use this FM for many related scenarios.
It is been developed based on SAP defined function module ALSM_EXCEL_TO_INTERNAL_TABLE by
working on OLE concepts.
By: Mirza Ifthekhar Baig
Company and Title: KTree Computer Solutions India (P) Ltd. Hyderabad, (A.P.), India.
Date: 6th January 2006
© 2005 SAP AG
3
Function Module To Upload Data From
Excel File Into Two Internal Tables
Code Sample
FUNCTION Z_MIRZA_UPLOADING_FROM_2SHEETS.
*"---------------------------------------------------------------------*"*"Local interface:
*" IMPORTING
*"
VALUE(FILE_NAME) LIKE RLGRAP-FILENAME
*"
VALUE(START_ROW_SHEET1) TYPE I
*"
VALUE(START_COLUMN_SHEET1) TYPE I
*"
VALUE(START_ROW_SHEET2) TYPE I
*"
VALUE(START_COLUMN_SHEET2) TYPE I
*"
VALUE(END_ROW_SHEET1) TYPE I
*"
VALUE(END_COLUMN_SHEET1) TYPE I
*"
VALUE(END_ROW_SHEET2) TYPE I
*"
VALUE(END_COLUMN_SHEET2) TYPE I
*" TABLES
*"
IT_DATA1 STRUCTURE ALSMEX_TABLINE
*"
IT_DATA2 STRUCTURE ALSMEX_TABLINE
*" EXCEPTIONS
© 2005 SAP AG
4
Function Module To Upload Data From
Excel File Into Two Internal Tables
*"
INCONSISTENT_PARAMETERS
*"
UPLOAD_OLE
*"----------------------------------------------------------------------
*
DATA DECLARATION
DATA: excel_tab
excel_tab1
TYPE ty_t_sender,
TYPE ty_t_sender.
DATA: ld_separator TYPE c.
DATA: application TYPE ole2_object,
workbook
TYPE ole2_object,
SHEET
TYPE OLE2_OBJECT,
range
TYPE ole2_object,
worksheet
TYPE ole2_object.
DATA: h_cell
TYPE ole2_object,
h_cell1
TYPE ole2_object.
DATA: ld_rc
TYPE i.
*
MESSAGE DEFINATION
DEFINE m_message.
© 2005 SAP AG
5
Function Module To Upload Data From
Excel File Into Two Internal Tables
case sy-subrc.
when 0.
when 1.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
when others. raise upload_ole.
endcase.
END-OF-DEFINITION.
*
PARAMETER CHECK
IF START_ROW_SHEET1 > END_ROW_SHEET1.
RAISE inconsistent_parameters.
ENDIF.
IF START_COLUMN_SHEET1 > END_COLUMN_SHEET1.
RAISE inconsistent_parameters.
ENDIF.
IF START_ROW_SHEET2 > END_ROW_SHEET2.
RAISE inconsistent_parameters.
© 2005 SAP AG
6
Function Module To Upload Data From
Excel File Into Two Internal Tables
ENDIF.
IF START_COLUMN_SHEET2 > END_COLUMN_SHEET2.
RAISE inconsistent_parameters.
ENDIF.
CLASS cl_abap_char_utilities DEFINITION LOAD.
ld_separator = cl_abap_char_utilities=>horizontal_tab.
*
OPENING EXCEL FILE
IF application-header = space OR application-handle = -1.
CREATE OBJECT application 'Excel.Application'.
m_message.
ENDIF.
CALL METHOD OF APPLICATION 'Workbooks' = WORKBOOK.
m_message.
CALL METHOD OF application
'Workbooks' = workbook.
m_message.
CALL METHOD OF workbook 'Open'
EXPORTING #1 = FILE_NAME.
m_message.
CALL METHOD OF APPLICATION 'Worksheets' = SHEET EXPORTING #1 = 1.
© 2005 SAP AG
7
Function Module To Upload Data From
Excel File Into Two Internal Tables
m_message.
CALL METHOD OF APPLICATION 'Worksheets' = SHEET EXPORTING #1 = 1.
m_message.
CALL METHOD OF SHEET 'Activate'.
m_message.
GET PROPERTY OF application 'ACTIVESHEET' = sheet.
m_message.
*
MARKING OF WHOLE SPREADSHEET
CALL METHOD OF sheet 'Cells' = h_cell
EXPORTING #1 = START_ROW_SHEET1 #2 = START_COLUMN_SHEET1.
m_message.
CALL METHOD OF sheet 'Cells' = h_cell1
EXPORTING #1 = END_ROW_SHEET1 #2 = END_COLUMN_SHEET1.
m_message.
CALL METHOD OF sheet 'RANGE' = range
EXPORTING #1 = h_cell #2 = h_cell1.
m_message.
CALL METHOD OF range 'SELECT'.
m_message.
© 2005 SAP AG
8
Function Module To Upload Data From
Excel File Into Two Internal Tables
* Copy marked area (SHEET1) into Clippboard
CALL METHOD OF range 'COPY'.
m_message.
* Read clipboard into ABAP
CALL METHOD cl_gui_frontend_services=>clipboard_import
IMPORTING
data
= excel_tab
EXCEPTIONS
cntl_error
=1
*
ERROR_NO_GUI
*
NOT_SUPPORTED_BY_GUI = 3
OTHERS
=2
=4
.
IF sy-subrc <> 0.
MESSAGE a037(alsmex).
ENDIF.
PERFORM separated_to_intern_convert TABLES excel_tab IT_DATA1
USING ld_separator.
© 2005 SAP AG
9
Function Module To Upload Data From
Excel File Into Two Internal Tables
* Clear the clipboard
REFRESH excel_tab.
CALL METHOD cl_gui_frontend_services=>clipboard_export
IMPORTING
data
= excel_tab
CHANGING
rc
= ld_rc
EXCEPTIONS
cntl_error
=1
*
ERROR_NO_GUI
*
NOT_SUPPORTED_BY_GUI = 3
OTHERS
=2
=4
.
* Working in Second Excel Work Sheet
CALL METHOD OF APPLICATION 'Worksheets' = SHEET EXPORTING #1 = 2.
m_message.
CALL METHOD OF SHEET 'Activate'.
m_message.
© 2005 SAP AG
10
Function Module To Upload Data From
Excel File Into Two Internal Tables
GET PROPERTY OF application 'ACTIVESHEET' = sheet.
m_message.
* Mark Sheet2
CALL METHOD OF sheet 'Cells' = h_cell
EXPORTING #1 = START_ROW_SHEET2 #2 = START_COLUMN_SHEET2.
m_message.
CALL METHOD OF sheet 'Cells' = h_cell1
EXPORTING #1 = END_ROW_SHEET2 #2 = END_COLUMN_SHEET2.
m_message.
CALL METHOD OF sheet 'RANGE' = range
EXPORTING #1 = h_cell #2 = h_cell1.
m_message.
CALL METHOD OF range 'SELECT'.
m_message.
* Copy Marked Area (Sheet2) into Clippboard
CALL METHOD OF range 'COPY'.
m_message.
© 2005 SAP AG
11
Function Module To Upload Data From
Excel File Into Two Internal Tables
* Read Clipboard into ABAP
CALL METHOD cl_gui_frontend_services=>clipboard_import
IMPORTING
data
= excel_tab1
EXCEPTIONS
cntl_error
=1
*
ERROR_NO_GUI
*
NOT_SUPPORTED_BY_GUI = 3
OTHERS
=2
=4
.
IF sy-subrc <> 0.
MESSAGE a037(alsmex).
ENDIF.
PERFORM separated_to_intern_convert TABLES excel_tab1 IT_DATA2
USING ld_separator.
* Clear Clipboard
REFRESH excel_tab.
CALL METHOD cl_gui_frontend_services=>clipboard_export
© 2005 SAP AG
12
Function Module To Upload Data From
Excel File Into Two Internal Tables
IMPORTING
data
= excel_tab1
CHANGING
rc
= ld_rc
EXCEPTIONS
cntl_error
=1
*
ERROR_NO_GUI
*
NOT_SUPPORTED_BY_GUI = 3
OTHERS
*
Leaving Application
=2
=4
.
CALL METHOD OF application 'QUIT'.
m_message.
FREE OBJECT application.
m_message.
ENDFUNCTION.
Author Bio
Mirza Ifthekhar Baig is an SAP-ABAP consultant for KTree Computer Solutions India (P) Ltd. since
June 2005.
© 2005 SAP AG
13