Saturday, April 16, 2016

Export RDLC report to PDF programmatically in

In this article I am going to explain how we can export RDLC report data to PDF 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 going to explain to export the data into PDF without showing in reportviewer.

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

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

Export data to Excel file:
Create a method to get the data and export into excel file. And call it on page load.

C# Code:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    private void ExportReoprtoExecl()
            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("pdf", 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())
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
        End If
    End Sub
    Private Sub ExportReoprtoExecl()
            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("pdf", 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

No comments:

Post a Comment