Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
12 Nov 2012 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here