Article posted by Sksamantaray
on 3/27/2012 | Views: 5461 | Category: ASP.NET
| Level: Beginner | Points: 250
In this article we shall learn how to transforming the report output into MS Excel.
Download Download source code for Transforming a SSRS(RDLC) Report report output to Excel format
IntroductionSql Server Reporting service has got a wonderful option called export which in fact helps to export the report output to PDF or Excel. However unfortunately it does not allow user to generate any specific type of output like PDF or excel. So in this article we are going to see how to create only Excel file from the report output.
ObjectiveTo transform a RDLC report output to an Excel file
1.Create a SSRS RDLC Report
2.Create Function Which can convert Report output to Excel file.
Using the codeIn this code we are going to create a report to show top 20 employee details.After the report is created, it needs to be converted to Excel file manually. So in the under mentioned code the export control is removed ,intentionally ,by using ShowExportControls=false.
The getData() method of the code behind populates employee details in the report-viewer.
When one clicks on Export2Excel button the output of Report-Viewer gets converted to Excel file and the user is asked to save the file to physical location or directly open it .
<form id="form1" runat="server">
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" ShowExportControls="false"
Font-Size="8pt" Height="400px" Width="100%">
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
// Code Behind
public partial class _Default : System.Web.UI.Page
string connn = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString();
protected void Page_Load(object sender, EventArgs e)
protected void getData()
SqlDataAdapter da = new SqlDataAdapter("select top 20 * from Employees", connn);
DataSet ds = new DataSet();
ReportDataSource rdc = new ReportDataSource("DataSet1_Employees", ds.Tables);
ReportViewer1.Visible = true;
} //To generate Excel file protected void Button2_Click(object sender, EventArgs e)
Microsoft.Reporting.WebForms.Warning warnings = null;
string streamids = null;
String mimeType = null;
String encoding = null;
String extension = null;
Byte bytes = null;
bytes = ReportViewer1.LocalReport.Render("Excel", null, out mimeType, out encoding, out extension, out streamids, out warnings);
Response.Buffer = true;
Response.AddHeader("Content-Disposition", "attachment; filename=abc.xls");
When user first loads the page ,the getData() method is called,which takes care of loading employee details in the report-viewer. Here you cannot see any export option since it is made to false as i mention earlier in this article. To create an Excel file , user needs to click on Export2Excel button.
When user clicks on Export2Excel ,the following screen asks to open or save the file to physical location.
In this way an Excel file can be created manually from SSRS- RDLC Report output.
If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.
Found interesting? Add this to: