问题描述
我有一张包含产品详细信息的表,例如 ID ,说明, price 等.我正在尝试将这些数据导出到Excel中
I have a table with product details like ID, description, price etc.. I am trying to export these data to an Excel.
如果我只是执行 getModel("A")
,然后绑定不嵌套的"A"的几个属性,则可以很好地下载excel.但是,如果我尝试访问任何其他结构,例如 getModel("A").getProperty("/Person/PersonFullName")
,它将保留该列空白.
If I just do getModel("A")
, and then bind few properties of "A", which are not nested, the excel is downloaded fine. But if there is any other structure, which I am trying to access for example, getModel("A").getProperty("/Person/PersonFullName")
, it will leave that column blank.
{ // 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
}
];
},
}
推荐答案
根据 sap/ui/export/Spreadsheet 的API参考, dataSource
设置对象等待以下参数之一:
According to the API reference of sap/ui/export/Spreadsheet
, the dataSource
setting object awaits one of the following arguments:
但是在您的代码中,分配给 dataSource
的 aProduct
是对"A"
模型的引用,是无效.
But in your code, the aProduct
, which is assigned to the dataSource
, is a reference to the "A"
model which is invalid.
应根据模型类型设置 dataSource
:
const odataListBinding = this.byId("myResponsiveTable").getBinding("items");
const serviceEntryPath = "/sap.app/dataSources/<sourceName>/uri"; // See manifest.json for <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
参数.否则,将不会导出嵌套属性.
Note: Please make sure that the corresponding $expand
parameter is included in the dataUrl
. Otherwise, nested properties won't be exported.
- 带有嵌套属性($ expand)的示例
- 来自演示套件
{ // Constructor settings of sap/ui/export/Spreadsheet
dataSource: myJSONModel.getProperty("/SomeCollection"), // returns an array
fileName: "myExportedDataFromPlainJSON.xlsx"
}
样品
- 我的样本具有嵌套属性
- 来自演示套件
这篇关于将业务数据从UI5导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!