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 asp.net.


Description:

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).

Implementation:
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
AS
BEGIN
Select * from dbo.Tb_Movie
End

HTML Markup:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         <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" />
    </div>
    </form>
</body>
</html>


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

Add the namespace:
C# Code:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.Reporting.WebForms;
VB.net 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)
    {
        try
        {
            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";
            rptviewer.LocalReport.DataSources.Add(datasource);
            byte[] bytes = rptviewer.LocalReport.Render(filetype, null, out MIMETYPE, out encoding, out extension, out streams, out warnings);
            Response.Buffer = true;
            Response.Clear();
            Response.ContentType = MIMETYPE;
            Response.AddHeader("content-disposition","attachment; filename=" + DateTime.Now.ToString("ddMMyyyyhhmmss") + "." + extension);
            Response.BinaryWrite(bytes);
            Response.Flush();            
        }
        catch(Exception ex)
        {
        }
    }

VB.net Code:
Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ToString())
   
Private Sub ExportReoprtoExecl(filetype As String)
        Try
            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"
            rptviewer.LocalReport.DataSources.Add(datasource)
            Dim bytes As Byte() = rptviewer.LocalReport.Render(filetype, Nothing, MIMETYPE, encoding, extension, streams, warnings)
            Response.Buffer = True
            Response.Clear()
            Response.ContentType = MIMETYPE
            Response.AddHeader("content-disposition", Convert.ToString("attachment; filename=" + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".") & extension)
            Response.BinaryWrite(bytes)
            Response.Flush()
        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)
    {
        ExportReoprt("pdf");
    }
    protected void btnexcel_Click(object sender, EventArgs e)
    {
        ExportReoprt("Excel");
    }
    protected void btnword_Click(object sender, EventArgs e)
    {
        ExportReoprt("Word");
    }

VB.net code:

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

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

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




Post a Comment

[blogger]

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget