word转换成HTML 以及IE不兼容问题

Posted on 2014-09-26 18:23  月下小王子  阅读(1264)  评论(0编辑  收藏  举报
public static bool WordToHtml(string wordFileName, string htmlFileName)
        {
            try
            {
                Object oMissing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word._Application WordApp = new Microsoft.Office.Interop.Word.Application();
                WordApp.Visible = false;
                object filename = wordFileName;
                _Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                // Type wordType = WordApp.GetType();
                // 打开文件
                Type docsType = WordApp.Documents.GetType();
                // 转换格式,另存为
                Type docType = WordDoc.GetType();
                object saveFileName = htmlFileName;
                docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, WordDoc,
                    new object[] { saveFileName, WdSaveFormat.wdFormatHTML });

                //保存
                WordDoc.Save();
                WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                return false;
            }
        }

上面的方法是在winform中解决有Word转换成HTML网页的,在转换过程中,会产生一个网页和一个文件夹,如果想将这个网页在前台显示出来,第一步,需要将其这个网页源码中的XXX.files(转换过程中产生的文件夹的名称),转换成正确的路径,但是这样显示出来就会有一个问题,那就是在别的浏览器好使 ,但是在IE浏览器中是什么都不显示的,也就是传说中的不兼容,那么,接下来看我们的第二步.

第二步:删除源文件中的<![if !vml]> <![endif]><![if !vml]><![endif]>这四个标签,或者用string.Replace()替换成空值,这样就显示出来了,业解决掉了不兼容的问题