导出到word

         1. //服务器端rendercontrol生成word文档
            #region
            //HttpContext.Current.Response.Clear();
            //HttpContext.Current.Response.Charset = "UTF-8";
            //HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=导出文档.doc");
            //HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            //HttpContext.Current.Response.ContentType = "applicaton/ms-word";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
            //Page.EnableViewState = false;
            //System.IO.StringWriter sw = new System.IO.StringWriter();
            //System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);
            //this.renderexport.RenderControl(hw);
            //HttpContext.Current.Response.Write(sw.ToString());
            //HttpContext.Current.Response.End();

2. 将word文档另存为html页面,然后将html中需要替换的内容改成$$,在aspx页面中将内容替换掉。后通过下面代码输出其中word参数为字符串

Response.ContentEncoding = System.Text.Encoding.Default;
            Response.ClearContent();
            Response.ClearHeaders();
            Response.AddHeader("content-disposition", "attachment;filename="  + "export.doc"); //这就是我失误的地方 attachment 写成了 inline
            Response.ContentType = "vnd.ms-excel/msword ";
            Response.Write(word);
            Response.Flush();
            Response.Close();

3.通过js导出word,注意浏览器中安全内ActiveX要均设为启用,其中value为通过document.getElementById方法获得的对象

function areaword(value) {
    var wd = new ActiveXObject("Word.Application");
    var doc = wd.Documents.Add("", 0, 1);
    var range = doc.Range(0, 1);
    var sel = document.body.createTextRange();
    sel.moveToElementText(value);
    sel.select();
    sel.execCommand("Copy");
    range.Paste();
    wd.Application.Visible = true;
    return false;
}   

原文地址:https://www.cnblogs.com/baozi/p/3200686.html