将业务数据从 UI5 导出到 Excel
Export business data from UI5 to Excel
我有一个 table,其中包含产品详细信息,例如 ID、description、price 等。我正在尝试将这些数据导出到 Excel.
问题
如果我只做 getModel("A")
,然后绑定 而非 嵌套的“A”的几个属性,则 excel 可以正常下载。但是如果有任何其他结构,我试图访问,例如 getModel("A").getProperty("/Person/PersonFullName")
,它将使 列空白 。
{ // Controller
onExport: function() {
var aCols, aProducts, oSettings;
aCols = this.createColumnConfig();
aProducts = this.getView().getModel("A"); // A has nested/deep entities
oSettings = {
workbook: { columns: aCols },
dataSource: aProducts
};
var oSpreadsheet = new Spreadsheet(oSettings); // required "sap/ui/export/Spreadsheet"
oSpreadsheet.build();
},
createColumnConfig: function() {
return [
{
label: 'Product ID',
property: 'name'
},
{
label: 'Category',
property: 'BillTo/BillToName', //Not able to get this property
}
];
},
}
根据 API reference of sap/ui/export/Spreadsheet
,dataSource
设置对象等待以下参数之一:
- Map of data source properties, // See API reference for available options
- URL to an OData service
- JSON array
但是在您的代码中,分配给 dataSource
的 aProduct
是对 "A"
模型的引用,该模型 无效 .
应设置dataSource
,根据模型类型,相应:
与ODataModel
const odataListBinding = this.byId("myResponsiveTable").getBinding("items");
const serviceEntryPath = "/sap.app/dataSources/<em><sourceName></em>/uri"; // 请参阅 manifest.json 以获取 <sourceName>
const serviceUrl = this.getOwnerComponent().getManifestEntry(serviceEntryPath);
{ // Constructor settings of sap/ui/export/Spreadsheet
dataSource: {
type: "OData",
useBatch: true,
serviceUrl: serviceUrl,
headers: odataListBinding.getModel().getHeaders(),
dataUrl: odataListBinding.getDownloadUrl(), // serviceUrl + "/Orders" + added queries ($expand, $select, ...)
count: odataListBinding.getLength(),
sizeLimit: /*e.g.*/1000,
},
worker: true, // false if working with mock server or if CSP is enabled.
fileName: "myExportedDataFromODataV2.xlsx"
}
注意:请确保dataUrl
中包含相应的$expand
参数。否则,将不会导出嵌套属性。
示例
- 我的示例 nested properties ($expand)
- 样本来自 Demo Kit
使用客户端模型(例如 JSONModel
)
{ // Constructor settings of sap/ui/export/Spreadsheet
dataSource: myJSONModel.getProperty("/SomeCollection"), // returns an array
fileName: "myExportedDataFromPlainJSON.xlsx"
}
示例
- 我的示例 nested properties
- 样本来自 Demo Kit
我有一个 table,其中包含产品详细信息,例如 ID、description、price 等。我正在尝试将这些数据导出到 Excel.
问题
如果我只做 getModel("A")
,然后绑定 而非 嵌套的“A”的几个属性,则 excel 可以正常下载。但是如果有任何其他结构,我试图访问,例如 getModel("A").getProperty("/Person/PersonFullName")
,它将使 列空白 。
{ // Controller
onExport: function() {
var aCols, aProducts, oSettings;
aCols = this.createColumnConfig();
aProducts = this.getView().getModel("A"); // A has nested/deep entities
oSettings = {
workbook: { columns: aCols },
dataSource: aProducts
};
var oSpreadsheet = new Spreadsheet(oSettings); // required "sap/ui/export/Spreadsheet"
oSpreadsheet.build();
},
createColumnConfig: function() {
return [
{
label: 'Product ID',
property: 'name'
},
{
label: 'Category',
property: 'BillTo/BillToName', //Not able to get this property
}
];
},
}
根据 API reference of sap/ui/export/Spreadsheet
,dataSource
设置对象等待以下参数之一:
- Map of data source properties, // See API reference for available options
- URL to an OData service
- JSON array
但是在您的代码中,分配给 dataSource
的 aProduct
是对 "A"
模型的引用,该模型 无效 .
应设置dataSource
,根据模型类型,相应:
与ODataModel
const odataListBinding = this.byId("myResponsiveTable").getBinding("items");
const serviceEntryPath = "/sap.app/dataSources/<em><sourceName></em>/uri"; // 请参阅 manifest.json 以获取 <sourceName>
const serviceUrl = this.getOwnerComponent().getManifestEntry(serviceEntryPath);
{ // Constructor settings of sap/ui/export/Spreadsheet
dataSource: {
type: "OData",
useBatch: true,
serviceUrl: serviceUrl,
headers: odataListBinding.getModel().getHeaders(),
dataUrl: odataListBinding.getDownloadUrl(), // serviceUrl + "/Orders" + added queries ($expand, $select, ...)
count: odataListBinding.getLength(),
sizeLimit: /*e.g.*/1000,
},
worker: true, // false if working with mock server or if CSP is enabled.
fileName: "myExportedDataFromODataV2.xlsx"
}
注意:请确保dataUrl
中包含相应的$expand
参数。否则,将不会导出嵌套属性。
示例
- 我的示例 nested properties ($expand)
- 样本来自 Demo Kit
使用客户端模型(例如 JSONModel
)
{ // Constructor settings of sap/ui/export/Spreadsheet
dataSource: myJSONModel.getProperty("/SomeCollection"), // returns an array
fileName: "myExportedDataFromPlainJSON.xlsx"
}
示例
- 我的示例 nested properties
- 样本来自 Demo Kit