【问题标题】:CSS for mso classes in web project ASP.NETWeb 项目 ASP.NET 中 mso 类的 CSS
【发布时间】:2015-10-28 13:27:36
【问题描述】:

这是我的情况:人们一直在用 Word 制作文档并将文本粘贴到 FreeTextBox 控件中。此文本随后会保存到 SQL 表中,并稍后显示在屏幕上。在此过程的桌面版本中,数据以用户创建的所有格式(表格、样式等)显示。现在,这个过程已经转移到一个 Web 应用程序中——虽然数据仍然保存并显示在屏幕上,但表格单元格边框之类的东西不再存在。我注意到 Word 在这些单元格上使用的属性以 mso-some-style-prop: some attribute;

为前缀

MSO-anything 不是有效属性。如何让 CSS 正确应用于 Web 项目中的用户控件?我一直在寻找一个没有运气的扩展,并且我试图创建一个替换这些属性的方法,但是它非常繁琐且耗时。所以,在我走这条路之前,我想与 SO 社区核实一下,看看是否有任何东西可以帮助缓解困难。提前致谢。

Microsoft 样式示例:

<TABLE style="mso-cellspacing: 1.5pt; mso-yfti-tbllook: 1184; mso-padding-    alt: 0in 0in 0in 0in" class=MsoNormalTable border=1 cellPadding=0>

【问题讨论】:

  • 嘿,祝你好运。 MS Office 生成的 HTML 绝对是一团糟。您可能有正确的想法将其转换为更有用的东西。也许您可以更进一步,创建一个其他人可以贡献的开源库?或者看看是否已经存在为此目的的库?
  • 不幸(或幸运),我不得不走的路越来越像创建图书馆。
  • 这是你用的吗? - freetextbox.com/demos/everything.aspx ,因为它有一个 Clean MS Word HTML 按钮 - 刚刚使用 Word 2010 中的文本对其进行了测试,看起来它不起作用。
  • 这就是正在使用的。可悲的是,它只是消除了所有样式。
  • 您可以尝试另一个所见即所得的编辑器,例如 tinymce - 它具有剥离 MSO 内容的功能。

标签: html css asp.net vb.net ms-word


【解决方案1】:

我想与大家分享解决方案。我最终找到了这个链接:http://blog.codinghorror.com/cleaning-words-nasty-html/

它很好地完成了这项工作。我对其中一个正则表达式有疑问,但它是一个很好的起点。希望这会对某人有所帮助。

这是我用的:

Public Module Parser

    Public Function Main(text As String) As String

        Dim html As String = ""
        'Console.WriteLine("input html is " + text.Length + " chars")
        html = CleanWordHtml(text)
        html = FixEntities(text)
        Return html
    End Function

    Private Function CleanWordHtml(html As String) As String


        Dim sc As 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 - I was unable to get this regular expression to work working
        'sc.Add("<(meta|link|/?o:|/?style|/?div|/?std|/?head|/?html|body|/?body|/?span|![)[^>]*?>")
        'sc.Add("<(meta|link|/?o:|/?style|/?div|/?std|/?head|/?html|body|/?body|/?span|![)[^>]*?>")
        ' Get rid of empty paragraph tags
        sc.Add("(<[^>]+>)+&nbsp;(</w+>)+")
        ' remove bizarre v: element attached to <img> tag
        sc.Add("s+v:w+=""[^""]+""")
        ' remove extra lines
        sc.Add("(nr){2,}")
        For Each s As String In sc
            html = Regex.Replace(html, s, "", RegexOptions.IgnoreCase)
        Next
        Return html
    End Function
    Private Function FixEntities(html As String) As String
        Dim nvc As New NameValueCollection()
        nvc.Add("""", "&ldquo;")
        nvc.Add("""", "&rdquo;")
        nvc.Add("–", "&mdash;")
        For Each key As String In nvc.Keys
            html = html.Replace(key, nvc(key))
        Next
        Return html
    End Function
End Module

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 2017-10-28
    相关资源
    最近更新 更多