23
SAP COMMUNITY NETWORK scn.sap.com © 2012 SAP AG 1 XLSX Upload – a unified approach Applies to: SAP NetWeaver 7.0 Enhancement Package 2 Onwards Summary This document provides a comprehensive introduction about XLSX files and a unified approach to upload XLSX files into SAP GUI and Webdynpro ABAP. This gives you an overview of the following, Key notions in XLSX Unified approach to upload XSLX file APIs used to upload a XLSX file XSL Transformations in ABAP Example Programs & Transformations Author: Benu Mariantony Company: SAP Labs Created on: 19 November 2012 Author Bio Benu Mariantony - Detail-oriented Senior SAP Technical Consultant with over 5 years' of experience in SAP ABAP, ABAP Objects, ABAP Webdynpro, HR-ABAP, Interfacing, ABAP XML Processing (XSLT and Simple Transformations) and upgrade. Skilled in both the "object" and the “classical” sides of ABAP stream, focusing on technical arena involved dynamic programming, design, optimization, and performance tuning. Currently, working for Solution Manager 7.1, Component Monitoring BI at SAP Labs.

XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

  • Upload
    buitu

  • View
    225

  • Download
    2

Embed Size (px)

Citation preview

Page 1: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 1

XLSX Upload – a unified approach

Applies to:

SAP NetWeaver 7.0 – Enhancement Package 2 Onwards

Summary

This document provides a comprehensive introduction about XLSX files and a unified approach to upload XLSX files into SAP GUI and Webdynpro ABAP. This gives you an overview of the following,

Key notions in XLSX

Unified approach to upload XSLX file

APIs used to upload a XLSX file

XSL Transformations in ABAP

Example Programs & Transformations

Author: Benu Mariantony

Company: SAP Labs

Created on: 19 November 2012

Author Bio

Benu Mariantony - Detail-oriented Senior SAP Technical Consultant with over 5 years' of experience in SAP ABAP, ABAP Objects, ABAP – Webdynpro, HR-ABAP, Interfacing, ABAP XML Processing (XSLT and Simple Transformations) and upgrade. Skilled in both the "object" and the “classical” sides of ABAP stream, focusing on technical arena involved dynamic programming, design, optimization, and performance tuning. Currently, working for Solution Manager 7.1, Component Monitoring – BI at SAP Labs.

Page 2: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 2

Table of Contents

Overview ........................................................................................................................................................... 3

Openness of Excel (XLSX Format) ............................................................................................................... 4

APIs for Office Applications ............................................................................................................................ 5

XLSX Import – How to ..................................................................................................................................... 6

Step 1 – Read the path of uploaded file ................................................................................................... 6

Step 2 – Load the file content..................................................................................................................... 6

Step 3 – Loading the Package ................................................................................................................... 7

Step 4 – Loading the parts of the Package .............................................................................................. 7

Step 5 – Load the XMLs .............................................................................................................................. 7

Step 6 – Process the XML – Less ABAP – More XSLT ......................................................................... 8

Processing the Sheet and Shared String XML .........................................................................................................................9

Business Scenario ......................................................................................................................................... 10

Transformations ............................................................................................................................................. 12

ABAP Program Implementation ................................................................................................................... 16

Webdynpro ABAP Implementation .............................................................................................................. 19

XLSX Source File – To Upload .................................................................................................................... 21

I knew that creating a XLSX document from scratch would take a lot of time. If you feel so get the file from the DDIC itself in the following way, ............................................................................................ 21

Related Content ............................................................................................................................................. 22

Copyright ......................................................................................................................................................... 23

Page 3: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 3

Overview

Every customer does their data maintenance in Excel and most of the SAP implementation or Support projects would get a requirement to upload an excel file into SAP system either to store the data in data dictionary or for reporting – Predominantly to display the excel data in ALV/ in table control (in both SAP GUI and Webdynpro).

Most of our SAP ABAP consultants would have thought about this business scenario and posted a lot of threads in SCN too ;). I see a lot of SCN responses recommends the consultants to convert excel (XLS) file to Tab Limited File or Comma Separated File (CSV) and to use GUI_UPLOAD for uploading the data into internal table. Most of the threads on this subject are left unanswered.

Besides, each ABAP consultants would have tried uploading the data using the following renowned function modules and somehow managed to complete the requirement. Have a gaze at the disadvantages in the classical approach in the following tabular representation,

Function Module Disadvantages

GUI_UPLOAD Does not handle the excel file. Excel file needs to be converted to Tab Limited or Comma Separated file. – The accuracy of the data and validating is pretty challenging

TEXT_CONVERT_XLS_TO_SAP Data processing is very slow and mass data upload is not optimistic.

ALSM_EXCEL_TO_INTERNAL_TABLE Data processing is very slow as it processes the data cell by cell and the count of rows and columns are fixed. KCD_EXCEL_OLE_TO_INT_CONVERT

Table 1: This table illustrates the conventional SE37 function modules available to upload the excel file into our

application server.

