Click here to Skip to main content
Click here to Skip to main content

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

, 12 Nov 2012
Rate this:
Please Sign up or sign in to vote.
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. 

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

About the Author

satyaki mishra
Web Developer
India India
No Biography provided

Comments and Discussions

 
Questioncreating docx files PinmemberAncillaJoan27-Feb-13 3:28 
AnswerRe: creating docx files Pinmembersatyaki mishra27-Feb-13 22:36 
GeneralGood Pinmembermikegerard9920-Feb-13 13:02 
BugNot properly explained.. PinmemberLalit_D14-Nov-12 1:30 
First of all we have to take the Web Application not the web site and then in report part how to add the "datasource" if the repository class is not automatically populated..?
Lalit_D

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 12 Nov 2012
Article Copyright 2012 by satyaki mishra
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid