---
title: "ASP.Net 2.0: Export GridView to Excel"  
description: "The focus of the article is the Export to Excel functionality - the Gridview and its data binding are only for demonstrating the Export functionality."  
author: "AVADHESH PATEL"  
published: 2012-08-31  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/349/asp-dot-net-2-0-export-gridview-to-excel  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# ASP.Net 2.0: Export GridView to Excel

The focus of the [article](https://www.mindstick.com/articles/95867/are-you-looking-for-500-credit-score-mortgage-lenders-houston-continue-reading-this-article) is the [Export to Excel](https://www.mindstick.com/forum/313/how-will-data-export-to-excel-in-datagridview) [functionality](https://www.mindstick.com/blog/136/using-watermark-functionality-in-textbox-by-jquery) - the [Gridview](https://www.mindstick.com/articles/873/update-delete-edit-gridview-in-asp-dot-net) and its [data binding](https://www.mindstick.com/articles/337466/exploring-data-binding-in-mvc-how-it-connects-model-and-view) are only for demonstrating the Export functionality.\

For exporting GridView to excel, take a gridveiw in [asp.net](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) page and bind gridview to a [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) for display records. Take a button and write bellow code on button’s click.

When you [execute](https://www.mindstick.com/interview/49/how-can-i-execute-a-php-script-using-command-line) the webpage, gridview and button will be display. When you click the button “Export.xml” file downloaded on your system. This file have the data which display in gridview.

```
            //Clears all content output from the buffer stream            Response.ClearContent();            //to pop a 'open/save file' dialog for the users, so that they can download an file on to their local machines            Response.AddHeader("content-disposition", "attachment; filename=Export.xls");            //A string describing the content type. This string is usually formatted as type/subtype,             //where type is the general content category, and subtype is the specific content type.            Response.ContentType = "application/ms-excel";            //StringWriter is used with HtmlTextWriter.            StringWriter sw = new StringWriter();            //HtmlTextWriter writes HTML programmatically. It allows you to generate a list of HTML elements, such as <div> elements.             HtmlTextWriter htw = new HtmlTextWriter(sw);            //The HtmlForm control is a container for server controls on a Web Forms page.            HtmlForm frm = new HtmlForm();            GridView1.Parent.Controls.Add(frm);            frm.Attributes["runat"] = "server";            frm.Controls.Add(GridView1);            frm.RenderControl(htw);            Response.Write(sw.ToString());            Response.End();
```

---

Original Source: https://www.mindstick.com/blog/349/asp-dot-net-2-0-export-gridview-to-excel

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