In addition, the last three function modules are available as part of “SAP_APPL” component and hence those are available only for logistics and accounting modules. The chances of using this three function module in other modules like Solution Manager or CRM or SRM systems are not possible owing to their unavailability. Furthermore, all the above Function Modules cannot be used in Webdynpro ABAP.

Beyond SAP’s technical difficulties, Microsoft excel has its own restrictions as the total number of rows is limited to 65,536 and the XLS format is stored in Binary format. Owing to all this complexities, until NetWeaver 7.0, SAP does not offer any standard way to upload Microsoft’s proprietary excel.

As of NW 7.02 (NetWeaver 7.0 Enhancement Package 2), SAP does support the excel operations (Import/ Export) due to the openness of excel since Microsoft office 2007. Click here to know the difference between XLS and XLSX files.

Page 4: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 4

Openness of Excel (XLSX Format)

XLSX is a default Excel 2007 workbook format. In reality, the XLSX document is a ZIP compressed archive with a directory structure of XML text documents. As shown in the screenshot below, the XLSX file could be extracted and the excel data are place in the following file path.

The following are an important XML files, we need to refer to fetch the data from ABAP,

1. Sheet Data – SPFLI\xl\sheet1.xml

2. Shared String – SPFLI\sharedStrings.xml

Have a look at the pictorial representation of the folder structure,

Figure 1: The screenshot depicts the following activities, 1. Decompress/ extract the XLSX file

2. Folder Structure of XLSX 3. XML files where we could find the excel data

Page 5: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 5

Each excel sheet and its cells are parsed as an XML document. Every strings in excel are uniquely stored in a separate XML document “sharedstrings.xml” and those indexes are maintained in the respective sheet XMLs whereas the integer values are maintained in the sheet XML itself. Have a glimpse of it in the following screenshots,

Figure 2: The topmost view illustrates the schema of sheet XML and shared string XML and the how the mapping takes

place. The lower view is a snapshot captured from the excel file.

Thus, the effort of reading excel is much simplified and now all we need to do is to read multiple XML files from XLSX package to populate an internal table.

APIs for Office Applications

As of NetWeaver 7 Ehp2 (7.02), we have a different APIs to adapt to the openness of MS Office platform. The following classes are as part of those,

CL_XLSX_DOCUMENT Loads/ creates an XLSX document. This is the API we use in this document to load the excel file into ABAP internal table.

CL_DOCX_DOCUMENT Loads/ creates an word document

CL_PPTX_DOCUMENT Loads/ creates presentations.

Table 2: Classes for reading office applications (Excel, document and power point)

Page 6: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 6

XLSX Import – How to

The following are the steps ought to be followed to upload the file into ABAP Internal Table, here you go…

1. Read the path of the uploaded file by offering a “File open dialog”

2. Load the content of the file

3. Load the package

4. Load the parts of the package

5. Load the Shared String XML and Sheet1 XML

6. Process the XMLs using XSL transformation

The aforesaid steps are applicable for Webdynpro ABAP too by having “Upload UI element” in place.

Step 1 – Read the path of uploaded file

Get the file open dialog (on hitting F1) using the static method browse_local_file_open from the class cl_openxml_helper as shown in the following code snippet,

AT SELECTION-SCREEN ON VALUE-REQUEST FOR filename.

cl_openxml_helper=>browse_local_file_open(

iv_title = 'Select XLSX File'

iv_filename = ''

iv_extpattern = 'All files(*.*)|*.*' ).

This method gets you the path of the selected file in the string variable. You could achieve this using the static method file_open_dialog in the class cl_gui_frontend_services,

AT SELECTION-SCREEN ON VALUE-REQUEST FOR filename.

CALL METHOD cl_gui_frontend_services=>file_open_dialog

EXPORTING window_title = 'Select XLSX File'

initial_directory = dir

file_filter = '*.xlsx'

multiselection = ''

CHANGING file_table = filetable

rc = i.

IF i = 1.

READ TABLE filetable INDEX 1 INTO ft.

srcpath = ft-filename.

ENDIF.

Step 2 – Load the file content

Initially, the content of the selected file path should be uploaded into our program using the static method load_local_file( ) from the class cl_openxml_helper,

TRY.

mv_file_data = cl_openxml_helper=>load_local_file( lv_filename ).

CATCH cx_openxml_not_found.

ENDTRY.

This will get you a hexadecimal content. This could be also achieved using the removed GUI class, so called cl_gui_frontend_services=>fui_upload. As this frontend classes is usable only in SAP GUI based transactions (not for webdynpro), we use the XML helper class cl_openxml_helper, which could be used both in SAP GUI programs and webdynpro applications.

Page 7: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 7

Step 3 – Loading the Package

As I mentioned before, the entire XSLX file is wrapped up in a compressed package and which is not readable unless we read the package. For loading the package, we have a class cl_xlsx_document which has got unveiled since NW 702 (NetWeaver 7 Ehp 2).

TRY.

