/// <summary> /// word转成html /// </summary> /// <param name="wordFileName">word文档的相对路径</param> /// <returns>转化后的Html文档的物理路径</returns> private string WordToHtml(object wordFileName) { wordFileName = HttpContext.Current.Server.MapPath(wordFileName.ToString()); //在此处放置用户代码以初始化页面 Microsoft.Office.Interop.Word.ApplicationClass 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[] { wordFileName, true, true }); //转换格式,另存为 Type docType = doc.GetType(); string wordSaveFileName = wordFileName.ToString(); string strSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.LastIndexOf('.')) + ".html"; object saveFileName = (object)strSaveFileName; //下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成: /* * 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 docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML }); docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null); //退出 Word wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); return(saveFileName.ToString()); }
public void pdf(String wordpath, String pdf) { // Wordを取り扱うための変数を定義する // ※ExcelやPowerPointも同じようなクラス名なので明示的に名前空間を書くことにする Microsoft.Office.Interop.Word.Application objWord = null; Microsoft.Office.Interop.Word.Documents objDocuments = null; Microsoft.Office.Interop.Word.Document objDocument = null; // ファイル名を定義する string strWordFilePath = string.Empty; string strPdfFilePath = pdf; try { // ファイルパスを初期化する // ※今回PDFはWord文章と同じ場所に拡張子だけ変えて保存するようにした strWordFilePath = wordpath; // strPdfFilePath = System.IO.Path.GetDirectoryName(strWordFilePath) + @"\" + System.IO.Path.GetFileNameWithoutExtension(strWordFilePath) + @".pdf"; // Wordオブジェクトを実体化する objWord = new Microsoft.Office.Interop.Word.Application(); // 文章を管理しているオブジェクトを取得する objDocuments = objWord.Documents; // PDFに変換するWord文章を開く objDocument = objDocuments.Open(strWordFilePath); // PDF形式で保存する objDocument.ExportAsFixedFormat(strPdfFilePath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF); } catch (System.Exception objException) { System.Console.WriteLine(objException.Message); } finally { // 例外が発生しても必ずWordの文章を閉じるように処理はここ if (objDocument != null) { objDocument.Close(); } // 例外が発生しても必ずWordを狩猟するように処理はここ if (objWord != null) { objWord.Quit(); } } }