【问题标题】:ClosedXML reading text color from cell rich textClosedXML 从单元格富文本中读取文本颜色
【发布时间】:2014-01-09 08:50:29
【问题描述】:

我正在通过 ASP.NET/VB.NET 使用 ClosedXML 将 XLSX 文件导入数据库。我的客户需要保持单个单元格的格式,因此解决方案是使用单元格的 RichText 属性。

我写了一段代码,应该将 RichText 对象转换为 HTML 字符串,但是 ClosedXML 不返回颜色信息(无论文本颜色是什么,它总是返回 #FFFF3333。

这是我的sn-p:

Shared Function RichTextToHTML(ByVal rt1 As IXLRichText) As String
    'trasforma una string rich text in html
    Dim ret As String = ""
    If rt1.Text <> "" Then
        For Each rts1 As IXLRichString In rt1
            ret &= "<span style='"
            If rts1.Bold Then
                ret &= "font-weight:bold;"
            End If
            If rts1.Strikethrough Then
                ret &= "text-decoration:line-through;"
            End If
            ret &= "color:#" & rts1.FontColor.ToString & ";"
            ret &= "'>" & rts1.Text & "</span>"
        Next
    End If
    Return ret
End Function

“rts1.FontColor”永远不会返回正确的颜色。我也尝试使用其他库,如 SpreadsheetLight,但结果相同:电子表格单元格内的富文本没有颜色。

有什么想法吗?

【问题讨论】:

    标签: asp.net vb.net excel closedxml


    【解决方案1】:

    它对我有用(在带有 C# 的 0.68.1 版本中)。这段代码

    IXLCell cell = sheet.Cell(1,1);
    cell.RichText.ForEach(rts => Console.WriteLine(rts.Text + " / " + rts.FontColor));
    

    给出输出

    red / FFFF0000
    blue / Color Theme: Accent5, Tint: 1
    green / Color Theme: Accent6, Tint: 1
    

    这与我在 Excel 中制作的文件一致。
    但请注意,主题中的颜色不会给出十六进制颜色。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 2014-06-16
      • 2017-07-20
      相关资源
      最近更新 更多