mo_package = cl_xlsx_document=>load_document( iv_data = mv_file_data ).

CATCH cx_openxml_format cx_openxml_not_found.

ENDTRY.

Step 4 – Loading the parts of the Package

The loaded package will be in the format shown below and for reading the data from excel sheet1 we need to read the XML “sharedString.xml” from “XLSX\SPFLI\xl” and “sheet1.xml” from “XLSX\SPFLI\xl\worksheets” folder. As these files are present in the different folders; we need to differentiate and identify the XML by means of package parts.

Figure 3: The above screenshot refers to the different folder paths where user could locate the required XMLs to

populate the excel rows and columns.

The following code snippet would let you locate the exact XML file.

TRY.

mo_parts = mo_package->get_parts( ).

CATCH cx_openxml_format cx_openxml_not_found.

ENDTRY.

Step 5 – Load the XMLs

We need to read “Sheet” and “Shared String” XML files which are wrapped up in the loaded package. These files could be located using the part reference (refer step 4).

Read the “Sheet1 XML” by passing to the part number as “2” as shown in the code snippet below. (load_xml_data is the method you could find in the example program)

CONSTANTS co_sheet_xml TYPE i VALUE 2.

DATA mv_sheet_data TYPE xstring.

mv_sheet_data = load_xml_data( co_sheet_xml ).

Page 8: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 8

Read the “Shared String XML” by passing to the part number as “3”,

CONSTANTS co_shared_str_xml TYPE i VALUE 3.

DATA mv_shared_data TYPE xstring.

mv_shared_data = load_xml_data( co_shared_str_xml ).

Method Implementation:

METHOD load_xml_data.

DATA lo_xml_part TYPE REF TO cl_openxml_part.

DATA lo_xml_part_uri TYPE REF TO cl_openxml_parturi.

DATA lv_uri TYPE string.

TRY.

lv_uri = mo_parts->get_part( 2 )->get_parts( )->get_part(

iv_xml_index )->get_uri( )->get_uri( ).

lo_xml_part_uri = cl_openxml_parturi=>create_from_partname( lv_uri ).

lo_xml_part = mo_package->get_part_by_uri( lo_xml_part_uri ).

rv_xml_data = lo_xml_part->get_data( ).

CATCH cx_openxml_format cx_openxml_not_found.

ENDTRY.

ENDMETHOD.

Step 6 – Process the XML – Less ABAP – More XSLT

XSLT: XSL stands for EXtensible Stylesheet Language, and is a style sheet language for XML documents driven by W3C. XSLT stands for XSL Transformations. XSLT is a language for transforming XML documents into other XML documents or other objects such as HTML, XML or XSL. Typically, input documents are XML files, but anything from which the processor can build an XQuery and XPath Data Model can be used. The original document is not changed; rather, a new document is created based on the content of an existing one. XSLT is a Turing-complete language, meaning it can perform any calculation that can be performed by a modern computer program.

For more information for XSLT refer http://www.w3schools.com/xsl/

XSLT in ABAP: The SAP XSLT processor is already available as part of the kernel since Basis release 6.10 with the specifications defined for the XSLT version 1.0. XSLT has a capability to transform the following,

XML to XML

XML to ABAP Internal Table

ABAP Internal Tale to XML

ABAP Internal Table to ABAP Internal Table

Click here to know more about ABAP XSLT Programming and gaze at the examples too.

To create XSLT Programs in ABAP go to TCode SE80 Right Click on the Package Create Other (1) Transformation Enter the name of the transformation and choose the type as “XSLT”.

Page 9: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 9

Figure 4: Captured from SE80 transaction while attempting to create the XSLT transformation.

Processing the Sheet and Shared String XML

The processing of XSL transformation is much faster than ABAP language and hence we are going to do more XSLT and less ABAP for processing the XML data.

The XSLT will read the “sheet xml data” and map the cell indexes with the “shared string XML” to read the cell data.

In XSLT, we can pass only one XML file as a source; whereas we have two XML documents (Sheet XML and Shared String XML) which need to be desterilized to form an internal table. Hence we are going to pass the sheet XML as a source for XSLT and shared String XML as a parameter my removing the XML namespaces. For removing the namespaces we are going to create another transformation. (Our objective is to read and process both the XMLs using a single XSLT program.)

TRY.

CALL TRANSFORMATION zben_shared_string_del_namesp

SOURCE XML mv_shared_data

RESULT XML lv_shared_str_xml.

CALL TRANSFORMATION zben_xlsx_sheet_import

PARAMETERS p_shared_string = lo_shared_str_nodeset

SOURCE XML mv_sheet_data

RESULT lt_spfli = mt_spfli.

CATCH cx_xslt_exception.

ENDTRY.

The internal table mt_spfli is the final internal table needs to be passed to the ALV.

Transformation Parameters Remarks

ZBEN_SHARED_STRING_DEL_NAMESP Source Shared String XML

Result Shared String XML without namespaces

Removes the namespace of “Shared String XML”. [we are going to pass this XML as a parameter string to the main xslt as passing the XML as a parameter is forbidden]

