public static void GridViewToExcel(GridView gv, string excelName)
{
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + excelName + ".xls"); //关闭 ViewState
//this.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
//获取control的HTML
gv.RenderControl(hw);
//把HTML写回浏览器
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
tw.Close();
hw.Close();
}
public override void VerifyRenderingInServerForm(Control control)
{
//方法重写
}
protected void btnExcel_Click(object sender, EventArgs e)
{
string[] excelCustName = new string[dtgOdOrderlt.Columns.Count];
for (int i = 0; i < dtgOdOrderlt.Columns.Count; i++)
{
view_ALLodOrder col = new view_ALLodOrder();
if (!string.IsNullOrEmpty(col.CustName.ToString()))
{
string headText = col.CustName.ToString();
string names = dtgOdOrderlt.Columns[i].ToString();
names = headText;
int charIndex = dtgOdOrderlt.Columns[i].ToString().IndexOf('/');
if (charIndex > -1)
{
excelCustName[i] = headText.Substring(0, charIndex);
}
else
{
excelCustName[i] = headText;
}
}
else
{
excelCustName[i] = null;
}
}
GridViewToExcel(dtgOdOrderlt, "OrderLook");
}
报错:如下“/AlbumFoundation”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 878: get
行 879: {
行 880: return base.Getstring(ColumnNames.CustName);
行 881: }
行 882: set
源文件: d:\Projects\AlbumFoundation.root\AlbumFoundation\WebSites\AlbumFoundation\App_Code\Concrete\view_ALLodOrder.cs 行: 880 堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
MyGeneration.dOOdads.BusinessEntity.Getstring(String columnName) +72
Watabe.Shanghai.Albumfoundation.view_ALLodOrder.get_CustName() in d:\Projects\AlbumFoundation.root\AlbumFoundation\WebSites\AlbumFoundation\App_Code\Concrete\view_ALLodOrder.cs:880
orders_ODOrderListtest.btnExcel_Click(Object sender, EventArgs e) in d:\Projects\AlbumFoundation.root\AlbumFoundation\WebSites\AlbumFoundation\orders\ODOrderListtest.aspx.cs:448
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
{
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + excelName + ".xls"); //关闭 ViewState
//this.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
//获取control的HTML
gv.RenderControl(hw);
//把HTML写回浏览器
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
tw.Close();
hw.Close();
}
public override void VerifyRenderingInServerForm(Control control)
{
//方法重写
}
protected void btnExcel_Click(object sender, EventArgs e)
{
string[] excelCustName = new string[dtgOdOrderlt.Columns.Count];
for (int i = 0; i < dtgOdOrderlt.Columns.Count; i++)
{
view_ALLodOrder col = new view_ALLodOrder();
if (!string.IsNullOrEmpty(col.CustName.ToString()))
{
string headText = col.CustName.ToString();
string names = dtgOdOrderlt.Columns[i].ToString();
names = headText;
int charIndex = dtgOdOrderlt.Columns[i].ToString().IndexOf('/');
if (charIndex > -1)
{
excelCustName[i] = headText.Substring(0, charIndex);
}
else
{
excelCustName[i] = headText;
}
}
else
{
excelCustName[i] = null;
}
}
GridViewToExcel(dtgOdOrderlt, "OrderLook");
}
报错:如下“/AlbumFoundation”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 878: get
行 879: {
行 880: return base.Getstring(ColumnNames.CustName);
行 881: }
行 882: set
源文件: d:\Projects\AlbumFoundation.root\AlbumFoundation\WebSites\AlbumFoundation\App_Code\Concrete\view_ALLodOrder.cs 行: 880 堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
MyGeneration.dOOdads.BusinessEntity.Getstring(String columnName) +72
Watabe.Shanghai.Albumfoundation.view_ALLodOrder.get_CustName() in d:\Projects\AlbumFoundation.root\AlbumFoundation\WebSites\AlbumFoundation\App_Code\Concrete\view_ALLodOrder.cs:880
orders_ODOrderListtest.btnExcel_Click(Object sender, EventArgs e) in d:\Projects\AlbumFoundation.root\AlbumFoundation\WebSites\AlbumFoundation\orders\ODOrderListtest.aspx.cs:448
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
return base.Getstring(ColumnNames.CustName);调试下这个ColumnNames对象是否为空,或者ColumnNames.CustName这个属性为空也有可能
设置断点调试一下!!
return base.Getstring(ColumnNames.CustName);
多加个 判断把