/// <summary> /// Word转成Html /// </summary> /// <param name="path">要转换的文档的路径</param> /// <param name="savePath">转换成html的保存路径</param> /// <param name="wordFileName">转换成html的文件名字</param> public static void Word2Html(string path, string savePath, string wordFileName) { Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); 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[] { (object)path, true, true }); Type docType = doc.GetType(); string strSaveFileName = savePath + wordFileName + ".html"; object saveFileName = (object)strSaveFileName; docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML }); docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null); wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); }
/// <summary> /// 转换word为pdf /// </summary> /// <param name="filename"></param> /// <param name="savefilename"></param> public static void ConvertWordPDF2(object filename, object savefilename) { Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); Type wordType = wordApp.GetType(); Microsoft.Office.Interop.Word.Documents docs = wordApp.Documents; Type docsType = docs.GetType(); Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, (object)docs, new Object[] { filename, true, true }); Type docType = doc.GetType(); docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { savefilename, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF }); docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null); wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, wordApp, null); }
/// <summary> /// 将doc文档转换成html /// </summary> /// <param name="FilePath">虚拟路径文件名</param> /// <param name="NewHtmlName">新的html文件名</param> /// <param name="StrHtml">获取的Html</param> /// <param name="IsDelDocFile">是否删除doc文件</param> /// <param name="IsDelHtmlFile">是否删除html文件</param> /// <returns>如果返回false,则NewHtmlName和StrHtml均为空</returns> public bool Convertion2Html(string FilePath, out string NewHtmlName, out string StrHtml, bool IsDelDocFile, bool IsDelHtmlFile) { NewHtmlName = ""; StrHtml = ""; if (FilePath.Trim() == "") { return false; } //必须先关闭WINWORD try { System.Diagnostics.Process[] CloseID = System.Diagnostics.Process.GetProcessesByName("WINWORD"); for (int i = 0; i < CloseID.Length; i++) { if (!CloseID[i].CloseMainWindow()) { CloseID[i].Kill(); } } } catch { } //绝对路径,同时将后缀名定位小写doc,防止大写 string RealFileNamePath = HttpContext.Current.Server.MapPath(FilePath); RealFileNamePath = RealFileNamePath.Substring(0, RealFileNamePath.Length - 3) + "doc"; RealFileNamePath = "d:\\2.doc"; //调用word类库 Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); Type wordType = word.GetType(); Microsoft.Office.Interop.Word.Documents docs = word.Documents; Type docsType = docs.GetType(); //读取doc文档 Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document(); try { doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod,null, docs, new Object[] { RealFileNamePath, true, true }); } catch { try { wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); } catch { } return false; } // 转换格式,另存为 Type docType = doc.GetType(); object saveFileName = RealFileNamePath.Replace(".doc", ".htm"); try { docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML }); ///其它格式: ///wdFormatHTML ///wdFormatDocument ///wdFormatDOSText ///wdFormatDOSTextLineBreaks ///wdFormatEncodedText ///wdFormatRTF ///wdFormatTemplate ///wdFormatText ///wdFormatTextLineBreaks ///wdFormatUnicodeText wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); } catch { // 退出 Word try { wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); } catch { } return false; } //返回的数据 NewHtmlName = FilePath.Replace(".doc", ".htm"); //一定要等待几秒再操作,否则由于缓存的文件没有写入,会导致异常 System.Threading.Thread.Sleep(3000); StrHtml = this.GetFileContent(NewHtmlName); //根据需要删除文件 if (IsDelDocFile) { System.IO.File.Delete(RealFileNamePath); } if (IsDelHtmlFile) { System.IO.File.Delete(RealFileNamePath.Replace(".doc", ".htm")); ////删除文件夹,先判断是否存在该目录 //string dirPath = RealFileNamePath.Replace(".doc", ".files"); //if (System.IO.Directory.Exists(dirPath)) // System.IO.Directory.Delete(dirPath, true); } return true; }