Saturday, April 16, 2016

RDLC report: Export data to PDF, Excel and Word

In this article I am going to explain how to export RDLC report to PDF, Excel and Word file programmatically in


Commonly we are using report viewer control to show the data. From Reportviewer we can save or export the displaying records into Excel, PDF and Word document.  In this example I am using 3 buttons to export the data into different formats (PDF, Excel and Word).

I have a table Tb_Movie. I want to export the export the data of this table into Excel file.

Create Store Procedure:
Create proc SpGetMovie
Select * from dbo.Tb_Movie

HTML Markup:
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
         <asp:Button ID="btnpdf" runat="server" Text="PDF" />&nbsp;&nbsp; <asp:Button ID="btnexcel" runat="server" Text="Excel" />&nbsp;&nbsp; <asp:Button ID="btnword" runat="server" Text="Word" />

Create a RDLC report
To create a RDLC report refers to this article: simple steps to Create RDLC Reportin

Add the namespace:
C# Code:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.Reporting.WebForms; Code:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports Microsoft.Reporting.WebForms

Create a method to export data:
Create a method to get the data and export.
C# Code:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());

private void ExportReoprt(string filetype)
            SqlDataAdapter adp = new SqlDataAdapter("SpGetMovie", con);          
            DemoDataSet ds = new DemoDataSet();
            adp.Fill(ds, "SpGetMovie");
            ReportDataSource datasource = new ReportDataSource("DataSet1", ds.Tables[0]);

            Warning[] warnings;
            string[] streams;

            string MIMETYPE = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;

            ReportViewer rptviewer = new ReportViewer();
            rptviewer.ProcessingMode = ProcessingMode.Local;
            rptviewer.LocalReport.ReportPath = "Report.rdlc";
            byte[] bytes = rptviewer.LocalReport.Render(filetype, null, out MIMETYPE, out encoding, out extension, out streams, out warnings);
            Response.Buffer = true;
            Response.ContentType = MIMETYPE;
            Response.AddHeader("content-disposition","attachment; filename=" + DateTime.Now.ToString("ddMMyyyyhhmmss") + "." + extension);
        catch(Exception ex)
    } Code:
Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ToString())
Private Sub ExportReoprtoExecl(filetype As String)
            Dim adp As New SqlDataAdapter("SpGetMovie", con)
            Dim ds As New DemoDataSet()
            adp.Fill(ds, "SpGetMovie")
            Dim datasource As New ReportDataSource("DataSet1", ds.Tables(0))

            Dim warnings As Warning()
            Dim streams As String()
            Dim MIMETYPE As String = String.Empty
            Dim encoding As String = String.Empty
            Dim extension As String = String.Empty

            Dim rptviewer As New ReportViewer()
            rptviewer.ProcessingMode = ProcessingMode.Local
            rptviewer.LocalReport.ReportPath = "Report.rdlc"
            Dim bytes As Byte() = rptviewer.LocalReport.Render(filetype, Nothing, MIMETYPE, encoding, extension, streams, warnings)
            Response.Buffer = True
            Response.ContentType = MIMETYPE
            Response.AddHeader("content-disposition", Convert.ToString("attachment; filename=" + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".") & extension)
        Catch ex As Exception
        End Try
    End Sub

Call the method on button click
Now call the method on button and provide the filetype.

C# code:
protected void btnpdf_Click(object sender, EventArgs e)
    protected void btnexcel_Click(object sender, EventArgs e)
    protected void btnword_Click(object sender, EventArgs e)
    } code:

Protected Sub btnpdf_Click(sender As Object, e As EventArgs) Handles btnpdf.Click
    End Sub

    Protected Sub btnexcel_Click(sender As Object, e As EventArgs) Handles btnexcel.Click
    End Sub

    Protected Sub btnword_Click(sender As Object, e As EventArgs) Handles btnword.Click
    End Sub

1 comment:

  1. Thank you very much fro the codes...
    this is the error am getting
    Error 11 The best overloaded method match for 'Microsoft.Reporting.WebForms.Report.Render(string, string, out string, out string, out string, out string[], out Microsoft.Reporting.WebForms.Warning[])' has some invalid arguments---