C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ADO.NET »

Export GridviewData to Excelsheet using ASP.Net


Posted Date: 22 Aug 2009    Resource Type: Code Snippets    Category: ADO.NET
Author: Syed Shakeer Hussain Member Level: Diamond    
Rating: 1 out of 5Points: 7



In this Article you can Know how to Export a Gridview Data to Excel sheet using IO strems.
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.
StreamWriterclass takes the path the of the excel s
heet name as argument.Dont forget to usethe 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 excelsheet with the
name ExcelData in ‘F’Drive.If the Excel sheet is
alreadyexisting 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.

}

}


Now open your F: Drive.You will find an Excel Sheet with a Name ExcelData
Thanks for Reading my article!
Syed Shakeer Hussain



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Export GridviewData to Excelsheet using ASP.Net  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Program for the beginners in ADO.Net.
Previous Resource: How to display records as First-Next-Previous-Last in a Textboxes using Windows Application?
Return to Discussion Resource Index
Post New Resource
Category: ADO.NET


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use