.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 >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

How to export datagridview data to excel file

Posted By: priya     Posted Date: April 02, 2009    Points:2   Category :ASP.Net
Hi Friends,
I need to export datagridview data to excel file how can i do in asp.net using C#.

Thanks
Priya

Responses
Author: Athira Sreejith             
Posted Date: April 03, 2009     Points: 5   

Hi..

public static void ExportToExcel(string Title, GridView gv, HtmlGenericControl Grid, string EmpId, string FromDate, string ToDate, string Month, string Year, string productname, string Division, string workDate)
{
string title = string.Empty;
string totalString = string.Empty;
string attachment = string.Empty;


title = GetExcelTitle(Title, EmpId, FromDate, ToDate, Month, Year, productname, Division, workDate);
if (Title.Trim() == "Attendance Record of the")
{
attachment = "attachment;filename=Attendance.xls";
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter swtext = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hwtext = new HtmlTextWriter(swtext);

gv.GridLines = GridLines.Both;
gv.BorderColor = System.Drawing.Color.Transparent;
gv.BorderWidth = Unit.Pixel(2);
gv.BorderStyle = BorderStyle.Solid;
gv.HorizontalAlign = HorizontalAlign.Center;
gv.ShowHeader = true;

Grid.Attributes.Add("width", "80%");
Grid.RenderControl(hwtext);
totalString = title + swtext.ToString();
HttpContext.Current.Response.Write(totalString.ToString());
HttpContext.Current.Response.End();
}


protected void btnExport_Click(object sender, ImageClickEventArgs e)
{
VetcareExport.ExportToWord("Attendance Record of the", grdAttendance, Grid, VetCareSession.LoginID, "", "", drpMonth.SelectedItem.Text, drpYear.SelectedItem.Text, "", "", "","");
}

Author: Meetu Choudhary             
Posted Date: April 30, 2009     Points: 5   

Try This Code

public void ExportGridView(GridView GridView1, String strFileName)
{
//string attachment = "attachment; filename=Contacts.xls";
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.AddHeader("content-disposition","attachment;filename="+ strFileName);
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Charset = "";
//System.Web.UI.Page.EnableViewState = false;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}

/*Use this commented function in all the pages where the above export function is used
public override void VerifyRenderingInServerForm(Control control)
{
}*/

Author: Nabin             
Posted Date: August 27, 2009     Points: 5   

Hi Priya,
first u create a button in the page where your data grid is present(in the design page)
then write the following code in the button click


protected void btncsv_Click(object sender, EventArgs e)
{
string pagecontent = "<html><body lang=EN-US style='tab-interval:.5in'><table width=100% border=0><tr><Td>Product Name</td><Td>Category</td><Td>SubCategory</td><Td>Brand Name</td><Td>Subbrand Name</td><Td>Price</td></tr>";
SqlCommand cmd = new SqlCommand("select statement", con);
SqlDataReader rdcsv = cmd.ExecuteReader();
while (rdcsv.Read())
{
pagecontent += "<tr><td>" + rdcsv["prodName"].ToString() + "</td><Td>" + rdcsv["catName"].ToString() + "</td><Td>" + rdcsv["subcatName"].ToString() + "</td><Td>" + rdcsv["brdName"].ToString() + "</td><Td>" + rdcsv["subbrdName"].ToString() + "</td><td>" + rdcsv["price"].ToString() + "</td><Td>";
}
rdcsv.Close();
string filename = "";
Random rnd = new Random();
int num = rnd.Next();
filename = "MyFile" + num + ".xls";
pagecontent += "</table></body></html>";
Response.AppendHeader("Content-Type", "application/msexcel");
Response.AppendHeader("Content-disposition", "attachment;filename=" + filename);
Response.Write(pagecontent);
}


try this this works

Thanks
Nabin

Author: Rakesh             
Posted Date: May 10, 2011     Points: 20   

Hi,

Please follow the below link

http://www.dotnetcodes.com/dotnetcodes/code/Source-Codes-36-Export--DataGrid-to-Excel-in-C.aspx

there you can learn how to export datagrid data to excel.

Rakesh



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