/// <summary> /// 导出html到doc文件 /// </summary> /// <param name="ctl"></param> private void HtmlToWord(System.Web.UI.Control ctl) { HttpResponse response = HttpContext.Current.Response; response.Clear(); response.ClearContent(); response.ClearHeaders(); StringBuilder metaHtml = new StringBuilder(); //加入htm 标签,防止doc标示不到htm代码 metaHtml.AppendFormat("<head><meta http-equiv='Content-Type';content='text/html';charset='{0}' /> </head>", System.Text.Encoding.Default.WebName); response.Write(metaHtml); //另存为文件名 string fileName = System.Web.HttpUtility.UrlEncode("公文.doc", System.Text.Encoding.UTF8); response.AddHeader("content-disposition", String.Format("attachment;filename={0}", fileName)); response.ContentType = "application/ms-word"; StringWriter stringWriter = new StringWriter(); HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter); //ctl为服务器的一个控件 可以是DIV,Table ctl.RenderControl(htmlWrite); string content = ReplaceHref(stringWriter.ToString()); //写入 response.Write(content); response.End(); }