ZBEN_XLSX_SHEET_IMPORT Source Sheet XML

Parameter Shared String XML without namespace)

Result (LT_SPFLI) ABAP Internal Table

Processes the sheet data and shared string data to populate an ABAP internal table.

Table 3: The details of created XSLT Transformation to read the XSLX file

Page 10: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 10

Business Scenario

I have identified two scenarios one for SAP GUI and other for Webdynpro ABAP just to motivate you try out this unified approach. You could use the same transformation for both the scenarios. Refer the subsequent sections and try to make out the working scenario by referring to the code snippet showcased in the previous sections. If you find any difficulties, please go through the complete code (ABAP and XSLT in the last section)

Scenario 1 – Using SAP GUI

ALV needs to be populated by uploading the data from XLSX document.

Step 1: Choose the XLSX file in the selection screen

Figure 5: Selection Screen captured from SAP GUI with a single parameter for uploading the excel file

Step 2: On hitting F8, the uplaoded excel file needs to be processed and display the data in the ALV as shown below,

Figure 6: ALV which lists the Flight information which has been processed by our classes and transformation.

Page 11: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 11

Scenario 2 – Using Webdynpro ABAP

Table needs to be loaded by uploading the data from XLSX document

Step 1: Choose the XLSX file in the upload UI element

Figure 7: Webdynpro application with a “FileUpload” UI element and an button to upload the excel file.

Step 2: Click on “Upload XLSX”, the fine needs to be processed and displayed.

Figure 8: Webdynpro “Table” UI element which shows the uploaded data from the excel file.

Page 12: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 12

Transformations

Name ZBEN_XLSX_REMOVE_NAMESPACE

Parameters

Source XML: Shared String XML

Parameter: Nil

Result: DOM Reference with XML tags without namespaces.

Description Removes the namespaces available in all the nodes of the shared string XML

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ss="http://sch

emas.openxmlformats.org/spreadsheetml/2006/main" version="1.1">

<xsl:strip-space elements="*"/>

<xsl:output encoding="utf-8" indent="yes" method="xml" omit-xml-declaration="yes"/>

<xsl:template match="/">

<xsl:element name="sst" namespace="">

<xsl:for-each select="ss:sst/ss:si">

<xsl:element name="si" namespace="">

<xsl:element name="t" namespace="">

<xsl:value-of select="ss:t"/>

</xsl:element>

</xsl:element>

</xsl:for-each>

</xsl:element>

</xsl:template>

</xsl:stylesheet>

Name ZBEN_XLSX_SHEET_IMPORT

Parameters

Source XML: Sheet1 XML

Parameter: P_SHARED_STRING (Shared String with namespaces removed using the previous transformation “ZBEN_XLSX_REMOVE_NAMESPACE”)

Result: LT_SPFLI (ABAP Internal Table)

Description

Processes the Sheet XML and Shared String XML to populate the Internal Table.

Process Flow:

1. Read the index from the sheet XML

2. Retrieve the cell content by reading the string available in shared string XML

Page 13: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 13

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:ss="http://schemas.openxmlformats.org/spreadsheetml/2006/main"

xmlns:sap="http://www.sap.com/sapxsl" xmlns:asx="http://www.sap.com/abapxml" exclude-

result-prefixes="c" version="1.0">

<xsl:param name="P_SHARED_STRING" select=""/>

<xsl:strip-space elements="*"/>

<xsl:output encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>

<xsl:variable name="V_SHARED_STRING">

<xsl:if test="$P_SHARED_STRING">

<xsl:copy-of select="$P_SHARED_STRING"/>

</xsl:if>

</xsl:variable>

<xsl:template match="/">

<asx:abap version="1.0">

<asx:values>

<LT_SPFLI>

<xsl:for-each select="ss:worksheet/ss:sheetData/ss:row">

<xsl:if test="position() &gt; 1">

<item>

<CARRID>

<xsl:variable name="cell_id" select="concat('A', position())"/>

<xsl:variable name="v_index" select="ss:c[@r=$cell_id][@t='s']/ss:v"/>

<xsl:if test="$v_index">

<xsl:value-of select="$V_SHARED_STRING/sst/si[$v_index + 1]/t"/>

</xsl:if>

<xsl:if test="not($v_index)">

<xsl:value-of select="ss:c[@r=$cell_id]/ss:v"/>

</xsl:if>

</CARRID>

<CONNID>

<xsl:variable name="cell_id" select="concat('B', position())"/>

<xsl:variable name="v_index" select="ss:c[@r=$cell_id][@t='s']/ss:v"/>

<xsl:if test="$v_index">

<xsl:value-of select="$V_SHARED_STRING/sst/si[$v_index + 1]/t"/>

</xsl:if>

<xsl:if test="not($v_index)">

<xsl:value-of select="ss:c[@r=$cell_id]/ss:v"/>

</xsl:if>

</CONNID>

<COUNTRYFR>

