XLSX Upload - IN SAP GUI and WDA

9,026 views

Published on

Unified approach to upload the excel(XLSX) file in Webdynpro ABAP and SAP GUI

Published in: Technology
1 Comment
1 Like
Statistics
Notes
  • For SAP online training register at http://www.todaycourses.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
9,026
On SlideShare
0
From Embeds
0
Number of Embeds
28
Actions
Shares
0
Downloads
192
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

XLSX Upload - IN SAP GUI and WDA

  1. 1. XLSX Upload – a unified approachApplies to:SAP NetWeaver 7.0 – Enhancement Package 2 OnwardsSummaryThis document provides a comprehensive introduction about XLSX files and a unified approach to uploadXLSX 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 & TransformationsAuthor: Benu MariantonyCompany: SAP LabsCreated on: 19 November 2012Author 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.SAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 1
  2. 2. XLSX Upload – a unified approachTable of ContentsOverview ........................................................................................................................................................... 3Openness of Excel (XLSX Format) ............................................................................................................... 4APIs for Office Applications ............................................................................................................................ 5XLSX 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 .........................................................................................................................9Business Scenario ......................................................................................................................................... 10Transformations ............................................................................................................................................. 12ABAP Program Implementation................................................................................................................... 16Webdynpro ABAP Implementation.............................................................................................................. 19XLSX Source File – To Upload .................................................................................................................... 21I knew that creating a XLSX document from scratch would take a lot of time. If you feel so get thefile from the DDIC itself in the following way, ............................................................................................ 21Related Content ............................................................................................................................................. 22Copyright ......................................................................................................................................................... 23SAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 2
  3. 3. XLSX Upload – a unified approachOverviewEvery customer does their data maintenance in Excel and most of the SAP implementation or Supportprojects would get a requirement to upload an excel file into SAP system either to store the data in datadictionary or for reporting – Predominantly to display the excel data in ALV/ in table control (in both SAP GUIand Webdynpro).Most of our SAP ABAP consultants would have thought about this business scenario and posted a lot ofthreads in SCN too ;). I see a lot of SCN responses recommends the consultants to convert excel (XLS) fileto Tab Limited File or Comma Separated File (CSV) and to use GUI_UPLOAD for uploading the data intointernal 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 functionmodules and somehow managed to complete the requirement. Have a gaze at the disadvantages in theclassical approach in the following tabular representation, Function Module Disadvantages Does not handle the excel file. Excel file needs to be GUI_UPLOAD converted to Tab Limited or Comma Separated file. – The accuracy of the data and validating is pretty challenging Data processing is very slow and mass data upload is not TEXT_CONVERT_XLS_TO_SAP optimistic. ALSM_EXCEL_TO_INTERNAL_TABLE Data processing is very slow as it processes the data cell by KCD_EXCEL_OLE_TO_INT_CONVERT cell and the count of rows and columns are fixed.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 thoseare available only for logistics and accounting modules. The chances of using this three function module inother 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 islimited to 65,536 and the XLS format is stored in Binary format. Owing to all this complexities, untilNetWeaver 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 betweenXLS and XLSX files.SAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 3
  4. 4. XLSX Upload – a unified approachOpenness of Excel (XLSX Format)XLSX is a default Excel 2007 workbook format. In reality, the XLSX document is a ZIP compressed archivewith a directory structure of XML text documents. As shown in the screenshot below, the XLSX file could beextracted 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 – SPFLIxlsheet1.xml 2. Shared String – SPFLIsharedStrings.xmlHave 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 dataSAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 4
  5. 5. XLSX Upload – a unified approachEach excel sheet and its cells are parsed as an XML document. Every strings in excel are uniquely stored ina separate XML document “sharedstrings.xml” and those indexes are maintained in the respective sheetXMLs whereas the integer values are maintained in the sheet XML itself. Have a glimpse of it in the followingscreenshots,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 filesfrom XLSX package to populate an internal table.APIs for Office ApplicationsAs 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, Loads/ creates an XLSX document. This is the API we use in this document CL_XLSX_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)SAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 5
  6. 6. XLSX Upload – a unified approachXLSX Import – How toThe 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 transformationThe aforesaid steps are applicable for Webdynpro ABAP too by having “Upload UI element” in place.Step 1 – Read the path of uploaded fileGet the file open dialog (on hitting F1) using the static method browse_local_file_open from the classcl_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 thestatic 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 contentInitially, the content of the selected file path should be uploaded into our program using the static methodload_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 calledcl_gui_frontend_services=>fui_upload. As this frontend classes is usable only in SAP GUI basedtransactions (not for webdynpro), we use the XML helper class cl_openxml_helper, which could be used bothin SAP GUI programs and webdynpro applications.SAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 6
  7. 7. XLSX Upload – a unified approachStep 3 – Loading the PackageAs I mentioned before, the entire XSLX file is wrapped up in a compressed package and which is notreadable unless we read the package. For loading the package, we have a class cl_xlsx_document whichhas 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 PackageThe loaded package will be in the format shown below and for reading the data from excel sheet1 we needto read the XML “sharedString.xml” from “XLSXSPFLIxl” and “sheet1.xml” from“XLSXSPFLIxlworksheets” folder. As these files are present in the different folders; we need to differentiateand 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 XMLsWe need to read “Sheet” and “Shared String” XML files which are wrapped up in the loaded package. Thesefiles 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 ).SAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 7
  8. 8. XLSX Upload – a unified approachRead 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 XSLTXSLT: XSL stands for EXtensible Stylesheet Language, and is a style sheet language for XML documentsdriven by W3C. XSLT stands for XSL Transformations. XSLT is a language for transforming XML documentsinto other XML documents or other objects such as HTML, XML or XSL. Typically, input documents are XMLfiles, but anything from which the processor can build an XQuery and XPath Data Model can be used. Theoriginal document is not changed; rather, a new document is created based on the content of an existingone. XSLT is a Turing-complete language, meaning it can perform any calculation that can be performed bya 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.10with 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 TableClick 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”.SAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 8
  9. 9. XLSX Upload – a unified approachFigure 4: Captured from SE80 transaction while attempting to create the XSLT transformation.Processing the Sheet and Shared String XMLThe processing of XSL transformation is much faster than ABAP language and hence we are going to domore 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 thecell data.In XSLT, we can pass only one XML file as a source; whereas we have two XML documents (SheetXML and Shared String XML) which need to be desterilized to form an internal table. Hence we are going topass the sheet XML as a source for XSLT and shared String XML as a parameter my removing the XMLnamespaces. For removing the namespaces we are going to create another transformation. (Our objective isto 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 RemarksZBEN_SHARED_STRING_DEL_NAMESP Source  Shared String Removes the namespace of XML “Shared String XML”. [we are going to pass this XML as a Result  Shared String parameter string to the main XML without namespaces xslt as passing the XML as a parameter is forbidden]ZBEN_XLSX_SHEET_IMPORT Source  Sheet XML Processes the sheet data and shared string data to populate Parameter  Shared an ABAP internal table. String XML without namespace) Result (LT_SPFLI)  ABAP Internal TableTable 3: The details of created XSLT Transformation to read the XSLX fileSAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 9
  10. 10. XLSX Upload – a unified approachBusiness ScenarioI have identified two scenarios one for SAP GUI and other for Webdynpro ABAP just to motivate you try outthis unified approach. You could use the same transformation for both the scenarios. Refer the subsequentsections and try to make out the working scenario by referring to the code snippet showcased in the previoussections. If you find any difficulties, please go through the complete code (ABAP and XSLT in the lastsection)Scenario 1 – Using SAP GUIALV needs to be populated by uploading the data from XLSX document.Step 1: Choose the XLSX file in the selection screenFigure 5: Selection Screen captured from SAP GUI with a single parameter for uploading the excel fileStep 2: On hitting F8, the uplaoded excel file needs to be processed and display the data in the ALV asshown below,Figure 6: ALV which lists the Flight information which has been processed by our classes and transformation.SAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 10
  11. 11. XLSX Upload – a unified approachScenario 2 – Using Webdynpro ABAPTable needs to be loaded by uploading the data from XLSX documentStep 1: Choose the XLSX file in the upload UI elementFigure 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.SAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 11
  12. 12. XLSX Upload – a unified approachTransformationsName ZBEN_XLSX_REMOVE_NAMESPACE Source XML: Shared String XMLParameters 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 Source XML: Sheet1 XML Parameter: P_SHARED_STRING (Shared String with namespaces removed using theParameters previous transformation “ZBEN_XLSX_REMOVE_NAMESPACE”) Result: LT_SPFLI (ABAP Internal Table) Processes the Sheet XML and Shared String XML to populate the Internal Table. Process Flow:Description 1. Read the index from the sheet XML 2. Retrieve the cell content by reading the string available in shared string XMLSAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 12
  13. 13. XLSX Upload – a unified approach <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">SAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 13
  14. 14. XLSX Upload – a unified approach <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>SAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 14
  15. 15. XLSX Upload – a unified approach <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>SAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 15
  16. 16. XLSX Upload – a unified approach <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 ImplementationThis ABAP program will get you the result for the scenario mentioned in the previous chapter. Beforeexecuting 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 *----------------------------------------------------------------------*SAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 16
  17. 17. XLSX Upload – a unified approach 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 XMLSAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 17
  18. 18. XLSX Upload – a unified approach 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 transformationSAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 18
  19. 19. XLSX Upload – a unified approach 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.SAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 19
  20. 20. XLSX Upload – a unified approachPlease note that the methods called in the above action needs to be created in the webdynpro view which issimilar 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.SAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 20
  21. 21. XLSX Upload – a unified approach 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 UploadI knew that creating a XLSX document from scratch would take a lot of time. If you feel so get the file fromthe 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.SAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 21
  22. 22. XLSX Upload – a unified approachRelated ContentReference 1Reference 2Reference 3SAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 22
  23. 23. XLSX Upload – a unified approachCopyright© 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 SystemsIncorporated 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 ofCitrix Systems, Inc.HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C®, World Wide Web Consortium, MassachusettsInstitute 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 mentionedherein 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, andother Business Objects products and services mentioned herein as well as their respective logos are trademarks or registeredtrademarks 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 documentserves 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 ("SAPGroup") for informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors oromissions with respect to the materials. The only warranties for SAP Group products and services are those that are set forth in theexpress warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting anadditional warranty.SAP COMMUNITY NETWORK scn.sap.com© 2012 SAP AG 23

×