In this article I will demonstrate you how to create rdlc report in asp.net.
Microsoft made it very simple to generate PDF by using the “Microsoft.ReportViewer.WebForms”
Take the reference of Microsoft.ReportViewer.WebForms to work with RDLC. It will appear in your reference folder like below;
Now create the Dataset which you want to link with RDLC file, I have create the dataset for Employee class which is having Name, Salary, EmployeeID and Designation.
Now How to Create the Dataset?
Click on the Solution Explorer à Add New Items you will get the Below Window; Give the Name to Dataset and click on the Add.
Now you will get the XSD file in your solution folder like it for Dataset;
Open XSD and create the datatable that you desire for like below process;
In our solution I created the Dataset like below;
Now Create RDLC file in application on which above datatable will render and generate the PDF file.
Add RDLC file and Drag the List and bind the Dataset with the List. Now it will appear the Screen like Below.
Now need to work with the code in C# to generate the PDF in this RDLC file and link to it.
Create the class which will give the List of Employee details that we are going to display in RDLC pdf file;
public static List<Employee>
GetAllEmployees()
{
Employee employee1 = new
Employee { EName = "BOB",
EmployeeID = "E001", EDesignation
= "SW", ESalary = 5000 };
Employee employee2 = new
Employee { EName = "Eric",
EmployeeID = "E002", EDesignation
= "Manager", ESalary = 8000 };
Employee employee3 = new
Employee { EName = "Danny",
EmployeeID = "E003", EDesignation
= "SSW", ESalary = 6000 };
return new List<Employee>
{ employee1, employee2, employee3 };
}
protected void Page_Load(object sender, EventArgs
e)
{
Warning[] warnings;
string[]
streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
//enter code here`
string extension = string.Empty;
ReportViewer viewer = new
ReportViewer();
viewer.LocalReport.Refresh();
viewer.LocalReport.ReportPath = "Report1.rdlc";
//This is your rdlc name.
//
viewer.LocalReport.SetParameters(param);
ReportDataSource rds = new
ReportDataSource();
rds.Name = "DataSet1";//This refers to the dataset name in the RDLC file
rds.Value = EmployeeRepository.GetAllEmployees();
viewer.LocalReport.DataSources.Add(rds);
byte[] bytes = viewer.LocalReport.Render("PDF", null,
out mimeType, out
encoding, out extension, out streamIds, out
warnings);
// byte[] bytes =
viewer.LocalReport.Render("Excel", null, out mimeType, out encoding,
out extension, out streamIds, out warnings);
// Now that you have all the bytes representing the PDF
report, buffer it and send it to the client.
//
System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Buffer = true;
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition",
"attachment; filename= filename" +
"." + extension);
Response.OutputStream.Write(bytes, 0, bytes.Length); // create the file
Response.Flush(); // send it to the client to
download
Response.End();
}
So lets see the output. Its plain PDF but you can change the Font, Color, background color as you need.
No comments:
Post a Comment