.NET word内容转换成HTML

 using System.IO;
using Word = Microsoft.Office.Interop.Word;

public static string getWordFulltext(string WordFilePath, string docId)
        {
            string docText = "";
            try
            {
                if (File.Exists(WordFilePath) && docId != "" && WordFilePath.EndsWith(".doc"))
                {

                    Word.Application wordApp = new Word.Application();
                    object Source = WordFilePath;
                    string SaveHtmlPath = HttpContext.Current.Server.MapPath("UploadHTML") + "\\" + docId + ".html";
                    object Target = SaveHtmlPath;
                    object Unknown = Type.Missing;
                    object readOnly = true;
                    Word.Document doc = wordApp.Documents.Open(ref Source, ref Unknown, ref readOnly, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                    docText = doc.Content.Text.ToString();
                    object format = Word.WdSaveFormat.wdFormatHTML;
                    doc.SaveAs(ref Target, ref format, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                    doc.Close(ref Unknown, ref Unknown, ref Unknown);
                    wordApp.Quit(ref Unknown, ref Unknown, ref Unknown);

                }

            }
            catch { }
            return docText;
        }

posted @ 2012-08-23 09:52  尹成亮  阅读(661)  评论(0编辑  收藏  举报