用这样的
public void ToExcel(System.Web.UI.Control ctl)
{ HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
可以直接把gridview显示出来的当前页下载,
如果有分页的,不是当前页的没在下载的内容中。
要怎么处理才能把所有内容下载
public void ToExcel(System.Web.UI.Control ctl)
{ HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
可以直接把gridview显示出来的当前页下载,
如果有分页的,不是当前页的没在下载的内容中。
要怎么处理才能把所有内容下载
不過數據太多的話,就要考慮頁面的性能了。
e-mail:[email protected]
2楼的大哥能不能给我发一份??
exc.ToExcel(GridView1);
GridView1.AllowPaging = true;//恢复GridView控件分页 databind();//数据绑定函数这样可以下载全部的了,但是
public void ToExcel(System.Web.UI.Control ctl)
{ HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.Charset = "GB2312"; HttpContext.Current.Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
改成这样,在韩文系统中下载后,中文还是变成乱码