Customer of the project, which I am working on, asked us to make additional functionality for our reporting tool: exported Microsoft Report should look different when previewed report.
There are different methods to implement such behaviour. One of them is a Rendering Extension but Microsoft says it is better to change the report instead of implementing Rendering Extension. Partially I checked this solution but was not satisfied. Adding the buttons over Report Viewer control is not the case because UI is already approved by the customer and there is no possibility to make any changes.
Finally found and implemented solution looks like this:
1. implement new rdl report which should look like customer wants to export;
2. add the next element on a page where ReportViewer control is placed
<iframe id="reportDownloader" src="../blank.html" width="10" height="10" style="display:none;"></iframe>
3. the next javascript code helps us to handle client export click
function HandleClientSideExportAlt()
{
var formatDropDown = GetControl(this.m_formatsID);
if (formatDropDown.selectedIndex == 0) return false;
var selectedFormat = formatDropDown.value;
var fullExportUrl = "/AdvancedExport.aspx?exportFormat=" + escape(selectedFormat);
document.getElementById('reportDownloader').src = fullExportUrl;
formatDropDown.selectedIndex = 0;
this.m_exportController.SetViewerLinkActive(false);
return true;
}
RSToolbar.prototype.HandleClientSideExport = HandleClientSideExportAlt;
4. in Page_Load method of AdvancedExport.aspx implement your Server Report export:
String ExportFormat = String.IsNullOrEmpty(Request["exportFormat"]) ? "EXCEL" : Request["exportFormat"].ToUpper();
String exportFileName = String.Format("Report{0}.{1}",
DateTime.Now.ToString("ddMMyyhhmmss"),
ExportFormat.Contains("EXCEL") ? "xls" : "pdf");
using (ReportViewer rv = new ReportViewer())
{
ServerReport serverReport = rv.ServerReport;
serverReport.ReportServerUrl = ...
serverReport.ReportPath = ... <- name of new report from step 1.
serverReport.SetParameters(...);
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
byte[] bytes = serverReport.Render(ExportFormat, null, out mimeType, out encoding, out extension, out streamids, out warnings);
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-length", bytes.Length.ToString());
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", exportFileName));
Response.BinaryWrite(bytes);
}
Excel and PDF formats are just examples and current solution can be used for other exporting formats.
Tuesday, 24 November 2009
Subscribe to:
Post Comments (Atom)


No comments:
Post a Comment