This topic describes how to create, setup and export XAF reports in a non-XAF application. Since XAF stores reports in the database, and XAF reports use Object Space to retrieve data, you should manually connect to the XAF database and create an Object Space in a non-XAF application.
本主题介绍如何在非 XAF 应用程序中创建、设置和导出 XAF 报表。由于 XAF 在数据库中存储报表,并且 XAF 报表使用对象空间来检索数据,因此应手动连接到 XAF 数据库并在非 XAF 应用程序中创建对象空间。
Tip 提示
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=T275059
完整的示例项目可在 DevExpress 代码示例数据库中找到,http://www.devexpress.com/example=T275059
.
1. Implement the IReportObjectSpaceProvider and IObjectSpaceCreator interfaces.
1. 实现 IReport 对象空间提供程序和 I对象空间创建器接口。
using DevExpress.ExpressApp; using DevExpress.ExpressApp.ReportsV2; using DevExpress.Persistent.Base.ReportsV2; // ... public class MyReportObjectSpaceProvider : IReportObjectSpaceProvider, IObjectSpaceCreator { IObjectSpaceProvider objectSpaceProvider; IObjectSpace objectSpace; public MyReportObjectSpaceProvider(IObjectSpaceProvider objectSpaceProvider) { this.objectSpaceProvider = objectSpaceProvider; } public void DisposeObjectSpaces() { if (objectSpace != null) { objectSpace.Dispose(); objectSpace = null; } } public IObjectSpace GetObjectSpace(Type type) { if (objectSpace == null) { objectSpace = objectSpaceProvider.CreateObjectSpace(); } return objectSpace; } public IObjectSpace CreateObjectSpace(Type type) { return objectSpaceProvider.CreateObjectSpace(); } }
2. Inherit the ReportDataSourceHelper class and override the CreateReportObjectSpaceProviderCore method.
2. 继承报表数据源帮助器类并重写创建报表对象空间提供程序核心方法。
using DevExpress.ExpressApp; using DevExpress.ExpressApp.ReportsV2; using DevExpress.Persistent.Base.ReportsV2; // ... public class MyReportDataSourceHelper : ReportDataSourceHelper { IObjectSpaceProvider objectSpaceProvider; public MyReportDataSourceHelper(IObjectSpaceProvider objectSpaceProvider) : base(null) { this.objectSpaceProvider = objectSpaceProvider; } protected override IReportObjectSpaceProvider CreateReportObjectSpaceProvider() { return new MyReportObjectSpaceProvider(objectSpaceProvider); } }
3. Create an Object Space Provider using the approach from the Access XAF Application Data in a non-XAF Application article and register the required types in the Types Info Subsystem.
3. 使用非 XAF 应用程序文章中的 Access XAF 应用程序数据的方法创建对象空间提供程序,并在"类型信息子系统"中注册所需的类型。
An example for the Entity Framework.
实体框架的示例。
using DevExpress.ExpressApp; using DevExpress.ExpressApp.DC; using DevExpress.ExpressApp.EF; using DevExpress.Persistent.BaseImpl.EF; // ... connectionString = @"Integrated Security=SSPI;Pooling=false;MultipleActiveResultSets=True;Data Source=(localdb)v11.0;Initial Catalog=ExportReportDemoEF"; EFObjectSpaceProvider objectSpaceProvider = new EFObjectSpaceProvider(typeof(ExportReportDemoEFDbContext), connectionString); ((TypesInfo)XafTypesInfo.Instance).AddEntityStore(objectSpaceProvider.EntityStore); XafTypesInfo.Instance.RegisterEntity(typeof(Person));
An example for XPO.
using DevExpress.ExpressApp; using DevExpress.ExpressApp.DC; using DevExpress.ExpressApp.DC.Xpo; using DevExpress.ExpressApp.Xpo; using DevExpress.Persistent.BaseImpl; // ... connectionString = @"Integrated Security=SSPI;Data Source=(localdb)v11.0;Initial Catalog=ExportReportDemo"; XpoTypesInfoHelper.ForceInitialize(); ITypesInfo typesInfo = XpoTypesInfoHelper.GetTypesInfo(); XpoTypeInfoSource xpoTypeInfoSource = XpoTypesInfoHelper.GetXpoTypeInfoSource(); typesInfo.RegisterEntity(typeof(Person)); typesInfo.RegisterEntity(typeof(ReportDataV2)); ConnectionStringDataStoreProvider dataStoreProvider = new ConnectionStringDataStoreProvider(connectionString); XPObjectSpaceProvider objectSpaceProvider = new XPObjectSpaceProvider(dataStoreProvider, typesInfo, xpoTypeInfoSource);
4. Use the approach described in the How to: Print a Report Without Displaying a Preview topic to access and print the report.
4. 使用"如何:打印报表而不显示预览主题"中所述的方法访问和打印报表。
using DevExpress.ExpressApp; using DevExpress.XtraReports.UI; using DevExpress.ExpressApp.ReportsV2; // ... IObjectSpace objectSpace = objectSpaceProvider.CreateObjectSpace(); ReportDataV2 reportData = objectSpace.FindObject<ReportDataV2>( new BinaryOperator("DisplayName", "Employees Report")); XtraReport report = ReportDataProvider.ReportsStorage.LoadReport(reportData); MyReportDataSourceHelper reportDataSourceHelper = new MyReportDataSourceHelper(objectSpaceProvider); ReportDataProvider.ReportObjectSpaceProvider = new MyReportObjectSpaceProvider(objectSpaceProvider); reportDataSourceHelper.SetupBeforePrint(report); report.ExportToPdf("report.pdf");