想把datagrid导出excel的代码做成一个类来调用,可不成功,请大家帮忙看看.
.aspx.cs代码:
private void Button1_Click(object sender, System.EventArgs e)
{
MyDataGrid.AllowPaging = false;
BindGrid();
DGToExcel.ToExcleFile(MyDataGrid) ;
MyDataGrid.AllowPaging = true;
BindGrid();
}
.cs类代码
public void ToExcleFile(DataGrid MyDataGrid)
{
MyDataGrid.SelectedItemStyle.BackColor = Color.White;
MyDataGrid.AlternatingItemStyle.BackColor = Color.White;
MyDataGrid.ItemStyle.BackColor = Color.White;
MyDataGrid.HeaderStyle.BackColor = Color.White;
MyDataGrid.HeaderStyle.ForeColor = Color.Red;
HttpContext.Current.Response.AppendHeader( "Content-Disposition ", "attachment;filename=Filel123.xls ");
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding( "gb2312 ");
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel ";
HttpContext.Current.Response.Charset = " ";
MyDataGrid.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
MyDataGrid.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
可就是这两句出错,
HttpResponse resp;
resp = Page.Response;
提示:
c:\inetpub\wwwroot\DGToExcel2_Sample\WebDataAccess.cs(43): 非静态的字段、方法或属性“System.Web.UI.Page.Response”要求对象引用
在网上找了找,没能解决,请大家帮忙看看.
------解决方案--------------------单步调试下
------解决方案--------------------HttpResponse resp;
//resp = Page.Response;
resp = HttpContext.Current.Response;