首页 > 代码库 > HTML转换成word文档
HTML转换成word文档
1工具类保存word文件
public class WordAction { public static void SaveAsWord(string fileName, string pFileName)//使用原生方法将mht转换为word文档,不是那种直接修改后缀名的方式 { object missing = System.Reflection.Missing.Value; object readOnly = false; object isVisible = true; object file1 = fileName; object html1 = pFileName; object format = WdSaveFormat.wdFormatDocument; ApplicationClass oWordApp = new ApplicationClass(); oWordApp.Visible = false; Document oWordDoc = oWordApp.Documents.Open(ref file1, ref format, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); oWordApp.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdPrintView;//将web视图修改为默认视图,不然打开word的时候会以web视图去展示,而不是默认视图。(唯独这句代码是自己加的 = =|||) oWordDoc.SaveAs(ref html1, ref format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); oWordDoc.Close(ref missing, ref missing, ref missing); oWordDoc = null; oWordApp.Application.Quit(ref missing, ref missing, ref missing); oWordApp = null; // killAllProcess(); } }
2转换
public class HTMLToWord { private static void HtmlToMht(string src, string dst) { CDO.Message msg = new CDO.Message(); CDO.Configuration c = new CDO.Configuration(); msg.Configuration = c; msg.CreateMHTMLBody(src, CDO.CdoMHTMLFlags.cdoSuppressNone, "", ""); ADODB.Stream stream = msg.GetStream(); stream.SaveToFile(dst, ADODB.SaveOptionsEnum.adSaveCreateOverWrite); } /// <summary> /// html转换成word /// </summary> /// <param name="title">word的名称(内容的标题)</param> /// <param name="content">html的内容</param> /// <param name="path">转换后word的保存的路径</param> public static void WriteHtml(string title, string content,string path)//参数内容都是从数据库读出来的文章信息,其中content就是ewebeditor生成的html代码 { DateTime dt = DateTime.Now;//将string型的日期格式转为DateTime型的因为默认的日期格式不能作为文件名,所以将日期的“:”替换为“-” string Temp_Name = HttpContext.Current.Server.MapPath("~/Content") + "/HtmlTemp.html"; string File_Name = @"D:\abc\a.html";//生成html文件的路径 string File_NameM = @"D:\abc\b.mht";//生成mht文件的路径 string File_Name2 = path+@"\"+title+".doc";//生成Word文档的路径 StreamReader sr = new StreamReader(Temp_Name); StringBuilder htmltext = new StringBuilder(); String line; while ((line = sr.ReadLine()) != null) { htmltext.Append(line);//读取到html模板的内容 } sr.Close(); //替换相应的内容到指定的位置 htmltext = htmltext.Replace("$htmldata[1]", title); htmltext = htmltext.Replace("$htmldata[2]", title); htmltext = htmltext.Replace("$htmldata[3]", content); using (StreamWriter sw = new StreamWriter(File_Name, false, System.Text.Encoding.GetEncoding("UTF-8"))) //保存地址 { //生成HTML文件 sw.WriteLine(htmltext); sw.Flush(); sw.Close(); } HtmlToMht(File_Name, File_NameM);//因为带图片的html直接转为Word的话,图片会以引用的形式展示(也就是说不是内置到word文档里去的,一旦断网或将图片放在别的路径之后,打开word文档图片会显示不出来,所以通过折冲的办法先生成html,然后转换为mht,再转为word) WordAction.SaveAsWord(File_NameM, File_Name2);//生成word } }
HTML转换成word文档
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。