由于ActiveX控件只支持IE(好像FF可以通过安装插件支持),所以js导出word的方式就比较局限
可是如果当页面经过js修改以后,.net是无法获取到的,所以要通过js获取到最新的html并传给后台生成文件流以供下载
class="code_img_closed" src="/Upload/Images/2013091716/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('6a89eaf9-0970-4f53-a359-e026c6cddbff',event)" src="/Upload/Images/2013091716/2B1B950FA3DF188F.gif" alt="" />1 $("#expword").click(function () { 2 3 var form = $("<form>"); 4 form.attr('style', 'display:none'); 5 form.attr('target', ''); 6 form.attr('method', 'post'); 7 form.attr('action', 'ExportWord.aspx'); 8 var input1 = $('<input>'); 9 input1.attr('type', 'hidden'); 10 input1.attr('name', 'curDivHtml'); 11 input1.attr('value', $('#divArea').html()); 12 $('body').append(form); 13 form.append(input1); 14 15 form.submit(); 16 form.remove(); 17 });View Code
通过以上方式传输html内容,需要在源页面和"ExportWord.aspx"页面上加上ValidateRequest="false"
获取到html内容后传入下面的方法中,就会通过Response输出word文件了
1 /// <summary> 2 /// 将HTML导出为Word文档 3 /// </summary> 4 /// <param name="html">html代码</param> 5 /// <param name="filename">word文件名</param> 6 public void ExportHTML(string html,string filename) 7 { 8 //Word文件头 9 HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".doc", System.Text.Encoding.UTF8)); 10 HttpContext.Current.Response.ContentType = "application/ms-word"; 11 HttpContext.Current.Response.Charset = "UTF-8"; 12 HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; 13 14 //输出 15 HttpContext.Current.Response.Write(html); 16 HttpContext.Current.Response.End(); 17 18 }View Code