asp.net 导出word文档问题

图片说明pload/201711/21/1511259339_992470.png)
前台:

后台: 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("<html");
   int endIndex = pageHtml.LastIndexOf("</div>");
   int lenth = endIndex - startIndex;
   pageHtml = pageHtml.Substring(startIndex, lenth);
   // pageHtml = pageHtml.Remove(pageHtml.LastIndexOf("<div class=\"flowbar\">"));

   HttpContext.Current.Response.Write(pageHtml.ToString());
   HttpContext.Current.Response.Flush();
   HttpContext.Current.Response.End();

        问题:1 为什么会出现自动换行; 2 怎么会出现窗体?

你这个导出的不是word而是html,只是因为word也支持打开html文档的缘故,好像是word文档。
要导出真正的word文档,需要用npoi这个第三方的库。