|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
The ReportExporters uses the Microsoft Report Viewer Redistributable 2005. You can download it from here. Also, if you have installed .NET Framework 3.5, you can try to change the references to the libraries from the Microsoft Report Viewer Redistributable 2008. IntroductionSome time ago, I was looking for a solution for exporting data from a While working with SQL Server 2005 Reporting Services, I found out that the component
Classes OverviewI have written a few interfaces, a lot of data model classes, and RDLC element wrapper classes. These are the main ones:
The simplest usage of the DataGridView myDataGridView;
...
//Create DataGridViewReportDataAdapter instance
IReportDataAdapter reportDataAdapter = new DataGridViewReportDataAdapter(myDataGridView);
//Create WinFormsReportExporter instance for reportDataAdapter
IReportExporter winFormsReportExporter = new WinFormsReportExporter(reportDataAdapter);
//Execute method ExportToXls to get Excel file content
MemoryStream xlsFileData = winFormsReportExporter.ExportToXls();
...
//Than xlsFileData can be saved to local disk or etc.
ReportColumn Properties
ReportDataColumn Properties
BaseStyle Properties
ReportStyle Properties
Demo ApplicationIn the demo application, I want to show you an example of exporting data of different types ( Here is the result:
For binding as a datasource, I'm using an array of public class GImage
{
// Methods
public GImage(SearchResult _searchResult, string _searchTag);
// Properties
public string FullFileName { get; set; }
public double ImageSize { get; set; }
public string ImageUrl { get; set; }
public string SearchTag { get; set; }
public byte[] ThumbnailData { get; set; }
public Image ThumbnailImage { get; set; }
public string ThumbnailUrl { get; set; }
}
Custom ReportDataAdapterIn order to change metadata (columns width, formatting, public class GImageReportDataAdapter : DataGridViewReportDataAdapterter
{
private bool useGrouping;
public GImageReportDataAdapter(DataGridView _dataGridView,
bool _useGrouping) : base(_dataGridView)
{
useGrouping = _useGrouping;
}
public override ReportColumnCollection GetColumns()
{
ReportColumnCollection toRet = new ReportColumnCollection();
ReportColumnCollection baseColumns = base.GetColumns();
ReportDataColumn rcSearchTag = baseColumns[0] as ReportDataColumn;
ReportDataColumn rcFullFileName = baseColumns[1] as ReportDataColumn;
ReportDataColumn rcThumbnailUrl = baseColumns[2] as ReportDataColumn;
ReportDataColumn rcImageSize = baseColumns[3] as ReportDataColumn;
ReportDataColumn rcImageUrl = baseColumns[4] as ReportDataColumn;
ReportDataColumn rcThumbnailImage = baseColumns[5] as ReportDataColumn;
ReportDataColumn rcThumbnailData = baseColumns[6] as ReportDataColumn;
//Replace ReportDataColumn to ReportHyperlinkColumn
ReportHyperlinkColumn hyperlinkColumnImageUrl =
ReportHyperlinkColumn.ReplaceDataColumn(rcImageUrl);
//set ImageUrl column as Hyperlink for ThumbnailUrl Column
rcThumbnailUrl.HyperlinkColumn = hyperlinkColumnImageUrl;
// Change ReportControl for ThumbnailData(byte[]) to Image instead TextBox
CellViewImage databaseCellViewImage =
CellViewImage.CreateDatabaseImage(ImageMIMEType.Jpeg);
databaseCellViewImage.Properties.Sizing = ImageSizing.FitProportional;
rcThumbnailData.DataCellViewType = databaseCellViewImage;
toRet.Add(rcSearchTag);
toRet.Add(rcFullFileName);
toRet.Add(rcThumbnailUrl);
toRet.Add(rcImageSize);
// add ReportHyperlinkColumn instead ReportDataColumn
toRet.Add(hyperlinkColumnImageUrl);
toRet.Add(rcThumbnailImage);
//Skip rcThumbnailData because thumbnailDataDataGridViewImageColumn
//in DataGridView is invisible
if (this.dataGridView.Columns[6].Visible)
{
toRet.Add(rcThumbnailData);
}
#region Apply custom formatting for ImageSize ReportDataColumn
rcImageSize.DefaultCellStyle.Format = "N2";
rcImageSize.TemplateFormat = "{0} Kb";
#endregion
#region Set custom BackgroundImage for FullFileName ReportDataColumn
//Get embedded image
Stream cellBackgroundImageStream =
GetResourceStream(Assembly.GetExecutingAssembly(),
"cellBackground.png");
if (cellBackgroundImageStream != null)
{
BackgroundImage bgrdImage = new BackgroundImage(
new EmbeddedImage("cellBackground_png",
Image.FromStream(cellBackgroundImageStream)));
rcFullFileName.DefaultCellStyle.BackgroundImage = bgrdImage;
}
#endregion
#region Apply custom border to all reportDataColumns instead first(rcSearchTag)
Border customBorder = new Border();
customBorder.Color = Color.Red;
customBorder.Style = System.Web.UI.WebControls.BorderStyle.Dashed;
customBorder.Width = new System.Web.UI.WebControls.Unit(2,
System.Web.UI.WebControls.UnitType.Point);
foreach (ReportColumn rColumn in toRet)
{
if ((rColumn is ReportDataColumn) && (rColumn != rcSearchTag))
{
((ReportDataColumn)rColumn).DefaultCellStyle.Border = customBorder;
}
}
#endregion
return toRet;
}
......
}
GroupingMicrosoft Reporting Services allows to group data by criteria. In order to set grouping data, it's necessary to override the There is the implementation for public override ReportTableGroupList GetTableGroups(ReportColumnCollection columns)
{
if (!useGrouping)
{
return null;
}
else
{
ReportTableGroupList gImagesGroupList = new ReportTableGroupList();
{
ReportTableGroup reportTableGroupBySearchTag = new ReportTableGroup();
//group by SearchTag
reportTableGroupBySearchTag.ColumnGrouping.Add(columns[0]);
//sort by SearchTag(Descending)
reportTableGroupBySearchTag.ColumnSorting.Add(columns[0],
ReportExporters.Common.Rdlc.Enums.SortOrder.Descending);
gImagesGroupList.Add(reportTableGroupBySearchTag);
}
return gImagesGroupList;
}
}
In this demo, I used the
Custom Export SettingsReport rendering can be customized by using Device Information Settings. I've written classes inherited from PdfDeviceInfoSettings deviceInfo = new PdfDeviceInfoSettings();
//set paper size to A3 (11in × 17in)
deviceInfo.PageHeight = new Unit(11, UnitType.Inch);
deviceInfo.PageWidth = new Unit(17, UnitType.Inch);
string deviceInfoXml = deviceInfo.ToString();
MemoryStream pdfFileData = winFormsReportExporter.ExportToPdf(deviceInfoXml);
Several Worksheets in one Excel WorkbookMuch to my surprise MS Reporting services allows us to export reports in Excel workbook with several worksheets. It can be designed by placing one by one a few In the code, in order to use the ability of adding extra worksheets to a workbook it's necessary to put List<IReportDataAdapter> sameAdapters = new List<IReportDataAdapter>();
for (int aIndex = 0; aIndex < nudSheets.Value; aIndex++)
{
sameAdapters.Add(gImageReportDataAdapter);
}
winFormsReportExporter = new WinFormsReportExporter(sameAdapters);
//Execute method ExportToXls to get Excel WorkBook with several worksheets
MemoryStream xlsFileData = winFormsReportExporter.ExportToXls();
...
//Than xlsFileData can be saved to local disk or etc.
How to Export a DataSetClass DataSetAdapterProvider Methods
DataSet myDataSet;
....
DataSetAdapterProvider dsaProvider = new DataSetAdapterProvider(myDataSet);
//Retreive list of IReportDataAdapters
List<IReportDataAdapter> datasetAdapters = dsaProvider.GetAdapters();
//Pass list of IReportDataAdapters to WinFormsReportExporter constructor
winFormsReportExporter = new WinFormsReportExporter(datasetAdapters);
//Execute method ExportToXls to get Excel file content
MemoryStream xlsFileData = winFormsReportExporter.ExportToXls();
...
//Than xlsFileData can be saved to local disk or etc.
Other FormatsAnton Ponomarev describes in his article, Adding DOC, RTF, and OOXML Export Formats to the Microsoft Report Viewer Control, adding a custom rendering extension to the standard Microsoft Report Viewer control. He modified the ReportViewer assemblies by using .NET tools. As a result, he got the Microsoft.ReportViewer.WinForms.Modified.dll assembly in which the Report Viewer component is able to generate reports in Microsoft Word formats (DOC, RTF, WordprocessingML, and OOXML) when it works in local mode. I suppose if references in ReportExporters to Microsoft.ReportViewer.WinForms.dll is changed to Microsoft.ReportViewer.WinForms.Modified.dll, and the I don't have the Microsoft.ReportViewer.WinForms.Modified.dll yet. I'm going to investigate this problem soon. ConclusionWith the ReportExporters library, you can:
Points of InterestIt was very interesting and exciting for me to write this library. I hope it will be useful for developers who are looking for a similar solution. Any suggestions and questions are welcome! History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||