<xsl:variable name="cell_id" select="concat('C', position())"/>

<xsl:variable name="v_index" select="ss:c[@r=$cell_id][@t='s']/ss:v"/>

<xsl:if test="$v_index">

<xsl:value-of select="$V_SHARED_STRING/sst/si[$v_index + 1]/t"/>

</xsl:if>

<xsl:if test="not($v_index)">

<xsl:value-of select="ss:c[@r=$cell_id]/ss:v"/>

</xsl:if>

</COUNTRYFR>

<CITYFROM>

<xsl:variable name="cell_id" select="concat('D', position())"/>

<xsl:variable name="v_index" select="ss:c[@r=$cell_id][@t='s']/ss:v"/>

<xsl:if test="$v_index">

Page 14: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 14

<xsl:value-of select="$V_SHARED_STRING/sst/si[$v_index + 1]/t"/>

</xsl:if>

<xsl:if test="not($v_index)">

<xsl:value-of select="ss:c[@r=$cell_id]/ss:v"/>

</xsl:if>

</CITYFROM>

<AIRPFROM>

<xsl:variable name="cell_id" select="concat('E', position())"/>

<xsl:variable name="v_index" select="ss:c[@r=$cell_id][@t='s']/ss:v"/>

<xsl:if test="$v_index">

<xsl:value-of select="$V_SHARED_STRING/sst/si[$v_index + 1]/t"/>

</xsl:if>

<xsl:if test="not($v_index)">

<xsl:value-of select="ss:c[@r=$cell_id]/ss:v"/>

</xsl:if>

</AIRPFROM>

<COUNTRYTO>

<xsl:variable name="cell_id" select="concat('F', position())"/>

<xsl:variable name="v_index" select="ss:c[@r=$cell_id][@t='s']/ss:v"/>

<xsl:if test="$v_index">

<xsl:value-of select="$V_SHARED_STRING/sst/si[$v_index + 1]/t"/>

</xsl:if>

<xsl:if test="not($v_index)">

<xsl:value-of select="ss:c[@r=$cell_id]/ss:v"/>

</xsl:if>

</COUNTRYTO>

<CITYTO>

<xsl:variable name="cell_id" select="concat('G', position())"/>

<xsl:variable name="v_index" select="ss:c[@r=$cell_id][@t='s']/ss:v"/>

<xsl:if test="$v_index">

<xsl:value-of select="$V_SHARED_STRING/sst/si[$v_index + 1]/t"/>

</xsl:if>

<xsl:if test="not($v_index)">

<xsl:value-of select="ss:c[@r=$cell_id]/ss:v"/>

</xsl:if>

</CITYTO>

<AIRPTO>

<xsl:variable name="cell_id" select="concat('H', position())"/>

<xsl:variable name="v_index" select="ss:c[@r=$cell_id][@t='s']/ss:v"/>

<xsl:if test="$v_index">

<xsl:value-of select="$V_SHARED_STRING/sst/si[$v_index + 1]/t"/>

</xsl:if>

<xsl:if test="not($v_index)">

<xsl:value-of select="ss:c[@r=$cell_id]/ss:v"/>

</xsl:if>

</AIRPTO>

<FLTIME>

<xsl:variable name="cell_id" select="concat('I', position())"/>

<xsl:variable name="v_index" select="ss:c[@r=$cell_id][@t='s']/ss:v"/>

<xsl:if test="$v_index">

<xsl:value-of select="$V_SHARED_STRING/sst/si[$v_index + 1]/t"/>

</xsl:if>

<xsl:if test="not($v_index)">

<xsl:value-of select="ss:c[@r=$cell_id]/ss:v"/>

</xsl:if>

</FLTIME>

Page 15: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 15

<DEPTIME>

<xsl:variable name="cell_id" select="concat('J', position())"/>

<xsl:variable name="v_index" select="ss:c[@r=$cell_id][@t='s']/ss:v"/>

<xsl:if test="$v_index">

<xsl:value-of select="$V_SHARED_STRING/sst/si[$v_index + 1]/t"/>

</xsl:if>

<xsl:if test="not($v_index)">

<xsl:value-of select="ss:c[@r=$cell_id]/ss:v"/>

</xsl:if>

</DEPTIME>

<ARRTIME>

<xsl:variable name="cell_id" select="concat('K', position())"/>

<xsl:variable name="v_index" select="ss:c[@r=$cell_id][@t='s']/ss:v"/>

<xsl:if test="$v_index">

<xsl:value-of select="$V_SHARED_STRING/sst/si[$v_index + 1]/t"/>

</xsl:if>

<xsl:if test="not($v_index)">

<xsl:value-of select="ss:c[@r=$cell_id]/ss:v"/>

</xsl:if>

</ARRTIME>

<DISTANCE>

<xsl:variable name="cell_id" select="concat('L', position())"/>

<xsl:variable name="v_index" select="ss:c[@r=$cell_id][@t='s']/ss:v"/>

<xsl:if test="$v_index">

