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这个第三方的库。

    评论

报告相同问题?

悬赏问题

  • ¥15 镍氢电池充电器设计实物使用原理
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号