In this article I will explain with an example, how to export RDLC Report and download in Word, Excel, PDF and Image file formats in ASP.Net MVC Razor.
Entity Framework will be used to populate RDLC Reports from SQL Server database in ASP.Net MVC 5 Razor.
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
Creating an Entity Data Model
Following is the Entity Data Model of the Customers Table of the Northwind Database which will be used later in this project.
Controller
The Entity Framework is now configured and hence now we can create a Controller and write code to fetch the records from the Customers Table of the Northwind Database.
Inside the Index Action method, the Top 10 Customer records are fetched and returned to the View.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
NorthwindEntities entities = new NorthwindEntities();
return View(from customer in entities.Customers.Take(10)
select customer);
}
}
Configuring RDLC Report in ASP.Net MVC
HTML Markup
The HTML Markup consists of a RDLC ReportViewer control, a RadioButtonList with the Export file formats and a Button to export RDLC Reports to Word, Excel, PDF and Image file formats.
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="500" SizeToReportContent = "true">
</rsweb:ReportViewer>
<br />
Format:
<asp:RadioButtonList ID="rbFormat" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Text="Word" Value="WORD" Selected="True" />
<asp:ListItem Text="Excel" Value="EXCEL" />
<asp:ListItem Text="PDF" Value="PDF" />
<asp:ListItem Text="Image" Value="IMAGE" />
</asp:RadioButtonList>
<br />
<asp:Button ID="btnExport" Text="Export" runat="server" OnClick="Export" />
</form>
</body>
</html>
Namespaces
You will need to import the following namespace.
using Microsoft.Reporting.WebForms;
Populating the RDLC Report using Entity Framework
Inside the Page Load event, the RDLC Report is populated with the records of the database using Entity Framework.
public partial class Report : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("Report.rdlc");
NorthwindEntities entities = new NorthwindEntities();
ReportDataSource datasource = new ReportDataSource("Customers", (from customer in entities.Customers.Take(10)
select customer));
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(datasource);
}
}
Exporting RDLC Report and download Word, Excel, PDF and Image file formats in ASP.Net MVC
When the Export Button is clicked, the selected value of the RadioButtonList is passed to the Render method of RDLC ReportViewer.
The Render method returns the exported document in Byte Array format.
Finally, the Byte Array is written to Response Stream and downloaded as Attachment in Word, Excel, PDF or Image files.
protected void Export(object sender, EventArgs e)
{
Warning[] warnings;
string[] streamIds;
string contentType;
string encoding;
string extension;
//Export the RDLC Report to Byte Array.
byte[] bytes = ReportViewer1.LocalReport.Render(rbFormat.SelectedItem.Value, null, out contentType, out encoding, out extension, out streamIds, out warnings);
//Download the RDLC Report in Word, Excel, PDF and Image formats.
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=RDLC." + extension);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
Screenshots
The RDLC Report
RDLC Report exported to Word Document
RDLC Report exported to Excel Spreadsheet
RDLC Report exported to PDF file
RDLC Report exported to Image file
Downloads