Go to DotNetFunda.com
 Online : 1083 |  Welcome, Guest!   Login
 
Home > Articles > ASP.NET > Simplest Method Export GridviewData to Excelsheet using ASP.Net

  • Download the OOPS, ASP.NET and ADO.NET Training Videos for FREE, click here.

Submit Article | Articles Home | Search Articles |

Simplest Method Export GridviewData to Excelsheet using ASP.Net

 Posted on: 6/23/2009 12:05:09 AM by Syedshakeer | Views: 1036 | Category: ASP.NET | Level: Intermediate | Print Article
In this Article you can Know how to Export a Gridview Data to Excel sheet using IO strems.

.NET Training Videos!
Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

Bind the Data to the Gridview using SqlDatSource and one Button on the webpage.

How to Create a Excel sheet?

TextWriter
is used to Create a Excel Sheet by creting the StreamWriter class object.StreamWriter
class takes the path the of the excel sheet name as argument.Dont forget to use
the verbatile string (@) at the beginning of the path as follows:


TextWriter sw = new StreamWriter(@"F:\\ExcelData.xls");

 

The Extension of the Excel Sheet is .xls.It will automatically creats the excel
sheet with the name ExcelData in ‘F’Drive.If the Excel sheet is already
existing with the name ‘ExcelData’ it will overrides the Data.

in .asp.cs page


using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

using System.IO;

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

 

//Double click your Button and write the below code to Export Gridview Data to Excel

    protected void Button1_Click(object sender, EventArgs e)

    {

    TextWriter sw = new StreamWriter(@"F:\\ExcelData.xls");

    //Getting the Gridview HeaderRow Values

sw.WriteLine(GridView1.HeaderRow.Cells[0].Text+"\t"+GridView1.HeaderRow.Cells[1].Text+"\t"

        +GridView1.HeaderRow.Cells[2].Text);

 

        for (int i = 0; i < GridView1.Rows.Count; i++)

        {

            //Getting the Gridview Row values

            sw.WriteLine(GridView1.Rows[i].Cells[0].Text  + "\t " + GridView1.Rows[i].Cells[1].Text  + "\t" + GridView1.Rows[i].Cells[2].Text );

        }

        sw.Close();//Don’t Forget to Close the StreamWrtier object.

    }

}


output of Excel sheet with data.



The Following Error occurs when you try to export a Data to Excel when the Excel Sheet is in open State.

Error Iamge.





If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


Experience:1 year(s)
Home page:http://www.dotnetfunda.com
Member since:Thursday, February 05, 2009
Level:Bronze
Status: [Member]
Biography:Hi to All ..
I am Working as Software Developer on .NET and MVM of www.dotnetspider.com
 Latest post(s) from Syedshakeer

   ◘ How to get the value of a Hidding column in a Gridview using C# posted on 9/8/2009 6:05:36 PM
   ◘ How to calculate total at the BackEnd using Trigger? posted on 8/25/2009 6:14:14 PM
   ◘ How to display records as First-Next-Previous-Last in a Textboxes using Windows Application? posted on 8/25/2009 6:01:05 PM
   ◘ How to Start Mobile Application on Windows? posted on 8/18/2009 1:00:01 AM
   ◘ Paging for First, Next, Previous and Last in gridview posted on 8/16/2009 12:47:43 PM


Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)