前台页面需要添加属性:EnableEventValidation="false"
导出事件主要代码:
protected void btnImportPage_Click(object sender, EventArgs e)
{
//设置Http的头信息,编码格式
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("行政管理制度.doc", System.Text.Encoding.UTF8));
HttpContext.Current.Response.ContentType = "application/ms-word";
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
//关闭控件的视图状态
Page.EnableViewState = false;
//初始化HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
Page.RenderControl(htmlWriter);
//输出
string pageHtml = writer.ToString();
int startIndex = pageHtml.IndexOf("<div style=\"margin: 0 auto;\" id=\"mainContent\">");
int endIndex = pageHtml.LastIndexOf("</div>");
int lenth = endIndex - startIndex;
pageHtml = pageHtml.Substring(startIndex, lenth);
pageHtml = pageHtml.Remove(pageHtml.LastIndexOf("<div class=\"flowbar\">"));
//设置图片路径
Uri MyUrl = Request.UrlReferrer;
pageHtml = pageHtml.Replace("../Images/", "http://" + MyUrl.Authority + "/Images/").Replace("textarea", "span");
HttpContext.Current.Response.Write(pageHtml.ToString());
HttpContext.Current.Response.End();
}