Microsoft Report Viewer is a powerful .NET control allowing the use of RDL and RDLC reports in WinForms and ASP.NET applications. It enables users to view and export reports to different formats. The control is included with Microsoft Visual Studio 2005 and 2008, and is also available as a free download from Microsoft.
Report Viewer can generate reports independently using a built-in engine (local mode), or it can display reports that are generated on a Microsoft SQL Server Reporting Services Report Server (remote mode).
When working in the remote mode, Report Viewer can export reports to all formats installed on the Report Server to which it is connected. Therefore, to export reports to Microsoft Word formats you only need to install Aspose.Words for Reporting Services on the server.
When working in the local mode however, Report Viewer does not connect to a Report Server and the list of export formats is limited to only a few built-in formats.
By installing Aspose.Words for Reporting Services on a development machine and following the steps below, you will be able to add the ability to export to Microsoft Word formats from Report Viewer working in the local mode.
Step 1. Add a Reference to Aspose.Words.ReportingServices.dll to your project.
Open your project in Visual Studio, right click on the References folder and select Add Reference. Click the Browse tab and browse to the following assembly:
- If using Microsoft Report Viewer 2005 – browse to <InstallDir>/Bin/ReportViewer2005/Aspose.Words.ReportingServices.dll
- If using Microsoft Report Viewer 2008 - browse <InstallDir>/Bin/ReportViewer2008/Aspose.Words.ReportingServices.dll
where <InstallDir> is the directory, where you installed or unpacked Aspose.Words for Reporting Services.
Adding a Reference to Aspose.Words.ReportingServices.dll to your project.
Step 2. Copy and paste the following AddExtension method into your project.
This method adds the specified rendering extension to the list of supported extensions in Microsoft Report Viewer using private reflection.
[C#]
using System.Collections;
using System.Reflection;
using Microsoft.ReportingServices.ReportRendering;
// Use one of the two namespaces below depending on whether you are developing
// a WinForms or WebForms application.
using Microsoft.Reporting.WinForms;
// -- or --
// using Microsoft.Reporting.WebForms;
/// <summary>
/// Adds the specified rendering extension to the specified ReportViewer instance.
/// </summary>
/// <param name="viewer">A ReportViewer control instance.</param>
/// <param name="formatName">The name of the export format to appear on the dropdown list.</param>
/// <param name="extensionType">The class of the rendering extension to add.</param>
private static void AddExtension(ReportViewer viewer, string formatName, Type extensionType)
{
const BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
// CommonService.ListRenderingExtension is an internal method that returns a list of supported
// rendering extensions. This list is also stored in a class field so we can simply get this list
// and add Aspose.Words for Reporting Services rendering extensions to make Microsoft Word
// export formats appear on the dropdown.
// Get the service type.
FieldInfo previewService = viewer.LocalReport.GetType().GetField("m_previewService", flags);
// Get the ListRenderingExtensions method info.
MethodInfo listRenderingExtensions = previewService.FieldType.GetMethod("ListRenderingExtensions", flags);
// Obtan a list of existing rendering extensions.
IList extensions = listRenderingExtensions.Invoke(previewService.GetValue(viewer.LocalReport), null) as IList;
// LocalRenderingExtensionInfo is a class that holds information about a rendering extension.
// We should create an instance of this class to add the info about the specified extension.
// Since the IRenderingExtension interface is defined in the Microsoft.ReportViewer.Common
// assembly, use this trick to obtain the corresponding Assembly instance. This will work for
// both Report Viewer 2005 (8.0) and 2008 (9.0).
Assembly commonAssembly = typeof(IRenderingExtension).Assembly;
// Now, get the LocalRenderingExtensionInfo type as it is defined in the same assembly.
Type localRenderingExtensionInfoType = commonAssembly.GetType("Microsoft.Reporting.LocalRenderingExtensionInfo");
// Get the LocalRenderingExtensionInfo constructor info.
ConstructorInfo ctor = localRenderingExtensionInfoType.GetConstructor(
flags,
null,
new Type[] { typeof(string), typeof(string), typeof(bool), typeof(Type), typeof(bool) },
null);
// Create an instance of LocalRenderingExtensionInfo.
object instance = ctor.Invoke(new object[] { formatName, formatName, true, extensionType, true });
// Finally, add the info about our rendering extension to the list.
extensions.Add(instance);
}
[VB .NET]
Imports System.Collections
Imports System.Reflection
Imports Microsoft.ReportingServices.ReportRendering
// Use one of the two namespaces below depending on whether you are developing
// a WinForms or WebForms application.
Imports Microsoft.Reporting.WinForms
// -- or --
// Imports Microsoft.Reporting.WebForms
'' Adds the specified rendering extension to the specified ReportViewer instance.
Private Shared Sub AddExtension(ByVal viewer As ReportViewer, ByVal formatName As String, ByVal extensionType As Type)
Const flags As BindingFlags = BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Instance
' CommonService.ListRenderingExtension is an internal method that returns a list of supported
' rendering extensions. This list is also stored in a class field so we can simply get this list
' and add Aspose.Words for Reporting Services rendering extensions to make Microsoft Word
' export formats appear on the dropdown.
' Get the service type.
Dim previewService As FieldInfo = viewer.LocalReport.GetType().GetField("m_previewService", flags)
' Get the ListRenderingExtensions method info.
Dim listRenderingExtensions As MethodInfo = previewService.FieldType.GetMethod("ListRenderingExtensions", flags)
' Obtan a list of existing rendering extensions.
Dim extensions As IList = TryCast(listRenderingExtensions.Invoke(previewService.GetValue(viewer.LocalReport), Nothing), IList)
' LocalRenderingExtensionInfo is a class that holds information about a rendering extension.
' We should create an instance of this class to add the info about the specified extension.
' Since the IRenderingExtension interface is defined in the Microsoft.ReportViewer.Common assembly, use this trick
' to obtain the corresponding Assembly instance. This will work for both Report Viewer 2005 (8.0) and 2008 (9.0).
Dim commonAssembly As System.Reflection.Assembly = GetType(IRenderingExtension).Assembly
' Now, get the LocalRenderingExtensionInfo type as it is defined in the same assembly.
Dim localRenderingExtensionInfoType As Type = commonAssembly.GetType("Microsoft.Reporting.LocalRenderingExtensionInfo")
' Get the LocalRenderingExtensionInfo constructor info.
Dim ctor As ConstructorInfo = localRenderingExtensionInfoType.GetConstructor(flags, Nothing, New Type() { GetType(String), GetType(String), GetType(Boolean), GetType(Type), GetType(Boolean) }, Nothing)
' Create an instance of LocalRenderingExtensionInfo.
Dim instance As Object = ctor.Invoke(New Object() { formatName, formatName, True, extensionType, True })
' Finally, add the info about our rendering extension to the list.
extensions.Add(instance)
End Sub
Step 3. Invoke the AddExtension method from your code.
You can call AddExtension (shown in the previous step) whenever you need to add Aspose.Words for Reporting Services export formats to a Report Viewer control instance. Consider calling from the Form_Load or Page_Load event handler of a WinForms or ASP .NET application.
You can add all or only some export Aspose.Words for Reporting Services export formats. You can specify any display name for the formats to appear in Report Viewer.
To add Aspose.Words for Reporting Services export formats to Microsoft Report Viewer in local mode, use the following code:
[C#]
AddExtension(reportViewer1, "DOC - Word Document via Aspose.Words",
typeof(Aspose.Words.ReportingServices.DocRenderer));
AddExtension(reportViewer1, "DOCX - Office Open XML via Aspose.Words",
typeof(Aspose.Words.ReportingServices.DocxRenderer));
AddExtension(reportViewer1, "RTF - Rich Text Format via Aspose.Words",
typeof(Aspose.Words.ReportingServices.RtfRenderer));
AddExtension(reportViewer1, "XML - WordprocessingML via Aspose.Words",
typeof(Aspose.Words.ReportingServices.WordMLRenderer));
AddExtension(reportViewer1, "HTML - Web Page via Aspose.Words",
typeof(Aspose.Words.ReportingServices.HtmlRenderer));
AddExtension(reportViewer1, "MHTML - Web Archive via Aspose.Words",
typeof(Aspose.Words.ReportingServices.MhtmlRenderer));
AddExtension(reportViewer1, "TXT - Plain Text via Aspose.Words",
typeof(Aspose.Words.ReportingServices.TxtRenderer));
[VB .NET]
AddExtension(reportViewer1, "DOC - Word Document via Aspose.Words",
GetType(Aspose.Words.ReportingServices.DocRenderer))
AddExtension(reportViewer1, "DOCX - Office Open XML via Aspose.Words",
GetType(Aspose.Words.ReportingServices.DocxRenderer))
AddExtension(reportViewer1, "RTF - Rich Text Format via Aspose.Words",
GetType(Aspose.Words.ReportingServices.RtfRenderer))
AddExtension(reportViewer1, "XML - WordprocessingML via Aspose.Words",
GetType(Aspose.Words.ReportingServices.WordMLRenderer))
AddExtension(reportViewer1, "HTML - Web Page via Aspose.Words",
GetType(Aspose.Words.ReportingServices.HtmlRenderer))
AddExtension(reportViewer1, "MHTML - Web Archive via Aspose.Words",
GetType(Aspose.Words.ReportingServices.MhtmlRenderer))
AddExtension(reportViewer1, "TXT - Plain Text via Aspose.Words",
GetType(Aspose.Words.ReportingServices.TxtRenderer))
Step 4. Test the new export formats.
Run your application and you should notice a number of new export formats available in the Export dropdown list in Report Viewer. Select one of the formats and run export. Verify the document is created the way you expected.
New export formats appear in Report Viewer running in local mode.