实际上,如果您将值作为 HTML 传递并使用某些东西将文本格式化为 justify'ed html 文本,则实际上可以在 SSRS 报告中对齐文本,在我的情况下,我使用 .NET C# 将传递的字符串格式化为对齐的 html文本。
但在此之前,我们需要配置我们的 SSRS 报告以接受 HTML,为此我们需要添加一个文本框并创建一个占位符。
要添加占位符,请单击文本框,直到它允许您向其中写入文本,然后右键单击并选择“创建占位符...”
创建占位符后,系统会提示您输入占位符的属性,您只需要指定值和标记类型
确保将标记类型选择为 HTML,并为值指定将具有对齐的 html 文本的变量,在我们的例子中,我们将其称为transformedHtml。
现在我们需要创建一个函数,将我们的字符串转换为对齐的 HTML 文本
/// <summary>
///
/// </summary>
/// <param name="text">The text that we want to justify</param>
/// <param name="width">Justified text width in pixels</param>
/// <param name="useHtmlTagsForNewLines">if true returns the output as justified html if false returns the ouput as justified string</param>
/// <returns>Justified string</returns>
public string GetText(string text, int width, bool useHtmlTagsForNewLines = false)
{
var palabras = text.Split(' ');
var sb1 = new StringBuilder();
var sb2 = new StringBuilder();
var length = palabras.Length;
var resultado = new List<string>();
var graphics = Graphics.FromImage(new Bitmap(1, 1));
var font = new Font("Times New Roman", 11);
for (var i = 0; i < length; i++)
{
sb1.AppendFormat("{0} ", palabras[i]);
if (graphics.MeasureString(sb1.ToString(), font).Width > width)
{
resultado.Add(sb2.ToString());
sb1 = new StringBuilder();
sb2 = new StringBuilder();
i--;
}
else
{
sb2.AppendFormat("{0} ", palabras[i]);
}
}
resultado.Add(sb2.ToString());
var resultado2 = new List<string>();
string temp;
int index1, index2, salto;
string target;
var limite = resultado.Count;
foreach (var item in resultado)
{
target = " ";
temp = item.Trim();
index1 = 0; index2 = 0; salto = 2;
if (limite <= 1)
{
resultado2.Add(temp);
break;
}
while (graphics.MeasureString(temp, font).Width <= width)
{
if (temp.IndexOf(target, index2) < 0)
{
index1 = 0; index2 = 0;
target = target + " ";
salto++;
}
index1 = temp.IndexOf(target, index2);
temp = temp.Insert(temp.IndexOf(target, index2), " ");
index2 = index1 + salto;
}
limite--;
resultado2.Add(temp);
}
var res = string.Join(useHtmlTagsForNewLines ? "<br> " + Environment.NewLine : "\n", resultado2);
if (useHtmlTagsForNewLines)
res = $"<div>{res.Replace(" ", " ").Replace("<br> ", "<br>")}</div>";
return res;
}
通过使用此函数,我们可以将任何字符串转换为两端对齐的文本,并且我们可以选择输出是 HTMl 还是简单字符串
那么我们可以像这样调用这个方法
string text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
string transformedHtml = GetText(text, 350, true);
我们得到如下输出:
在 C# 中
在 SSRS 中
现在这个例子主要展示了如果你将值从 C# 代码传递到 ssrs 报告时如何获得对齐的文本,但是如果你在存储过程中创建相同的函数来以相同的方式格式化任何文本,你就可以实现这一点。希望这对某人有所帮助。