How to download data from a table using the on-demand resource streaming technique in NW 2004s combined with a ToolBarLinkToURL-UI-Element to open the Excel resource instantly. This technique does not require opening a Web Dynpro popup window with a LinkToURL-UI-element pointing to a cached Excel resource. This sample application is implemented in two separate components: a reusable service component for Excel export and a client component displaying table data.
Applies To: Web Dynpro for Java UI Development, SAP NetWeaver 7.0
Code Sample: TutWD_ExcelExport_NW04s.zip - WD Sample Project ZIP for this Code Tutorial based on NW04s SP Stack 8
Level of complexity: Intermediate
Time required for completion: 1.5 hrs.
Created by: Bertram Ganz, SAP AG
Submitted: April 23, 2007
This code tutorial demonstrates how to implement the following functionality in a Web Dynpro Java application based on NW 04s:
- how to export table data to a MIME resource (Excel file) on-demand
- how to link to such an on-demand resource in a table tool bar using a ToolbarLinkToURL-UI-element
The following screenshots demonstrate the required user interaction:
- user clicks ToolBarLinkToURL-UI-element to download table data exported to an Excel file.
- a popup window occurs which asks the user how to handle the downloaded resource: save or display.
- the mime file or Excel resource is opened with the related progam installed on client side (MS Excel).
In comparison to the Excel Export Sample Application for NW 04 the NW 04s solution described in this code tutorial does not require to open a Web Dynpro popup window any more. This can be achieved by streaming the Excel resource content on-demand although the resource object is initially being created. This empty resource object is called 0-byte-resource. We can initially link to the URL of this resource using a ToolbarLinkToURL-UI-element in the table tool bar without knowing its binary content yet.
Related Sample Project
You can download the complete sample project implementing this Web Dynpro code tutorial here.
Creating a Reusable Excel Export Component
The first part of this code tutorial describes how to create a faceless Web Dynpro component encapsulating the Excel export functionality.
To use this Excel export component (service component) in a client component displaying table data the following steps are required:
- Client component must invoke the
initialize()-method of the service component passing references to the table's data node and to the table column's header texts.
- Client component must bind the property reference of its ToolbarLinkToURL-UI-element to the attribute ResourceURL in the interface context of the service component (implies context mapping). The Excel export component calculates this URL and exposes it to the client component.
- Client component must bind the property imageSourcee of its ToolbarLinkToURL-UI-element to the attribute ImageURL in the interface context of the service component (implies context mapping). The Excel export component calculates this image URL and exposes it to the client component.
Defining the Component Controller Context
The context of the component controller looks like this ...
cardinality = 1..1
type = string
type = Resource
type = String
The two important attributes are Resource and ResourceOnDemandStreamCalc. They are used as a combination for streaming resource content to the client on-demand. The other two attributes are used for exposing the resource URL and the resource icon (Excel icon) URL to the client component using the service component for exporting table data as Excel file.
Implementing the Component Controller
Initializing Member Variables
First we declare and initialize some private member variables in the component controller class:
Initializing the Componet Controller in
wdDoInit()-hook method of the component controller we initialize the member variables
calcAttrPointer and then generate the URL of the Excel image icon which is deployed with the Excel export component.
Implementing Private Method
This private method is invoked in the calculated context attribute getter method where the on-demand resource content is actually being created and in the
initialize()-method which is invoked by the client component. We create a new and empty 0-byte resource object, store it in the component controller context, get its URL and store this URL in the context too.
Implementing Public Method
initialize()-method is invoked by the client component passing references to the table's dataNode and to its column information (display texts of the table column headers). This method delegates to the
initializeOnDemandExcelResource()-method where a new 0-byte resource object is stored in the component context.
Implementing the calculated context attribute getter
To stream the content of the Excel resource to the client on-demand we implement a calculated context attribute getter method:
Implementing the Excel Export Code
The following lines of code are implemented in the ExcelExport component controller's private user coding area:
Mapping the Component Interface Controller Context
To expose the context attributes IconURL and ResourceURL to the client component we must add mapped attributes to the interface context of the Excel export component:
Implementing the Component Interface Controller
Finally we declare and implement the public method
initialize() to the interface controller of the Excel export component. This method is invoked by the client component and delegates to the same method in the component controller.
Using the ExcelExport Component inside the Table Component
Using the Excel export component in the client component (table display component) is quite simple. First we add a component usage relation to the ExcelExport component in the table component.
Mapping the View Controller Context to the Interface Context of the ExcelExport Component
In the next step we define a context mapping relation from the TableDisplayView controller to the Interface Controller of the used ExcelExport service component. This can be defined in the data modeler of the Web Dynpro Tools easily:
Adding a ToolBarLinkToURL UI Element to the View Layout
After mapping the two URL attributes in teh view context to the interface context of the used ExcelExport component we can bind the corresponding properties in a new ToolBarLinkToURL-UI-element to them. At runtime the transfer of the URL values from the ExcelExport service component to the TableDisplay client component is fully automated based on data binding and context mapping.
Implementing the View Controller
Finally we implement one single line of custom code within the
wdDoInit()-hook-method of the TableDisplayView view controller class. We just invoke the
initialize()-method exposed by the
IExternal-API of the ExcelExport component interface controller.
The required controller usage relation is already added to the view controller properties based on the context mapping definition done in the data modeler.
The client component must pass a Map with key-displaytext-pairs for the to the Excel export service component. The keys are the technical context attribute names and the values are the corresponding displaytexts used for column header texts. Please note, that the described Excel export component cannot create column header texts with empty spaces. This means in case a displaytext comprises spacial characters, the excel export component trims them because XML element names (to be created) must adhere to XML rules (that is, they cannot include special characters, spaces, and so on. A LinkedHashMap is used to keep a stable order of key-value pairs (context attribute names, column header texts) passed by the client component.
Read more about tpics covered in this tutorial:
- Web Dynpro Tutorial: Excel Export Sample Application for NW 04
- Web Dynpro Tutorial: Downloading and Uploading Files in Web Dynpro - NW04s
- Web Dynpro Article: Uploading and Downloading Files in Web Dynpro Tables - NW04s