.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> Windows Application >> Post New QuestionBookmark and Share Subscribe to Forum

how to export data grid content into excel sheet

Posted By: mohd     Posted Date: January 11, 2010    Points:2   Category :Windows Application
i have a data grid content for my windows application. i m fetching the data into to the data grid at runtime but i want to export the data into an excel sheet for window application using c#.

Please help me.

Responses
Author: Amit Mehra             
Posted Date: January 11, 2010     Points: 5   

Yes you can pass the data from DataGridview to Excel application worksheet. For code you can refer this articles

http://www.codeproject.com/KB/grid/ExportDatagridviewToExcel.aspx

Thanks
Amit

Thanks,
Amit
Please "Mark As Answer " if this post help you.
Author: Lalij Mer             
Posted Date: January 11, 2010     Points: 5   

Try this code,

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}

private void BindData()
{
string query = "SELECT * FROM Categories";
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
DataSet ds = new DataSet();
ad.Fill(ds, "Categories");
GridView1.DataSource = ds;
GridView1.DataBind();
}

private string ConnectionString
{
get { return @"Server=localhost;Database=NorthWind;Trusted_Connection=true"; }

}
protected void BtnExport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";

// If you want the option to open the Excel file without saving then
// comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}


more reference
-----------
http://aspalliance.com/771_CodeSnip_Exporting_GridView_to_Excel
http://www.codersource.net/published/view/283/exporting_data_grid_to_excel.aspx
http://www.vbdotnetheaven.com/UploadFile/mahesh/ExportDataGridtoExcel01092007134204PM/ExportDataGridtoExcel.aspx

Please Check my answer if you help....
Thank You...

Lalit.
Author: Edward             
Posted Date: March 31, 2011     Points: 20   

http://www.c-sharpcorner.com/UploadFile/d0cbea/6031/Default.aspx
This article shows code to export data from GridView to Excel.
HOPE HELPFUL!



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend