Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Freinds,
            In my page I assign some values into labels. Now I want to Export those values into excel. All the values are inside the 
<div id="all">
<table></table></div> format.

Note: I need to export those values into Excel by using ASP.Net c#

Please let me know...
Posted
Updated 21-Mar-13 19:13pm
v2

1 solution

Hi There,

C#
public void import_Click(object sender, EventArgs e)
{

//Adding the Header and other attributes to the excel
Myliteral.Text = "";
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=DataTable.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);

 
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("NAME");
DataColumn dc2 = new DataColumn("ADDRESS");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
DataRow dr1 = dt.NewRow();
 

GridView GRid1 = new GridView();//Just add this line here
 

 
this.GRid1.Rows.Add("1", yourtext);
this.GRid1.Rows.Add("2", yourtext2);
GRid1. DataSource() = dt;
GRid1.BindData();

for (int i = 0; i < GRid1.Rows.Count; i++)
{

//Apply text style to each Row
GRid1.Rows[i].Attributes.Add("class", "textmode");

}

GRid1.RenderControl(hw);

 
//style to format numbers to string

string style = @"<style> .textmode { mso-number-format:\@; } style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}

public override void VerifyRenderingInServerForm(Control control)
{
return;
}</style>
i hope this will work for you .
 
Share this answer
 
v3
Comments
FspFriends 22-Mar-13 2:12am    
Thanks daman!

But i'm not using GridView control here! i'm using table ans lables only. So how can i use this?

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