<xsl:value-of select="$V_SHARED_STRING/sst/si[$v_index + 1]/t"/>

</xsl:if>

<xsl:if test="not($v_index)">

<xsl:value-of select="ss:c[@r=$cell_id]/ss:v"/>

</xsl:if>

</DISTANCE>

<DISTID>

<xsl:variable name="cell_id" select="concat('M', position())"/>

<xsl:variable name="v_index" select="ss:c[@r=$cell_id][@t='s']/ss:v"/>

<xsl:if test="$v_index">

<xsl:value-of select="$V_SHARED_STRING/sst/si[$v_index + 1]/t"/>

</xsl:if>

<xsl:if test="not($v_index)">

<xsl:value-of select="ss:c[@r=$cell_id]/ss:v"/>

</xsl:if>

</DISTID>

<FLTYPE>

<xsl:variable name="cell_id" select="concat('N', position())"/>

<xsl:variable name="v_index" select="ss:c[@r=$cell_id][@t='s']/ss:v"/>

<xsl:if test="$v_index">

<xsl:value-of select="$V_SHARED_STRING/sst/si[$v_index + 1]/t"/>

</xsl:if>

<xsl:if test="not($v_index)">

<xsl:value-of select="ss:c[@r=$cell_id]/ss:v"/>

</xsl:if>

</FLTYPE>

<PERIOD>

<xsl:variable name="cell_id" select="concat('O', position())"/>

<xsl:variable name="v_index" select="ss:c[@r=$cell_id][@t='s']/ss:v"/>

<xsl:if test="$v_index">

<xsl:value-of select="$V_SHARED_STRING/sst/si[$v_index + 1]/t"/>

</xsl:if>

Page 16: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 16

<xsl:if test="not($v_index)">

<xsl:value-of select="ss:c[@r=$cell_id]/ss:v"/>

</xsl:if>

</PERIOD>

</item>

</xsl:if>

</xsl:for-each>

</LT_SPFLI>

</asx:values>

</asx:abap>

</xsl:template>

</xsl:transform>

ABAP Program Implementation

This ABAP program will get you the result for the scenario mentioned in the previous chapter. Before executing this program make sure that the transformation programs are created in the system.

*&---------------------------------------------------------------------*

*& Report ZBEN_ALV_XLSX_UPLOAD

*&

*&---------------------------------------------------------------------*

*& Author : Benu Mariantony

*& Date Created : 16-Oct-2012

*& Desc : To upload the xlsx data into ALV grid

*&---------------------------------------------------------------------*

REPORT zben_alv_xlsx_upload.

DATA lv_tmpfilename TYPE string.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-000.

PARAMETERS filename TYPE cts_filename DEFAULT '' LOWER CASE OBLIGATORY.

SELECTION-SCREEN END OF BLOCK b1.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR filename.

lv_tmpfilename = cl_openxml_helper=>browse_local_file_open

( iv_title = 'Select XLSX File'

iv_filename = ''

iv_extpattern = 'All files(*.*)|*.*' ).

IF lv_tmpfilename IS NOT INITIAL.

filename = lv_tmpfilename.

ENDIF.

*----------------------------------------------------------------------*

* CLASS lcl_alv_xlsx_upload DEFINITION

*----------------------------------------------------------------------*

* Class Definitions for Excel Upload

*----------------------------------------------------------------------*

Page 17: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 17

CLASS lcl_alv_xlsx_upload DEFINITION FINAL.

PUBLIC SECTION.

CLASS-METHODS initialize IMPORTING value(iv_filename) TYPE cts_filename.

PRIVATE SECTION.

DATA mv_file_data TYPE xstring.

DATA mo_package TYPE REF TO cl_openxml_package.

DATA mo_parts TYPE REF TO cl_openxml_partcollection.

DATA mt_spfli TYPE TABLE OF spfli.

DATA mv_sheet_data TYPE xstring.

DATA mv_shared_data TYPE xstring.

CONSTANTS co_sheet_xml TYPE i VALUE 2.

CONSTANTS co_shared_str_xml TYPE i VALUE 3.

METHODS read_xlsx IMPORTING value(iv_filename) TYPE cts_filename.

METHODS load_package.

METHODS load_xml_data IMPORTING value(iv_xml_index) TYPE i

RETURNING value(rv_xml_data) TYPE xstring.

METHODS transform_xml_to_abap.

METHODS display_alv.

ENDCLASS. "lcl_alv_xlsx_upload DEFINITION

*----------------------------------------------------------------------*

* CLASS lcl_alv_xlsx_upload IMPLEMENTATION

*----------------------------------------------------------------------*

* Class Implementations for Excel Upload

*----------------------------------------------------------------------*

CLASS lcl_alv_xlsx_upload IMPLEMENTATION.

METHOD initialize.

DATA lo_xlsx TYPE REF TO lcl_alv_xlsx_upload.

CREATE OBJECT lo_xlsx.

"Read the excel

lo_xlsx->read_xlsx( iv_filename ).

