本文介绍了按表值导出Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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...

推荐答案

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>

我希望这对你有用。


这篇关于按表值导出Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 07:37