I’ve been getting a lot of requests lately for help with exporting a report to a file in SQL Server Reporting Service (SSRS). The focus of this tutorial will be to show you guys how you can export a report from your Report Server.
Please note, this tutorial will not show you how to export an RDLC (client-side) report.
The first thing we need to do is setup up our Visual Studio .NET project:
- Download ReportHelper zip and extract the wrapper C# class ReportHelper.cs into your project.
- You will need to add a service reference to your Report Server execution web service (e.g. http://YourServerName/ReportServer/ReportExecution2005.asmx).
- Alternatively, you can use the VS.NET WSDL tool to extract a C# proxy class and add that to your project (I’ll provide instructions on how to do this further down).
Once you’re all setup, create a new VS.NET MVC or Web Forms project.
MVC: Create the following action method in your HomeController:
public FileContentResult ExportReport()
{
var @params = new List<ReportingServices.ParameterValue>();
@params.Add(new ReportingServices.ParameterValue() { Name = “ParameterName”, Value = “YourValue”});
var report = new NunoSolutions.Report();
report.ReportServerUrl = “http://YourServer/ReportServer”;
report.ReportPath = “/Home/Report1”; // This is the report server path to your report
report.ExportParameters = @params.ToArray(); // can be excluded if your report doesn’t have parameters
report.ReportTitle = “Your Report Name”;
report.ReportFormat = “Excel”;
return NunoSolutions.ReportHelper.ExportMVC(report);
}
WebForms: Create new button called “Button1” and connect the “Click” event to the following method:
protected void Button1_Click(object sender, EventArgs e)
{
var @params = new List<ReportingServices.ParameterValue>();
@params.Add(new ReportingServices.ParameterValue() { Name = “ParameterName”, Value = “YourValue”});
var report = new NunoSolutions.Report();
report.ReportServerUrl = “http://YourServer/ReportServer”;
report.ReportPath = “/Home/Report1”; // This is the report server path to your report
report.ExportParameters = @params.ToArray(); // can be excluded if your report doesn’t have parameters
report.ReportTitle = “Your Report Name”;
report.ReportFormat = “Excel”;
NunoSolutions.ReportHelper.Export(report);
}
You’re done, go ahead and run your project. If you configured everything correctly, your report should get exported to Excel format.
Here’s how you can generate the SSRS proxy class:
- Open VS.NET Command Prompt
- Enter CD C:\Temp and press enter (this will be the location where the proxy file will be created)
- Execute the following command to generate the SSRS proxy class (make sure you change the report server name to your server’s name): wsdl /out:ReportExecutionService.cs http://YourReportServerName/ReportServer/ReportExecution2005.asmx?WSDL
- In Visual Studio, add C:\Temp\ReportExecutionService.cs file to your project
Click Here for more information on how to export a C# Proxy class using Microsoft’s WSDL utility.