绘制到Excel

可以通过Excel绘制扩展将报表绘制到微软的Excel中。导出的Excel文件支持Xls和Xlsx两种格式。

为了能够将报表绘制到Excel中,需要在项目中添加下面的程序集:

l GrapeCity.ActiveReports.Export.Excel.v9.dll

l GrapeCity.ActiveReports.Extensibility.v9.dll

下面的步骤展示了如何将报表绘制到微软的Excel中。

1.    在Visual Studio中创建一个新的Windows窗体应用程序或者打开一个已经存在的。

2.    在打开的Form.cs或者Form.vb中,双击标题栏创建Form_Load事件。

3.    在Form_Load事件中添加下面的代码。

Visual Basic.Net代码

' Provide the page report you want to render.

Dim report As New GrapeCity.ActiveReports.PageReport(New System.IO.FileInfo("C:\Sample_PageReport.rdlx"))

Dim reportDocument As New GrapeCity.ActiveReports.Document.PageDocument(report)

 

' Create an output directory

Dim outputDirectory As New System.IO.DirectoryInfo("C:\MyExcel")

outputDirectory.Create()

 

' Provide settings for your rendering output.

Dim excelSetting As New GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings()

excelSetting.FileFormat = GrapeCity.ActiveReports.Export.Excel.Page.FileFormat.Xls

Dim setting As GrapeCity.ActiveReports.Extensibility.Rendering.ISettings = excelSetting

 

' Set the rendering extension and render the report.

Dim excelRenderingExtension As New GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension()

Dim outputProvider As New GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider(outputDirectory, System.IO.Path.GetFileNameWithoutExtension(outputDirectory.Name))

reportDocument.Render(excelRenderingExtension, outputProvider, excelSetting.GetSettings())

C#代码

// Provide the page report you want to render.

GrapeCity.ActiveReports.PageReport report = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(@"C:\Sample_PageReport.rdlx"));

GrapeCity.ActiveReports.Document.PageDocument reportDocument = new GrapeCity.ActiveReports.Document.PageDocument(report);

 

// Create an output directory

System.IO.DirectoryInfo outputDirectory = new System.IO.DirectoryInfo(@"C:\MyExcel");

outputDirectory.Create();

 

// Provide settings for your rendering output.

GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings excelSetting = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings();

excelSetting.FileFormat = GrapeCity.ActiveReports.Export.Excel.Page.FileFormat.Xls;

GrapeCity.ActiveReports.Extensibility.Rendering.ISettings setting = excelSetting;

 

//Set the rendering extension and render the report.

GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension excelRenderingExtension = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension();

GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider(outputDirectory, System.IO.Path.GetFileNameWithoutExtension(outputDirectory.Name));

reportDocument.Render(excelRenderingExtension, outputProvider, excelSetting.GetSettings());