Home > ASP.Net > Export ReportViwer to Excel,PDF,and Image from Code

Export ReportViwer to Excel,PDF,and Image from Code

Hi all,

try this example to export MS ReportViwer to Excel and PDF and Image from Code

In this example i will use Categories table in Northwind Database

1) Open VS2008 and create a new website

2) Right click on Solution Explorer and Add new DataSet and name it “NorthwindDS.xsd” and from Server Explorer drag Categories table into Dataset

3) Right click on Solution Explorer and Add new it then select Report and rename it to “CategoryReport.rdlc” and you can design your report which data to display in your report

4) Add new Web Page and from Toolbox drag and Drop ReportViwer and three buttons for export (Excel,Pdf,Image)

5) Now we need to display data from Categories table and display it on Reportviewer so add this code

private void FillReport()
 NorthwindDS _ds = new NorthwindDS();
 NorthwindDSTableAdapters.ProductsTableAdapter _ProductsTableAdapter = new NorthwindDSTableAdapters.ProductsTableAdapter();
 Microsoft.Reporting.WebForms.ReportDataSource rds;
 rds = new Microsoft.Reporting.WebForms.ReportDataSource("NorthwindDS_Categories", _ds.Categories);
 ReportViewer1.LocalReport.ReportPath = "Report/ProductReport.rdlc";


so will add this method in Page Load

protected void Page_Load(object sender, EventArgs e)
 if (!IsPostBack)

Now we need to create method for Report Export to Excel,Pdf,Image

public enum ReportType

private void ExportReport(string fileName,string reporttype)
 // Variables
 Warning[] warnings;
 string[] streamIds;
 string mimeType = string.Empty;
 string encoding = string.Empty;
 string extension = string.Empty;
 byte[] bytes = ReportViewer1.LocalReport.Render(reporttype, null, out mimeType, out encoding, out extension, out streamIds, out warnings);
 // Now that you have all the bytes representing the PDF report, buffer it and send it to the client.
 Response.Buffer = true;
 Response.ContentType = mimeType;
 Response.AddHeader("content-disposition", "attachment; filename=" + fileName + "." + extension);
 Response.BinaryWrite(bytes); // create the file
 Response.Flush(); // send it to the client to download

In Button Click we can call this method

protected void btn_Pdf_Click(object sender, EventArgs e)
 ExportReport("PDF", ReportType.PDF.ToString());
 protected void btn_Excel_Click(object sender, EventArgs e)
 ExportReport("Excel", ReportType.Excel.ToString());
 protected void btn_Image_Click(object sender, EventArgs e)
 ExportReport("Image", ReportType.Image.ToString());

Now Browse your website

Hope this helps

Good Luck


Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: