Click here to Skip to main content
16,004,969 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my web application i want to download data directly from a SQL database to MS Excel with customize heading. In the web page there will be one textbox, user only choose the date from that textbox(this contain the pop up calender) and when user clicks on download button, it will directly download data into Excel. I don't want to bind huge data into GridView, so i want to download data directly into Excel using ASP.NET and C#.
How can i achieve this?
Posted
Updated 8-Jan-12 20:05pm
v2

You can easily do it from gridview to excel by Export To Excel control with same format.

otherwise put you data in datatable and export it to excel as in example
http://www.dotnetboss.com/2010/05/13/how-to-datatable-export-to-excel-in-asp-net/
 
Share this answer
 
 
Share this answer
 
Comments
Rajendra Patel Jat 5-Apr-12 2:52am    
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=6DEGREESIT37;Initial Catalog=lpc4;Integrated Security=True");
string ss = "select * from blog";
SqlDataAdapter da = new SqlDataAdapter(ss,con);
DataTable dt = new DataTable();
da.Fill(dt);
string attachment = "attachment; filename=Employee.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
string tab = "";
foreach (DataColumn dc in dt.Columns)
{
Response.Write(tab + dc.ColumnName);
tab = "\t";
}
Response.Write("\n");

int i;
foreach (DataRow dr in dt.Rows)
{
tab = "";
for (i = 0; i < dt.Columns.Count; i++)
{
Response.Write(tab + dr[i].ToString());
tab = "\t";
}
Response.Write("\n");
}
Response.End();

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900