Click here to Skip to main content
15,868,127 members
Articles / Web Development / ASP.NET

Exporting to Word/PDF using Microsoft Report (RDLC) without using Report Viewer.

Rate me:
Please Sign up or sign in to vote.
4.71/5 (5 votes)
12 Nov 2012CPOL2 min read 119.4K   5.8K   26   5
In this article we will know how to export data to a WORD doc or PDF using Microsoft Report(RDLC) without using Report Viewer.

Introduction 

Sometimes we have requirement to directly export data in WORD/PDF format without showing the data as a report in the page. In this article we will create a sample payslip in word/pdf file for employees. Each page in the word doc will contain payslip of corresponding employee in the employee list. We will not use Microsoft Report Viewer because we are not showing data in page.

Using the code 

Create a new website in Microsoft Visual Studio 2010. Add a new class to the website and name it as Employee. Add following code to the employee class. 

C#
public class Employee
{
    public string Name { get; set; }
    public float Salary { get; set; }
    public string EmployeeID { get; set; }
    public string Designation { get; set; }
}

Add a new class to the project and name it as “EmployeeRepository” . This will act as datasource for our project. Add following code to the EmployeeRepository class.

C#
public static class EmployeeRepository
{
    public static List<Employee> GetAllEmployees()
    {
        Employee employee1 = new Employee {Name="Satyaki Mishra", 
           EmployeeID="H0090", Designation="Software Engineer", Salary=10000 };
        Employee employee2 = new Employee { Name = "Raman Saha", 
           EmployeeID = "A0090", Designation = "Test Lead", Salary = 15000 };
        Employee employee3 = new Employee { Name = "Purab Dash", 
           EmployeeID = "ZD120", Designation = "Sr.Software Engineer", Salary = 13000 };
        return new List<Employee> { employee1, employee2, employee3 };
    } 
}

Add a new Report to the website which can be found in “Reporting” section in the “add new item” window. Double click in the RDLC in the solution explorer to open the design view. Add a new Dataset to the report by clicking on the “New” menu in “Report Data” section. In the “Dataset Properties” window , select the DataSource from the Datasource dropdown. In the Datasource dropdown, the namespace of the project is already populated. Select the Datasource from the dropdown or create a new datasource by clicking in the “New” button. Select the required dataset in the next dropdown. On selecting the dataset, available fields in the dataset are populated in the right side list. Click on OK to create the dataset.

Now let’s design our report. We will design our report to get a word document in which each page contains a salary slip of an employee from the employee list. Right click on the report area to add a List (Insert>List) to the report. We have used List because we need to get the report for all the employees in the employee list. Drag some text boxes from the Toolbox and place them to the List. Right click on the TextBox and click on Expression. Click on the “DataSets” in the left panel in Category section. Select required value from “Values” section.

Add reference to Microsoft.ReportViewer.WebForms to the project. In the page_load event of the code behind file add following code.

C#
protected void Page_Load(object sender, EventArgs e)
{
    LocalReport report = new LocalReport();
    report.ReportPath = "Report1.rdlc";
    ReportDataSource rds = new ReportDataSource();
    rds.Name = "DataSet1";//This refers to the dataset name in the RDLC file
    rds.Value = EmployeeRepository.GetAllEmployees();
    report.DataSources.Add(rds);
    Byte[] mybytes = report.Render("WORD");
    //Byte[] mybytes = report.Render("PDF"); for exporting to PDF
    using (FileStream fs = File.Create(@"D:\SalSlip.doc"))
    {
        fs.Write(mybytes, 0, mybytes.Length);
    }
}  

On running the application you can find the word document saved in the D: drive.

Happy coding!!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
BugNot properly explained.. Pin
Lalit_D14-Nov-12 1:30
Lalit_D14-Nov-12 1:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.