Skip to content

How to render ReportViewer rdlc report as PDF

January 5, 2012

Today, I found some really useful code that can render a really boring ReportViewer rdlc file in a report as  a PDF file with not many lines of code, here’s how: –

private void RenderReport()
{

LocalReport localReport = new LocalReport();
localReport.ReportPath = Server.MapPath(“~/Report.rdlc”);

// Get the data from the database – define your own helper here.

IList<StatsCollection> statsCollection = new List<StatsCollection>();

// Define your own modified custom helper class here that gets that data.

statsCollection = SqlHelper.GetStats();

// Give the collection a name (statsCollection) so that we can reference it in our report designer

DataTable statsData = new DataTable();

DataSet statsDataSet= new DataSet(“Sessions”);
statsData = statsDataSet.Tables.Add(“Sessions”);

statsData .Columns.Add(“Data”, typeof(string));
statsData .Columns.Add(“Session”, typeof(string));
statsData .Columns.Add(“StartDateTime”, typeof(DateTime));
statsData .Columns.Add(“EndDateTime”, typeof(DateTime));

foreach (StatsCollection sessions in sessionsCollection)
{
DataRow row = sessionData.NewRow();
row[“Data”] = sessions.Data;
row[“Session”] = sessions.Session;
row[“StartDateTime”] = sessions .StartDateTime;
row[“EndDateTime”] = sessions .EndDateTime;

sessionData.Rows.Add(row);
}

ReportDataSource reportDataSource = new ReportDataSource(“SessionsDataSet_session”, sessionData);
localReport.DataSources.Add(reportDataSource);

string reportType = “PDF”;
string mimeType;
string encoding;
string fileNameExtension;

//The DeviceInfo settings should be changed based on the reportType
//http://msdn2.microsoft.com/en-us/library/ms155397.aspx
string deviceInfo =
“<DeviceInfo>” +
” <OutputFormat>PDF</OutputFormat>” +
” <PageWidth>8.5in</PageWidth>” +
” <PageHeight>11in</PageHeight>” +
” <MarginTop>0.5in</MarginTop>” +
” <MarginLeft>1in</MarginLeft>” +
” <MarginRight>1in</MarginRight>” +
” <MarginBottom>0.5in</MarginBottom>” +
“</DeviceInfo>”;

Warning[] warnings;
string[] streams;
byte[] renderedBytes;

//Render the report
renderedBytes = localReport.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);

//Clear the response stream and write the bytes to the outputstream
//Set content-disposition to “attachment” so that user is prompted to take an action
//on the file (open or save)
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader(“content-disposition”, “attachment; filename=SessionsReport.” + fileNameExtension);
Response.BinaryWrite(renderedBytes);
Response.End();

}

You need to link the report to the rdlc file as noted on the first few lines of code, which you then also set the data source with your own SQL Helper class. Having heavily re modified the code to make it more generic, please make sure that I haven’t put any errors in the code. You then can render the format of your local report using the code, string reportType = “PDF”; with the render method to easily produce a PDF in the response stream using the BinaryWrite method. It is as simple as that. I had tied this Render method to a button which you can easily associate the correct format you wish to produce the report in, see below in Page_Load: –

if (Request.QueryString[“format”] != null)
{
string format = Request.QueryString[“format”].ToString();

switch (format)
{
case “pdf”:
RenderReport();
break;
case “excel”:
RenderReportAsExcel();
break;
default:
break;
}
}

 

Please note that in the section where you have to generate your own custom helper class, I have defined my own custom class using NHiberate that populates the IList for using eventually as the data source for the ReportViewer.

Advertisements

From → Uncategorized

Leave a Comment

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

%d bloggers like this: