To perform a spreadsheet export for any content other than SmartTable or sap.ui.mdc.Table, you have to set up the configuration for the columns and data sources, and you can configure some other additional properties.
If you export data from the SmartTable control, the configuration is already available. You can use it without making any modifications. You can also adapt the configuration to make it suitable for your own requirements. The existing export configuration is attached as an event parameter to the beforeExport event provided by SmartTable. The event is fired once the SmartTable control has finished creating the export configuration. To adapt the export configuration, you have to modify the event parameter by registering an event handler and then adjusting the provided configuration.
If you want to use the export for any content other than the SmartTable control, you have to define your own export configuration.
The export configuration is a JSON object that contains three major parts that are required to process the data export. While the column configuration and the data source information are mandatory, additional properties that are used for the processing are optional. The export configuration needs to be valid, otherwise the export process will be cancelled.
The column configuration is an array of JSON objects that is assigned to the column property of the export configuration.
var exportConfiguration = {
workbook: {
columns: [
{
// Place your column definition here
}
]
}
}First you need to identify all the columns you want to export, since there has to be a column definition object for every column that is exported. Regardless of the data source (OData or JSON array), each row represents an instance of an entity with several properties, and each row is mapped to one of these properties. It is also possible to map multiple properties to a single column.
A column definition object is a JSON object that contains at least one property property that maps the column to the property of the entity. Its value must be of type string or an array of strings and must not be empty; otherwise the column definition is invalid. The string value must contain the name of a property of the entity. If there is no property with the given name, the column in the exported Office Open XML spreadsheet will be empty.
Property Types
Apart from the property property, a column definition can have additional properties. The following properties are used:
-
Type-independent
-
Type-dependent
The following table shows the different kind of properties.
Properties
|
Type-Independent Properties |
Type-Dependent Properties |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
In this section, you can find out more about type-independent properties.
The label property is optional, and its value must be of type string. Its value will be used as column header for the column. If no label property has been provided, the value of the property property will be used instead.
The optional type property defines the data type for this column and needs to match one of the values of the sap.ui.export.EdmType enumeration. If the type property has not been defined or the enumeration does not contain its value, the default type (sap.ui.export.EdmType.String) is used. For more information, see the API Reference: EdmType.
The optional width property defines the column width based on the number of characters that can be visible. The Office Open XML spreadsheet standard uses a width calculation that is not equivalent to the CSS sizes. Therefore, the calculation is handled by the library. If no width property has been provided, or if its value is smaller than 1, the default width is used. The default width is 10 characters. If the column header text has a length that is greater than the actual width, it will override the width with the length of the column header text.
The optional textAlign property defines the horizontal text alignment. Its value must be of type string and either be left, right, or center. Other CSS alignments like begin or end are not supported. If no textAlign property has been provided or its value is empty or not supported, the default alignment is used. The default alignment is defined by the type of the column. This is done by the application using the scenario and the generated Office Open XML spreadsheet, for example, Microsoft Excel.
If you set the alignment for a particular column, the cell content is not always aligned as originally defined for every data type. The application that displays the spreadsheet can ignore the alignment depending on the column's data type or even the content of the cell, for example, in right-to-left scenarios.
The following code shows you an example of a column definition:
var exportConfiguration = {
workbook: {
columns: [
{
property: "Firstname",
width: 15
},
{
property: "Lastname",
width: 15
},
{
property: "User",
label: "Username",
width: 20
},
{
property: "Attempts",
label: "Login Attempts",
type: sap.ui.export.EdmType.Number
},
{
property: "LastLogin",
label: "Last Successful Login",
type: sap.ui.export.EdmType.DateTime
width: 20,
textAlign: "center"
}
]
}
}
Apart from the column configuration, data source configuration is the most important configuration for the export process. Data source configuration is mandatory. It can be a JSON array containing all data, a JSON object, sap.ui.model.ListBinding, or sap.ui.model.TreeBinding. If it is a JSON object, the following properties are required:
Data Source Configuration Properties
|
Property |
Type |
Optional |
Description |
|---|---|---|---|
|
|
|
Defines the type of the data service that provides the data. If it is an OData service, the value |
|
|
|
|
Request URL that is needed to request the data with all the filters and its order. The URL can either be relative or absolute. If the URL is relative, the current origin will be used as a host. |
|
|
|
|
URL of the data service that serves the entity which is requested by the
|
|
|
|
|
Indicator of the line items available through the service. During the export process there is neither a dedicated $count request nor is the
|
|
|
|
|
If set to |
|
|
|
|
Provides additional request headers within an OData batch request. Every property you add to the
|
|
|
|
|
Defines the number of records that are requested from the service with a single request. This is important to make fine adjustments. |
|
|
|
|
Defines the maximum number of rows that can be exported. If not specified, all rows are exported. |
The following code shows you an example of data source configuration:
/* JSON array as dataSource */
var exportConfiguration = {
dataSource: [
// Several line items that contain actual data
]
}
/* OData service as dataSource */
exportConfiguration = {
dataSource: {
type: "odata",
dataUrl: "/sap/opu/odata/sap/MM_PUR_PODWNPAYT_MNTR_SRV/C_PurOrdDownPaymentMntr(P_DisplayCurrency=%27EUR%27)/Results?sap-client=715&$format=json&$select=PurchaseOrder,PurchaseOrderItem,DisplayCurrency,DownPaymentsRequest,NetAmount,PurchasingDocumentStatus&$filter=(Supplier%20eq%20%2710300001%27)&$orderby=PurchasingDocumentStatus%20asc",
serviceUrl: "/sap/opu/odata/sap/MM_PUR_PODWNPAYT_MNTR_SRV",
count: 17491,
useBatch: true,
headers: {
/* Some sample headers DO NOT copy them */
"Accept": "application/json",
"Accept-Language": "en",
"sap-cancel-on-close": "true",
"DataServiceVersion": "2.0",
"x-csrf-token": "XvR_WdN7nCw83ngZnH9lZQ=="
},
sizeLimit: 500
}
}Apart from the mandatory configuration, you can add context information to the generated Office Open XML spreadsheet. This part of the configuration is optional. It is provided within a JSON object that is assigned to the context property within the workbook object of the export configuration. Built-in properties are not visible on a data sheet and stored as document properties in the generated file while custom metadata is visible on a data sheet in the workbook.
Apart from the actual data that is exported, sometimes additional information is required in the spreadsheet. For example, this meta information could be the URL of the system from which the data is exported, the system ID, a timestamp of the export date, or the name of the user who exported this data.
The sap.ui.export.Spreadsheet library always exports the actual data to the main sheet of the Office Open XML spreadsheet but it can also add an additional sheet for meta information. The consuming applications can then assign their own specific names to both sheets.
The following code shows you an example of context configuration:
var exportConfiguration = {
workbook: {
context: {
// Place your context information here
}
}
}You can use the following properties:
Context Information Properties
|
Property (optional) |
Description |
Built-in Context Information |
Custom Metadata Context Information |
|---|---|---|---|
|
|
Adds information about the business application that created a particular file. We recommend to use this property because there are often several similar apps that work on the same data entity but with a different scope or view. This makes it easier to identify where the data comes from if the exported file is shared, for example, among employees and managers. |
|
|
|
|
Identifies the application version through which a particular Office Open XML spreadsheet was created. This can be helpful for debugging because you can identify the version that caused the issues and compare it to previous builds. The more detailed your version information is, the easier it will be to identify your application changes within your source code management system.
|
||
|
|
Adds a self-explanatory title to the document generated. This can be useful if the exported entity is not the main entity of the application in question, for example, company codes within a purchase order application. |
||
|
|
Adds information about the user who created this document. If you do not use this property, the |
||
|
|
Changes the name of the sheet that contains the exported data. If this property is not used, the default value |
|
|
|
|
Changes the name of the sheet that contains the metadata. In contrast to |
||
|
|
An array of JSON objects that follow an exact specification, the so-called meta information groups. Each meta information group has an explicit |
Properties other than the ones listed are ignored during configuration.
The following code shows you an example of context configuration with some of the properties mentioned:
var exportConfiguration = {
workbook: {
context: {
application: "Supplier Invoices List",
version: "6.1",
title: "Supplier Invoices",
modifiedBy: "Doe, John",
sheetName: "Invoices"
}
}
}The following code shows you an example of context configuration with the metainfo property:
var exportConfiguration = {
workbook: {
context: {
metainfo: [
{
name: "Application settings",
items: [
{
key: "Name",
value: "Purchase Orders"
},
{
key: "Version",
value: "1.23.8742-p"
}
]
}
]
}
}
}The reasoning behind this design is that different layers (SAPUI5 framework, SAP Fiori elements, smart controls, and applications) can add metadata without depending on each other. As long as there is at least one valid meta information group within the
metainfoarray, the additional sheet will be shown in the generated file.
The sap.ui.export.Spreadsheet library offers some other additional properties that you can configure. This part of the configuration is also optional.
The following properties are available for configuration:
-
count(typenumber)The value of this property must be positive. This property is only used if
dataSourceis a plain string. If this is the case, it is taken over asdataSource.count. -
worker(typeboolean)The export process runs in a dedicated web worker by default. The
workerproperty allows you to disable this functionality. A web worker runs in a separate thread and does not affect the performance of the browser window's main thread.Although it has some advantages to use a web worker, it can also cause some problems. Especially if SAPUI5 is loaded from a Content Delivery Network (CDN) and is not served by the original host, it depends on the Content Security Policy (CSP) of the server if the export can be processed in a web worker. To resolve problems related to this scenario, you should either add a
worker-srcdirective to your CSP or disable the web worker functionality. If the web worker functionality is disabled, the export process will run in the main thread of the browser window. Currently, theworker-srcdirective is not fully supported by all browsers. If you can't set theworker-srcdirective, you can also adjust the fallback directivescript-src.The
script-srcdirective will also affect all other scripts that are loaded on your page. -
fileName(typestring)Defines a particular name for the generated export file. The file extension is added to the given file name and is always xlsx. To take effect, the property must not be empty.
-
showProgress(typeboolean)The export process shows a progress dialog by default.To prevent this from happening, you can set the
showProgressproperty tofalse.
The sap.ui.export.Spreadsheet library can also handle hierarchical structures.
To get exported, each object of the relevant entity must have the following:
-
A distinct property containing its absolute numerical hierarchy level
-
An order in which parent nodes are followed by their direct child nodes
To enable a hierarchical representation, you have to simply assign the name of the property containing the hierarchy level to the hierarchyLevel property of the workbook object in the export configuration.
The following code shows you an example of hierarchy data:
var exportConfiguration = {
workbook: {
hierarchyLevel: "level"
}
}Relational hierarchy data is not supported.
After you have created a valid configuration, you will have to create a new sap.ui.export.Spreadsheet instance and initialize it with the previously created configuration. After the instance has been initialized, you can start the export process by calling the build method. Everything else will be handled by the export library. The result will be an Office Open XML spreadsheet which is automatically downloaded. The export library does not offer you any events to which you can register. If you need to perform additional steps after the export has been completed, you can use the Promise that is returned by the build method.
The following code sample shows the start of an export:
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/export/Spreadsheet"
], function(Controller, Spreadsheet) {
"use strict";
return Controller.extend("sample.Spreadsheet", {
onExport: function() {
var oExportConfiguration, oExportPromise, oSpreadsheet;
/* Creates the configuration and initializes the spreadsheet export */
oExportConfiguration = this.createExportConfiguration();
oSpreadsheet= new Spreadsheet(oExportConfiguration);
/* Starts the export and returns a Promise */
oExportPromise = oSpreadsheet.build();
oExportPromise.then(function() {
// Here you can perform additional steps after the export has finished
});
},
createExportConfiguration: function() {
var oConfiguration;
// Create a valid export configuration
return oConfiguration;
}
});
});