"Display the data in the ALV grid

lo_xlsx->display_alv( ).

ENDMETHOD. "initialize

METHOD read_xlsx.

DATA lv_filename TYPE string.

lv_filename = iv_filename.

"Read the content of XLSX file

TRY.

mv_file_data = cl_openxml_helper=>load_local_file( lv_filename ).

CATCH cx_openxml_not_found.

ENDTRY.

load_package( ).

"Read the sheet XML

mv_sheet_data = load_xml_data( co_sheet_xml ).

"Read the shared string XML

Page 18: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 18

mv_shared_data = load_xml_data( co_shared_str_xml ).

transform_xml_to_abap( ).

ENDMETHOD. "read_xlsx

METHOD load_package.

TRY.

mo_package = cl_xlsx_document=>load_document( iv_data = mv_file_data ).

mo_parts = mo_package->get_parts( ).

CATCH cx_openxml_format cx_openxml_not_found.

ENDTRY.

ENDMETHOD. "load_package

METHOD load_xml_data.

DATA lo_xml_part TYPE REF TO cl_openxml_part.

DATA lo_xml_part_uri TYPE REF TO cl_openxml_parturi.

DATA lx_root TYPE REF TO cx_root.

DATA lv_uri TYPE string.

TRY.

lv_uri = mo_parts->get_part( 2 )->get_parts( )-

>get_part( iv_xml_index )->get_uri( )->get_uri( ).

lo_xml_part_uri = cl_openxml_parturi=>create_from_partname( lv_uri ).

lo_xml_part = mo_package->get_part_by_uri( lo_xml_part_uri ).

rv_xml_data = lo_xml_part->get_data( ).

CATCH cx_openxml_format cx_openxml_not_found.

ENDTRY.

ENDMETHOD. "load_xml_data

METHOD transform_xml_to_abap.

DATA lo_shared_str_dom TYPE REF TO if_ixml_document.

DATA lo_shared_str_nodeset TYPE REF TO if_ixml_node.

DATA lv_shared_str_xml TYPE xstring.

"We will pass the Sheet XML as XSL source and Shared XML as XSL Parameter

"Sheet XML contains just the indexes

"Using those indexes we need to fetch the real data from Shared String XML

"To send the Shared String XML as a XSL parameter we need to remove the

" - namespaces

TRY.

CALL TRANSFORMATION zben_shared_string_del_namesp

SOURCE XML mv_shared_data

RESULT XML lv_shared_str_xml.

"Create a nodeset from the shared XML

CALL FUNCTION 'SDIXML_XML_TO_DOM'

EXPORTING

xml = lv_shared_str_xml

IMPORTING

document = lo_shared_str_dom

EXCEPTIONS

invalid_input = 1

OTHERS = 2.

IF sy-subrc = 0.

lo_shared_str_nodeset = lo_shared_str_dom->clone( ).

ENDIF.

"Pass the SHEET XML and Shared String Nodeset to the main transformation

Page 19: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 19

CALL TRANSFORMATION zben_xlsx_sheet_import

PARAMETERS p_shared_string = lo_shared_str_nodeset

SOURCE XML mv_sheet_data

RESULT lt_spfli = mt_spfli.

CATCH cx_xslt_exception.

ENDTRY.

ENDMETHOD. "transform_xml_to_abap

METHOD display_alv.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = sy-repid

i_structure_name = 'SPFLI'

TABLES

t_outtab = mt_spfli

EXCEPTIONS

program_error = 1

OTHERS = 2.

IF sy-subrc <> 0.

ENDIF.

ENDMETHOD. "display_alv

ENDCLASS. "lcl_alv_xlsx_upload IMPLEMENTATION

START-OF-SELECTION.

lcl_alv_xlsx_upload=>initialize( iv_filename = filename ).

Webdynpro ABAP Implementation

1. Create a view

2. Create a following nodes in the view context

a. Binding the filename and content to the “FileUpload” UI element

b. Binging the Excel table to the “Table” UI element

3. Create a view with following elements,

a. FileUpload

b. Button

c. Table

4. Write the code in the button action as shown in the snippet below,

METHOD onactionupload.

CONSTANTS co_sheet_xml TYPE i VALUE 2.

CONSTANTS co_shared_str_xml TYPE i VALUE 3.

wd_this->load_package( ).

"Read the sheet XML

wd_this->mv_sheet_data = load_xml_data( co_sheet_xml ).

"Read the shared string XML

wd_this->mv_shared_data = load_xml_data( co_shared_str_xml ).

wd_this->transform_xml_to_abap( ).

wd_this->set_table_cotent( ).

ENDMETHOD.

Page 20: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 20

Please note that the methods called in the above action needs to be created in the webdynpro view which is similar to the ABAP program included above.

METHOD load_package.

DATA lo_el_xlsx_upload TYPE REF TO if_wd_context_element.

lo_el_xlsx_upload = wd_context->get_child_node( name = wd_this-

>wdctx_xlsx_upload )->get_element( ).

