Add this header name in your cs file:
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
In your export to excel button click event write this code:
SqlConnection con;
con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from tablename", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable ds = new DataTable(); try
{
da.Fill(ds);
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message);
throw ex;
}
finally
{
con.Close();
}
Convert(ds, Response);
}
public static void Convert(DataTable ds, HttpResponse Res)
{
Res.Clear();
Res.Charset = "";
Res.ContentType = "application/vnd.ms-excel";
Res.AddHeader("Content-Disposition", "attachment;filename=\\tablename.xls");
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
System.Web.UI.WebControls.GridView dg = new System.Web.UI.WebControls.GridView();
dg.DataSource = ds;
dg.DataBind();
dg.RenderControl(htmlWrite);
Res.Write(stringWrite.ToString());
Res.End();
}
This Convert method convert our database to Excel format .
When you run this code and click on Export button it download one .xls file in which our table data will be saved.
Happy Coding.....
Thank You.....
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
In your export to excel button click event write this code:
SqlConnection con;
con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from tablename", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable ds = new DataTable(); try
{
da.Fill(ds);
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message);
throw ex;
}
finally
{
con.Close();
}
Convert(ds, Response);
}
public static void Convert(DataTable ds, HttpResponse Res)
{
Res.Clear();
Res.Charset = "";
Res.ContentType = "application/vnd.ms-excel";
Res.AddHeader("Content-Disposition", "attachment;filename=\\tablename.xls");
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
System.Web.UI.WebControls.GridView dg = new System.Web.UI.WebControls.GridView();
dg.DataSource = ds;
dg.DataBind();
dg.RenderControl(htmlWrite);
Res.Write(stringWrite.ToString());
Res.End();
}
This Convert method convert our database to Excel format .
When you run this code and click on Export button it download one .xls file in which our table data will be saved.
Happy Coding.....
Thank You.....
No comments:
Post a Comment