Export to stream

Using the ReportViewer component for Reporting Services a user can save a report to one of the supported formats (pdf, excel, html, etc). But by default, after clicking the Export button, it opens a dialog to save a report or a report is opened in a new tab depending on the availability of the corresponding plug-in in a browser.

But sometimes you may need to get export result as bytes sequence and you don’t need to save a report or do any other actions. This can be done using the following method: ReportViewer.ExportToStream

/// <summary>
/// Exports document to the specified format and calls the callback function with export results
/// </summary>
/// <param name="format">Format to export a document to</param>
/// <param name="callBack">Function to be called on export completion</param>
public void ExportToStream(ExportFormat format,
                           Action<ExportResult> callBack)

Here is the code sample:

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    reportViewer.DebugMode = DebugModeEnum.Full;
    reportViewer.ReportName = "/Product Catalog 2008";
    reportViewer.ServiceUrl = "http://localhost:5555/ReportService.svc";
    reportViewer.ApplyTemplate();
    reportViewer.RenderDocument();
    reportViewer.DocumentLoaded += reportViewer_DocumentLoaded;
}
 
void reportViewer_DocumentLoaded(object sender, DocumentLoadedEventArgs e)
{
    reportViewer.ExportToStream(ExportFormat.Pdf,
      new Action<PerpetuumSoft.ReportingServices.Viewer.Client.ExportResult>
     (ExportToStreamCallback));
}
 
private void ExportToStreamCallback(ExportResult exportResult)
{
    if (exportResult.IsSucceed)
    {
        byte[] data = exportResult.Data;
        //Do something
    }
}


Add Feedback