小弟想实现将gridview中的数据导出Excel, 经过上网搜索 拼凑出了代码 经过测试 还是存在问题 小弟不知所措 求解。代码:Response.Clear();
Response.Buffer = true;
Response.Charset = "UTF-8";
Response.AppendHeader("Content-Disposition", "attachment;filename=HenuSoftData.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false; System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); try
{
GridView1.AllowPaging = false;
GridView1.Columns[0].Visible = false;
GridView1.RenderControl(oHtmlTextWriter);
GridView1.AllowPaging = true;
}
catch (System.Exception ex)
{
Alert(ex.Message, this.Page);
return;
}
Response.Write(oStringWriter.ToString());
Response.Flush();
Response.End(); public override void VerifyRenderingInServerForm(Control control)
{ }
运行以上代码出现的问题是 :1. Excel中出现了gridview以外的东西 (如两个按钮)
针对这个问题我 修改了下<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="YGList.aspx.cs" Inherits="Admin_BasicInfo_YGList" %>
再运行 1这个问题解决了 但是又出现乱码问题 我又经过反复的改动测试还没好 请教各位 静等赐教
Response.Buffer = true;
Response.Charset = "UTF-8";
Response.AppendHeader("Content-Disposition", "attachment;filename=HenuSoftData.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false; System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); try
{
GridView1.AllowPaging = false;
GridView1.Columns[0].Visible = false;
GridView1.RenderControl(oHtmlTextWriter);
GridView1.AllowPaging = true;
}
catch (System.Exception ex)
{
Alert(ex.Message, this.Page);
return;
}
Response.Write(oStringWriter.ToString());
Response.Flush();
Response.End(); public override void VerifyRenderingInServerForm(Control control)
{ }
运行以上代码出现的问题是 :1. Excel中出现了gridview以外的东西 (如两个按钮)
针对这个问题我 修改了下<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="YGList.aspx.cs" Inherits="Admin_BasicInfo_YGList" %>
再运行 1这个问题解决了 但是又出现乱码问题 我又经过反复的改动测试还没好 请教各位 静等赐教
http://dotnet.aspx.cc/file/Export-Gridview-To-Excel-With-Multi-Sheet.aspx