sunshinewen1987 2017-11-21 10:13 采纳率: 0%
浏览 929

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 怎么会出现窗体?
  • 写回答

1条回答 默认 最新

  • threenewbee 2017-11-21 16:07
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功