Load SSRS Report from C# ASP.net and export to Excel

10 08 2012

Good day all.

Lets go straight to the point. Today I will write about Loading and Exporting .rdl Reports that hosted at some Report Server to Excel file using C# ASP.net.

Lets say I have a report myReport.rdl that hosted in http://mySite.com/ReportServer/TestingReport/myReport.rdl

I want to automatically export myReport.rdl to myReport.xls via C# ASP.net scheduler.

Here are what we have to do :

1. Download ReportViewer from this link

2. Install ReportViewer from the intaller that we’ve downloaded

3. Open your ASP.net project

4. Create new class, we can say ReportGenerator.cs

5.  Add these code on the top of the class :

using System.IO;
using Microsoft.Reporting.WebForms;
using System.Web.Security;
using myProject.BackendClass;

6. Add new private variable with type ReportViewer in the class

private ReportViewer reportViewer;

7.  Add new method to the class :

public void Testing()
{
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string filenameExtension;
reportViewer = new ReportViewer();
reportViewer.ProcessingMode = ProcessingMode.Remote;
IReportServerCredentials irsc = new CustomReportCredentials(“username”,”Password”);
reportViewer.ServerReport.ReportServerCredentials = irsc;
reportViewer.ServerReport.ReportServerUrl = new Uri(“http://mySite.com/reportserver”);
reportViewer.ServerReport.ReportPath = “/TestingReport/myReport”;

byte[] bytes = reportViewer.ServerReport.Render(
“Excel”, null, out mimeType, out encoding, out filenameExtension,
out streamids, out warnings);

using (FileStream fs = new FileStream(“D:’\’”+”myReport.xls”, FileMode.Create))
{
fs.Write(bytes, 0, bytes.Length);
}

}

8. There are some “error” in validation at the editor, which correlating to “using myProject.BackendClass;” and “IReportServerCredentials irsc = new CustomReportCredentials(“username”,”Password”);”. To solve it, these what you need to do

9. Create new folder in your project with name “BackendClass”

10. Add new class in folder “BackendClass”. Name it CustomReportCredentials.cs

11. Put these code on the editor :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Reporting.WebForms;
using System.Net;

namespace PDE.BackendClass
{
public class CustomReportCredentials : IReportServerCredentials
{
private string _UserName;
private string _PassWord;
private string _DomainName;

public CustomReportCredentials(string UserName, string PassWord, string DomainName)
{
_UserName = UserName;
_PassWord = PassWord;
_DomainName = DomainName;
}

public CustomReportCredentials(string UserName, string PassWord)
{
_UserName = UserName;
_PassWord = PassWord;
//_DomainName = DomainName;
}

public System.Security.Principal.WindowsIdentity ImpersonationUser
{
get { return null; }
}

public ICredentials NetworkCredentials
{
//get { return new NetworkCredential(_UserName, _PassWord, _DomainName); }
get { return new NetworkCredential(_UserName, _PassWord); }
}

public bool GetFormsCredentials(out Cookie authCookie, out string user,
out string password, out string authority)
{
authCookie = null;
user = password = authority = null;
return false;
}
}
}

12. Now you can run your project, and… Voila……

That’s all folks. Good day :D

About these ads

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.

%d bloggers like this: