You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SSRS中文本两端对齐与加粗功能的冲突问题求解

解决SSRS中同时实现文本两端对齐与HTML加粗的问题

问题根源

此前通过手动插入空格实现两端对齐,但设置HTML标记类型后,SSRS的HTML渲染引擎会忽略多余空格,直接导致对齐效果失效。

解决方案:用HTML CSS样式替代手动空格对齐

直接利用SSRS支持的HTML/CSS实现两端对齐,同时保留HTML标签的格式支持,具体步骤如下:

  1. 修改VB函数逻辑
    移除原函数中手动插入空格的代码块,改为输出带CSS样式的HTML结构,同时保留对<b>标签的支持。修改后的函数示例:
Function GetTextWithFormat(text As String, width As Integer) As String
    Dim palabras As String() = text.Split(" "c)
    Dim sb1 As New System.Text.StringBuilder()
    Dim sb2 As New System.Text.StringBuilder()
    Dim length As Integer = palabras.Length
    Dim resultado As New System.Collections.Generic.List(Of String)()
    Dim i As Integer = 0
    
    While i < length
        sb1.AppendFormat("{0} ", palabras(i))
        If sb1.ToString().Length > width Then
            resultado.Add(sb2.ToString())
            sb1 = New System.Text.StringBuilder()
            sb2 = New System.Text.StringBuilder()
            System.Math.Max(System.Threading.Interlocked.Decrement(i), i + 1)
        Else
            sb2.AppendFormat("{0} ", palabras(i))
        End If
        System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
    End While
    resultado.Add(sb2.ToString())

    ' 拼接结果并替换换行符为HTML换行标签
    Dim builder As New System.Text.StringBuilder()
    For Each item As String In resultado
        builder.Append(item.Trim()).Append("<br>")
    Next

    ' 用带两端对齐样式的div包裹内容,指定等宽字体
    Return $"<div style='text-align: justify; font-family: Courier New;'>{builder.ToString()}</div>"
End Function
  1. 配置报表文本框
  • 将文本框的标记类型设置为HTML - Interpret HTML tags as styles
  • 确保文本框使用等宽字体(如Courier New),保证字符宽度一致,避免对齐偏差

原理说明

SSRS的HTML渲染引擎支持text-align: justify CSS属性,该属性会自动完成文本两端对齐,无需手动插入空格;同时<b>等HTML标签会被正常解析,实现加粗效果。保留等宽字体的要求,是为了避免不同字符宽度差异导致的对齐错位。

内容的提问来源于stack exchange,提问作者Shan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 21:05:00