标签归档:导出

asp.net中将GridView中的部分内容或者全部内容导出到Excel

1.以下是主要的导出为Excel格式的代码:

Response.Clear();
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");//此处为了处理保存名避免乱码
Response.Charset = "gb2312";
Response.ContentType = "application/vnd.xls";
 
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
 
GrantReleaseView.AllowPaging = false;//导出前先取消分页,以便能将所有数据导出。
GrantReleaseInfoBLL.Bind(campusId, depId, year, pageSize, pageIndex, out pageCount, GrantReleaseView);//再重新绑定一次数据
GrantReleaseView.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();

继续阅读