lo_el_xlsx_upload->get_attribute(

EXPORTING

name = `CONTENT`

IMPORTING

value = wd_this->mv_file_data ).

TRY.

wd_this->mo_package = cl_xlsx_document=>load_document( iv_data = wd_this-

>mv_file_data ).

wd_this->mo_parts = wd_this->mo_package->get_parts( ).

CATCH cx_openxml_format cx_openxml_not_found.

ENDTRY.

ENDMETHOD.

METHOD load_xml_data.

DATA lo_xml_part TYPE REF TO cl_openxml_part.

DATA lo_xml_part_uri TYPE REF TO cl_openxml_parturi.

DATA lx_root TYPE REF TO cx_root.

DATA lv_uri TYPE string.

TRY.

lv_uri = wd_this->mo_parts->get_part( 2 )->get_parts( )-

>get_part( iv_xml_index )->get_uri( )->get_uri( ).

lo_xml_part_uri = cl_openxml_parturi=>create_from_partname( lv_uri ).

lo_xml_part = wd_this->mo_package->get_part_by_uri( lo_xml_part_uri ).

rv_xml_data = lo_xml_part->get_data( ).

CATCH cx_openxml_format cx_openxml_not_found.

ENDTRY.

ENDMETHOD.

METHOD transform_xml_to_abap.

DATA lo_shared_str_dom TYPE REF TO if_ixml_document.

DATA lo_shared_str_nodeset TYPE REF TO if_ixml_node.

DATA lv_shared_str_xml TYPE xstring.

TRY.

CALL TRANSFORMATION zben_shared_string_del_namesp

SOURCE XML wd_this->mv_shared_data

RESULT XML lv_shared_str_xml.

"Create a nodeset from the shared XML

CALL FUNCTION 'SDIXML_XML_TO_DOM'

EXPORTING

xml = lv_shared_str_xml

IMPORTING

document = lo_shared_str_dom

EXCEPTIONS

invalid_input = 1

OTHERS = 2.

Page 21: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 21

IF sy-subrc = 0.

lo_shared_str_nodeset = lo_shared_str_dom->clone( ).

ENDIF.

"Pass the SHEET XML and Shared String Nodeset to the main transformation

CALL TRANSFORMATION zben_xlsx_sheet_import

PARAMETERS p_shared_string = lo_shared_str_nodeset

SOURCE XML wd_this->mv_sheet_data

RESULT lt_spfli = wd_this->mt_spfli.

CATCH cx_xslt_exception.

ENDTRY.

ENDMETHOD.

METHOD set_table_cotent.

wd_context->get_child_node( name = wd_this->wdctx_spfli )-

>bind_table( new_items = wd_this->mt_spfli set_initial_elements = abap_true ).

ENDMETHOD.

XLSX Source File – To Upload

I knew that creating a XLSX document from scratch would take a lot of time. If you feel so get the file from the DDIC itself in the following way,

1. Transaction Code SE16

2. Table Name: SPFLI

3. Execute

4. Upload the ALV output by exporting the data in XLSX format.

Page 23: XLSX Upload - a Unified Approach · KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are ... to load the excel file into ABAP internal table. ... XLSX Upload –

XLSX Upload – a unified approach

SAP COMMUNITY NETWORK scn.sap.com

© 2012 SAP AG 23

Copyright

© Copyright 2012 SAP AG. All rights reserved.

No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. The information contained herein may be changed without prior notice.

Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors.

Microsoft, Windows, Excel, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation.

IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System z10, System z9, z10, z9, iSeries, pSeries, xSeries, zSeries, eServer, z/VM, z/OS, i5/OS, S/390, OS/390, OS/400, AS/400, S/390 Parallel Enterprise Server, PowerVM, Power Architecture, POWER6+, POWER6, POWER5+, POWER5, POWER, OpenPower, PowerPC, BatchPipes, BladeCenter, System Storage, GPFS, HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex, MVS/ESA, AIX, Intelligent Miner, WebSphere, Netfinity, Tivoli and Informix are trademarks or registered trademarks of IBM Corporation.

Linux is the registered trademark of Linus Torvalds in the U.S. and other countries.

Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or registered trademarks of Adobe Systems Incorporated in the United States and/or other countries.

Oracle is a registered trademark of Oracle Corporation.

UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group.

Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of Citrix Systems, Inc.

HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C®, World Wide Web Consortium, Massachusetts Institute of Technology.

Java is a registered trademark of Oracle Corporation.

JavaScript is a registered trademark of Oracle Corporation, used under license for technology invented and implemented by Netscape.

SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP Business ByDesign, and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and other countries.

Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and other Business Objects products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Business Objects S.A. in the United States and in other countries. Business Objects is an SAP company.

All other product and service names mentioned are the trademarks of their respective companies. Data contained in this document serves informational purposes only. National product specifications may vary.

These materials are subject to change without notice. These materials are provided by SAP AG and its affiliated companies ("SAP Group") for informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP Group products and services are those that are set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.