word转化为HTML有了很多微软的垃圾代码什么的...怎样能去除啊? 去除代码该如何编写呢?或者有转化工具吗? 我有上万文档需要转化!沉余代码太多! 速度啊! 谢谢!

解决方案 »

  1.   

       /// <summary>
            /// 清理Word生成的冗余HTML 
            /// </summary>
            /// <param name="html"></param>
            /// <returns></returns>
            public static string CleanWordHtml(string html)
            {
                StringCollection sc = new StringCollection();
                // get rid of unnecessary tag spans (comments and title)
                sc.Add(@"<!--(\w|\W)+?-->");
                sc.Add(@"<title>(\w|\W)+?</title>");
                // Get rid of classes and styles
                sc.Add(@"\s?class=\w+");
                sc.Add(@"\s+style='[^']+'");
                // Get rid of unnecessary tags
                //sc.Add(@"<(meta|link|/?o:|/?style|/?div|/?st\d|/?head|/?html|body|/?body|/?span|!\[)[^>]*?>");
                sc.Add(@"<(meta|link|/?o:|/?style|/?font|/?strong|/?st\d|/?head|/?html|body|/?body|/?span|!\[)[^>]*?>");
                // Get rid of empty paragraph tags
                sc.Add(@"(<[^>]+>)+ (</\w+>)+");
                // remove bizarre v: element attached to <img> tag
                sc.Add(@"\s+v:\w+=""[^""]+""");
                // remove extra lines
                sc.Add(@"(\n\r){2,}");
                foreach (string s in sc)
                {
                    html = Regex.Replace(html, s, "", RegexOptions.IgnoreCase);
                }
                return html;
            }