Click here to Skip to main content
15,500,921 members
Articles / Web Development / ASP.NET
Posted 12 Nov 2012


26 bookmarked

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
In this article we will know how to export data to a WORD doc or PDF using Microsoft Report(RDLC) without using Report Viewer.


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. 

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.

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.

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();
    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!!!


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

QuestionTHANKS Pin
Member 103867503-Jun-15 21:34
MemberMember 103867503-Jun-15 21:34 
Questioncreating docx files Pin
AncillaJoan27-Feb-13 4:28
MemberAncillaJoan27-Feb-13 4:28 
AnswerRe: creating docx files Pin
satyaki mishra27-Feb-13 23:36
Membersatyaki mishra27-Feb-13 23:36 
GeneralGood Pin
mikegerard9920-Feb-13 14:02
Membermikegerard9920-Feb-13 14:02 
BugNot properly explained.. Pin
Lalit_D14-Nov-12 2:30
MemberLalit_D14-Nov-12 2: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.