gridview的数据导出到在asp.net中脱颖而出 [英] gridview data export to excel in asp.net
本文介绍了gridview的数据导出到在asp.net中脱颖而出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图转移网格视图数据到excel ....但输出是一个空白Excel sheet.How来解决这个问题呢?有没有什么code转移网格视图值创先争优的数据基片?
保护无效btnexcel_Click1(对象发件人,EventArgs的发送)
{
Response.Clear();
将Response.Buffer =真;
Response.AddHeader(内容处置,附件;文件名= GridViewExport.xls);
Response.Charset的=;
Response.ContentType =应用程序/ vnd.ms-EXCEL; StringWriter的SW =新的StringWriter();
HtmlTextWriter的HW =新的HtmlTextWriter(SW); gvdetails.AllowPaging = FALSE;
gvdetails.DataBind();
gvdetails.HeaderRow.Style.Add(背景色,#FFFFFF);
gvdetails.HeaderRow.Cells [0] .Style.Add(背景色,绿色);
gvdetails.HeaderRow.Cells [1] .Style.Add(背景色,绿色);
gvdetails.HeaderRow.Cells [2] .Style.Add(背景色,绿色);
的for(int i = 0; I< gvdetails.Rows.Count;我++)
{
GridViewRow行= gvdetails.Rows [I]
row.BackColor = System.Drawing.Color.White;
row.Attributes.Add(类,文本模式);
如果(ⅰ%2!= 0)
{
row.Cells [0] .Style.Add(背景色,#C2D69B);
row.Cells [1] .Style.Add(背景色,#C2D69B);
row.Cells [2] .Style.Add(背景色,#C2D69B);
}
} 串风格= @<风格> .textmode {MSO的数字格式:\\ @;}< /风格与GT;
的Response.Write(样式); Response.Output.Write(sw.ToString());
到Response.End();
}
解决方案
您表是空的,因为在空你的字符串作家。
以下是可以帮助
System.Web.UI.HtmlTextWriter htmlWrite =
新的HtmlTextWriter(stringWrite); GridView1.RenderControl(htmlWrite);
下面是完整的code
保护无效的button1_Click(对象发件人,EventArgs的发送)
{
Response.Clear(); Response.AddHeader(内容处置,附件;
文件名= FileName.xls);
Response.ContentType =应用程序/ vnd.xls System.IO.StringWriter stringWrite =新System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite =
新的HtmlTextWriter(stringWrite); GridView1.RenderControl(htmlWrite); 的Response.Write(stringWrite.ToString()); 到Response.End();}
i tried to transfer grid view data to excel .... But the output is a blank excel sheet.How to solve this problem?Is there any code to transfer grid view value to excel sheet with data base?
protected void btnexcel_Click1(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
gvdetails.DataBind();
gvdetails.HeaderRow.Style.Add("background-color", "#FFFFFF");
gvdetails.HeaderRow.Cells[0].Style.Add("background-color", "green");
gvdetails.HeaderRow.Cells[1].Style.Add("background-color", "green");
gvdetails.HeaderRow.Cells[2].Style.Add("background-color", "green");
for (int i = 0; i < gvdetails.Rows.Count;i++ )
{
GridViewRow row = gvdetails.Rows[i];
row.BackColor = System.Drawing.Color.White;
row.Attributes.Add("class", "textmode");
if (i % 2 != 0)
{
row.Cells[0].Style.Add("background-color", "#C2D69B");
row.Cells[1].Style.Add("background-color", "#C2D69B");
row.Cells[2].Style.Add("background-color", "#C2D69B");
}
}
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.End();
}
解决方案
Your sheet is blank because your string writer in null. Here is what may help
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Here is the full code
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;
filename=FileName.xls");
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
这篇关于gridview的数据导出到在asp.net中脱颖而出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文