c# word轉html(另存為)
阿新 • • 發佈:2020-08-13
c# word轉html(另存為)
方案一:
/// <summary> /// word另存為html /// </summary> /// <param name="filePath"></param> private void WordToHTML(string filePath) { var rootPath = AppDomain.CurrentDomain.BaseDirectory; var fileName = Path.GetFileNameWithoutExtension(filePath);object nothing = Missing.Value; object newPath = string.Format(@"{0}File\html\{1}.html", rootPath, fileName); object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML; Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document document= application.Documents.Add(filePath, nothing, nothing); document.SaveAs2(newPath, format, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, Encoding.UTF8.CodePage, nothing, nothing, nothing); document.Close(nothing, nothing, nothing); application.Quit(nothing, nothing, nothing); }
方案二:
/// <summary> /// word另存為html /// </summary> /// <param name="filePath"></param> private void WordToHTML2(string filePath) { var rootPath = AppDomain.CurrentDomain.BaseDirectory; var fileName = Path.GetFileNameWithoutExtension(filePath); object newPath = string.Format(@"{0}File\html\{1}.html", rootPath, fileName); Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.ApplicationClass(); Type wordType = word.GetType(); Microsoft.Office.Interop.Word.Documents docs = word.Documents; Type docsType = docs.GetType(); Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { filePath, true, true }); Type docType = doc.GetType(); docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { newPath, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML }); wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); }