Skip to main content Skip to footer

ActiveReports 7 Exports from the Viewer

With the release of ActiveReports 7, we have provided users of Data Dynamics Reports and ActiveReports with an upgrade that adds significant new features. When we were creating the new viewer control, one thing we didn't bring over from Data Dynamics Reports was a built-in export from the viewer. This is largely because the number of toolbar buttons on the viewer is already staggering. If you want to export the current report, we expose an Export method on the viewer control. In a future release we'll add the export option to the standalone designer and viewer applications as well. But that doesn't help if you want to provide that functionality now and you don't want to write the code from scratch. Well, when we're done with this post, you'll have a class that will create a toolbar button for you to provide exports to your users. You can download the complete project here: AR7ExportToolbar (23KB, Visual Studio 2012 project. If you are not using VS2012, you can just copy the classes out of the project and add the bitmap resource image). Let's get to it! If you look at the signature of the Export method you'll see that it takes just a few parameters: an object implementing IDocumentExportEx, the destination FileInfo or Stream, and, optionally, the page range. We'll make use of this to implement the export functionality. The classes implementing the IDocumentExportEx interface are all found in Section-based exports. Despite the name, these exports can handle both SectionDocument and PageDocument reports, because internally they convert a PageDocument into a SectionDocument at run time. Something important that we considered in our design was that you may not want to ship all of the exports with your application. That is why we provide two ways of excluding exports that do not require you to modify the toolbar button code at all.

  1. A collection that you can modify at runtime to remove exports that you don't want to make available.
  2. Runtime-checking to ensure that each export is available prior to displaying the list to the user.

Let's start by looking at how to implement a single export. If you open the project, you'll find the SectionReportExporter class. This abstract class provides the base capability to load up the export type at run time, and it returns the IDocumentExportEx object that does the exporting. It also provides some helper properties that the UI uses to provide a file dialog filter and default extension. (You may also notice an Export method that will allow you to do more advanced exports in the future, but for now, these stubs throw a NotImplementedException.) The reflection code is fairly basic: we take in the type name and use Type.GetType to load it at runtime. If it fails, then we know to hide the toolbar button.

protected Type CreateType()  
return Type.GetType(FullTypeName, false, true);  

Once we have the type, we can use the Activator class to create an instance of it, and return it from the GetExporter method as an implementation of IDocumentExportEx. Each of the exports inherits from this class and passes in the type name, and returns the appropriate file dialog values. Now that we have the IDocumentExportEx object, we can use it in the toolbar button. In the ExportToolStripButton class, see the OnExportItemClicked method.

using (var saveAsDlg = new SaveFileDialog())  
saveAsDlg.DefaultExt = export.DefaultExtension;  
saveAsDlg.Filter = export.FileDialogFilter;  

if (saveAsDlg.ShowDialog(host) != DialogResult.OK)  

host.Export(exporter, new FileInfo(saveAsDlg.FileName));  

This creates our Save As dialog, passes in our exporter's preferred file filter and default extension, and then calls the Export method of the viewer control, or "host" in this method. The rest of the code in the project deals with the management of the allowed exports and filters out any exports that aren't available at runtime. I've made it easy to use this code in your own project. Add the toolbar button image resource, and then the classes. Once you add those to your project, you can add the button with code like the following:

ExportToolStripButton button = new ExportToolStripButton(viewer1);  
// button.AvailableExports.Remove("PDF");  
viewer1.Toolbar.MainBar.Items.Insert(3, button);

The button loads up the available exports at run time. If you want to remove some from the list, you can modify the AvailableExports dictionary on the button (see the commented out code above) before you add the button to the toolstrip. If you were to use this same code in the button click event, the exports you remove would still display if the user dropped down the list without clicking the button. I hope you find this code useful. I plan on maintaining it with additional features and bug fixes as we make changes to ActiveReports 7 in the future. 'Til next time -James


comments powered by Disqus