教程:设置文本格式(报表生成器)
在本教程中,可以练习在 Reporting Services 分页报表中以不同方式设置文本格式。 可以尝试使用不同的格式。
在设置了具有数据源和数据集的空白报表后,你可以选择想要尝试的格式。 下图所示报表与你在此教程中创建的报表类似。
在一个步骤中,你可以故意犯某个错误,以便可以了解错误的成因。 然后,您可以纠正该错误以便实现预期效果。
完成本教程的估计时间:20 分钟。
要求
有关要求的信息,请参阅教程先决条件(报表生成器)。
创建具有数据源和数据集的一个空白报表
创建空白报表
通过计算机、Web 门户或 SharePoint 集成模式 启动报表生成器 Reporting Services 。
将打开“新建报表或数据集”对话框。
如未显示“新建报表或数据集”对话框,请通过“文件”菜单>“新建”转至该对话框。
在 “入门” 对话框的左窗格中,确保已选择 “新建报表” 。
在右窗格中,选择“空白报表”。
创建数据源
在“报表数据”窗格中,选择“新建”>“数据源”。
如果未出现“报表数据”窗格,请在“视图”选项卡上选中“报表数据”。
在“名称”框中输入:TextDataSource
选择“使用我的报表中嵌入的连接” 。
验证连接类型是否为 Microsoft SQL Server,然后在“连接字符串”框中输入:
Data Source = <servername>
注意
表达式
<servername>
(例如 Report001)指定安装了 SQL Server 数据库引擎实例的计算机。 本教程不需要具体数据;只需要与 SQL Server 数据库的连接。 如果你已经具有在“数据源连接”下列出的某一数据源连接,则可以选择该连接并且转到下一过程“创建数据集”。有关详细信息,请参阅获取数据连接的备选方法(报表生成器)。选择“确定”。
创建数据集
在“报表数据”窗格中,选择“新建”>“数据集”。
确保数据源为 TextDataSource。
在“名称”文本框中,输入“TextDataset”。
确保已选择“文本”查询类型,然后选择“查询设计器”。
选择“编辑为文本”。
将以下查询粘贴到查询窗格中:
注意
在本教程中,由于查询已经包含了数据值,因此它不需要外部数据源。 这样,查询就会非常长。 在业务环境中,查询不会包含数据。 本教程中的查询仅供学习使用。
SELECT CAST('2015-01-05' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory, 'Accessories' as Subcategory,'Carrying Case' as Product, CAST(16996.60 AS money) AS Sales, 68 as Quantity, 'Install Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2015-01-05' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory, 'Accessories' as Subcategory, 'Carrying Case' as Product, CAST(13747.25 AS money) AS Sales, 55 as Quantity, 'Report Builder in SQL Server' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2015-01-05' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory, 'Accessories' as Subcategory,'Carrying Case' as Product, CAST(9248.15 AS money) As Sales, 37 as Quantity, 'What is New in Reporting Services (SSRS)' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2015-01-05' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory, 'Accessories' as Subcategory,'Tripod' as Product, CAST(1350.00 AS money) AS Sales, 18 as Quantity, 'Install Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2015-01-05' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory, 'Accessories' as Subcategory,'Tripod' as Product, CAST(1800.00 AS money) AS Sales, 24 as Quantity, 'Report Builder in SQL Server' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2015-01-05' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory, 'Accessories' as Subcategory,'Tripod' as Product, CAST(1125.00 AS money) AS Sales, 15 as Quantity, 'What is New in Reporting Services (SSRS)' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2015-01-05' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory, 'Accessories' as Subcategory,'Lens Adapter' as Product, CAST(1147.50 AS money) AS Sales, 17 as Quantity, 'Install Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2015-01-05' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory, 'Accessories' as Subcategory, 'Lens Adapter' as Product, CAST(742.50 AS money) AS Sales, 11 as Quantity, 'Report Builder in SQL Server' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2015-01-05' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory, 'Accessories' as Subcategory,'Lens Adapter' as Product, CAST(1417.50 AS money) AS Sales, 21 as Quantity, 'What is New in Reporting Services (SSRS)' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2015-01-06' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory, 'Accessories' as Subcategory, 'Carrying Case' as Product, CAST(13497.30 AS money) AS Sales, 54 as Quantity, 'Install Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2015-01-06' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory, 'Accessories' as Subcategory, 'Carrying Case' as Product, CAST(11997.60 AS money) AS Sales, 48 as Quantity, 'Report Builder in SQL Server' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2015-01-06' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory, 'Accessories' as Subcategory, 'Carrying Case' as Product, CAST(10247.95 AS money) As Sales, 41 as Quantity, 'What is New in Reporting Services (SSRS)' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2015-01-06' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory, 'Accessories' as Subcategory, 'Tripod' as Product, CAST(1200.00 AS money) AS Sales, 16 as Quantity, 'Install Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2015-01-06' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory, 'Accessories' as Subcategory,'Tripod' as Product, CAST(2025.00 AS money) AS Sales, 27 as Quantity, 'Report Builder in SQL Server' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2015-01-06' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory, 'Accessories' as Subcategory,'Tripod' as Product, CAST(1425.00 AS money) AS Sales, 19 as Quantity, 'What is New in Reporting Services (SSRS)' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2015-01-06' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory, 'Accessories' as Subcategory,'Lens Adapter' as Product, CAST(887.50 AS money) AS Sales, 13 as Quantity, 'Install Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2015-01-06' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory, 'Accessories' as Subcategory, 'Lens Adapter' as Product, CAST(607.50 AS money) AS Sales, 9 as Quantity, 'Report Builder in SQL Server' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2015-01-06' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory, 'Accessories' as Subcategory,'Lens Adapter' as Product, CAST(1215.00 AS money) AS Sales, 18 as Quantity, 'What is New in Reporting Services (SSRS)' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2015-01-05' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory, 'Digital' as Subcategory,'Compact Digital' as Product, CAST(10191.00 AS money) AS Sales, 79 as Quantity, 'Install Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2015-01-05' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory, 'Digital' as Subcategory, 'Compact Digital' as Product, CAST(8772.00 AS money) AS Sales, 68 as Quantity, 'Report Builder in SQL Server' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2015-01-05' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory, 'Digital' as Subcategory, 'Compact Digital' as Product, CAST(10578.00 AS money) AS Sales, 82 as Quantity, 'What is New in Reporting Services (SSRS)' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2015-01-05' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory,'Digital' as Subcategory, 'Slim Digital' as Product, CAST(7218.10 AS money) AS Sales, 38 as Quantity, 'Install Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2015-01-05' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory,'Digital' as Subcategory, 'Slim Digital' as Product, CAST(8357.80 AS money) AS Sales, 44 as Quantity, 'Report Builder in SQL Server' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2015-01-05' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory,'Digital' as Subcategory,'Slim Digital' as Product, CAST(9307.55 AS money) AS Sales, 49 as Quantity, 'What is New in Reporting Services (SSRS)' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2015-01-06' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory, 'Digital' as Subcategory,'Compact Digital' as Product, CAST(3870.00 AS money) AS Sales, 30 as Quantity, 'Install Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2015-01-06' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory, 'Digital' as Subcategory,'Compact Digital' as Product, CAST(5805.00 AS money) AS Sales, 45 as Quantity, 'Report Builder in SQL Server' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2015-01-06' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory, 'Digital' as Subcategory, 'Compact Digital' as Product, CAST(8643.00 AS money) AS Sales, 67 as Quantity, 'What is New in Reporting Services (SSRS)' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2015-01-06' AS date) as SalesDate, 'Lauren Johnson' as FullName,'Central' as Territory, 'Digital' as Subcategory, 'Slim Digital' as Product, CAST(9877.40 AS money) AS Sales, 52 as Quantity, 'Install Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2015-01-06' AS date) as SalesDate, 'Warren Pal' as FullName,'North' as Territory, 'Digital' as Subcategory, 'Slim Digital' as Product, CAST(12536.70 AS money) AS Sales, 66 as Quantity, 'Report Builder in SQL Server' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2015-01-06' AS date) as SalesDate, 'Fernando Ross' as FullName,'South' as Territory, 'Digital' as Subcategory, 'Slim Digital' as Product, CAST(6648.25 AS money) AS Sales, 35 as Quantity, 'What is New in Reporting Services (SSRS)' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL
选择“运行”(!) 以运行查询。
查询结果将是可用于在您的报表中显示的数据。
选择“确定”。
选择“确定”。
向报表 Design Surface 添加字段
如果你希望来自你的数据集的字段出现在报表中,则第一感可能是要将其直接拖到 Design Surface。 本练习指出为什么无法这样做以及相应替代步骤。
向报表添加字段(获得错误结果)
将 FullName 字段从“报表数据”窗格中拖到设计图面中。
报表生成器将创建一个文本框,文本框中有一个表达式,该表达式表示为
<Expr>
。选择“运行”。
你仅看到一条记录 Fernando Ross,它是在查询中按字母排序的第一条记录。 该字段并不重复以便显示该字段中的其他记录。
选择“设计”返回设计视图。
在文本框中选择表达式
<Expr>
。在“属性”窗格中,对于“值”属性,将看到以下内容(如果没有看到“属性”窗格,则在“视图”选项卡上,选中“属性”):
=First(Fields!FullName.Value, "TextDataSet")
First
函数设计为只检索字段中的第一个值。将该字段直接拖到设计图面上创建了一个文本框。 文本框本身并非数据区域,因此它们不显示来自报表数据集的数据。 数据区域(例如表、矩阵和列表)中的文本框显示数据。
选择文本框(如果您已经选择了表达式,则按下 Esc 以便选择该文本框),然后按 Delete 键。
向报表添加字段(获得正确结果)
在功能区的“插入”选项卡的“数据区域”区域中,选择“列表”。 选择 Design Surface ,然后拖动以便创建一个框,该框大约 2 英寸宽、1 英寸高。
将 FullName 字段从“报表数据”窗格中拖到列表框中。
此时,报表生成器将创建一个文本框,表达式
[FullName]
将位于该文本框中。选择“运行”。
此时该框将重复以便显示查询中的所有记录。
选择“设计”返回设计视图。
在文本框中选择该表达式。
在“属性”窗格中,对于“值”属性,将看到以下内容:
=Fields!FullName.Value
通过将文本框拖到列表数据区域,将显示处于数据集该字段中的数据。
选择列表框并按 Delete 键。
向报表 Design Surface 添加表
创建此表是为了放置超链接和旋转后的文本。
在“插入”选项卡上,转至“表”>“表向导”。
在“新建表或矩阵”向导的“选择数据集”页上,选择“选择此报表中的现有数据集或共享数据集”>“TextDataset(在此报表中)”>“下一步”。
在“排列字段”页上,将“Territory”、“LinkText”和“Product”字段拖到“行组”上,将“Sales”字段拖到“值”上,然后选择“下一步”。
在“选择布局”页上,取消选中“展开/折叠组”复选框,以便查看整个表,然后选择“下一步”。
选择“完成”。
选择“运行”。
该表看起来没问题,但具有两个总计行。 LinkText 列不需要总计行。
选择“设计”返回设计视图。
选择“LinkText”列中的“总计”单元格,然后按住 Shift 键并选择右侧的两个单元格:“Product”列中的空单元格和“Sales”列中的
[Sum(Sales)]
单元格。选中这三个单元格后,右键单击其中一个单元格,然后选择“删除行”。
选择“运行”。
现在它只具有一个总计行。
向报表添加超链接
在本节中,您将向前一节中的表中的文本添加超链接。
选择“设计”返回设计视图。
右键单击包含
[LinkText]
的单元格,然后选择“文本框属性”。在“操作”选项卡上,选择“转到 URL” 。
在“选择 URL”框中,选择 [URL],然后选择“确定”。
文本看起来没有任何不同。 您需要使其看起来像链接文本。
选择
[LinkText]
。在“开始”选项卡上,转至“字体”,选择“下划线”,然后将“颜色”更改为“蓝色”。
选择“运行”。
文本现在看起来像链接了。
选择链接。 如果计算机已连接到 Internet,则浏览器将打开到报表生成器的帮助文章。
旋转报表中的文本
在本节中,您将旋转前一节的表中的某些文本。
选择“设计”返回设计视图。
在包含
[Territory].
的单元格中选择在“主文件夹”选项卡上的“字体”部分中,选择“加粗”按钮。
如果“属性”窗格未打开,请在“视图”选项卡上选中“属性”复选框。
在“属性”窗格中,找到 WritingMode 属性,并将其值从“默认”更改为“Rotate270” 。
注意
对“属性”窗格中的属性进行分类时,WritingMode 位于“本地化”类别中。 请确保您选择的是单元,而非文本。 WritingMode 是文本框的属性,而非文本的属性。
在“开始”选项卡上的“段落”部分中,选择“垂直居中”和“水平居中”,以便从垂直和水平方向上都将文本定位于单元格的中心。
选择“运行”(!)。
现在, [Territory]
单元中的文本将从单元的底部到顶部垂直放置。
设置货币格式
选择“设计”切换到设计视图。
选择包含
[Sum(Sales)]
的顶部表单元,按住 Shift 键,然后选择包含[Sum(Sales)]
的底部表单元。在“主文件夹”选项卡上,转至“数字”组 > 然后选择“货币”按钮。
(可选)如果区域设置为“英语(美国)”,则默认示例文本为 [$12,345.00]。 如果在“数字”组中看不到示例货币值,请选择“占位符样式”>“示例值”。
(可选)在“主文件夹”选项卡上的“数字”组中,选择两次“减少小数位数”按钮,显示不带美分的美元数字。
选择“运行”(!) 以预览报表。
报表现在将显示设置了格式的数据并且更易于阅读。
使用 HTML 格式显示文本
选择“设计”切换到设计视图。
在“插入”选项卡上,选择“文本框”,然后在 Design Surface 上,选择并拖动以便在表的下方创建一个宽度为 4 英寸、高度为 3 英寸的文本框。
复制此文本并将其粘贴到文本框中:
<h4>Limitations of cascading style sheet attributes</h4> <p>Only a basic set of <b>cascading style sheet (CSS)</b> attributes are defined:</p> <ul><li> text-align, text-indent </li><li> font-family, font-size </li><li> color </li><li> padding, padding-bottom, padding-top, padding-right, padding-left </li><li> font-weight </li></ul>
拖动文本框的下边缘,使其适合于所有文本。 注意在你拖动时设计图面会变大。
选择文本框中的所有文本。
右键单击所有选定文本,然后选择“文本属性”。
这是文本的属性,而非文本框的属性,因此,在一个文本框中,你可以混用纯文本和使用 HTML 标记作为样式的文本。
在“常规”选项卡上的“标记类型”下,选择“HTML - 将 HTML 标记解释为样式”。
选择“确定”。
选择“运行”(!) 以预览报表。
文本框中的文本将显示为标题、段落和带项目符号的列表。
保存报表
您可以将报表保存到报表服务器、SharePoint 库或本地计算机。
在本教程中,将报表保存到报表服务器。 如果你没有对 Report Server 的访问权限,则可以保存到你的计算机。
将报表保存到 Report Server
从“报表生成器”按钮,选择“另存为”。
选择“最近使用的站点和服务器”。
选择或输入你拥有保存报表权限的 Report Server 的名称。
此时将显示“正在连接到报表服务器”消息。 当连接完成时,您将看到报表服务器管理员指定为报表默认位置的报表文件夹的内容。
在“名称”中,用选择的名称替换默认名称。
选择“保存”。
报表即已保存至报表服务器。 你连接的 Report Server 的名称将显示在窗口底部的状态栏中。
将报表保存到计算机上
从“报表生成器”按钮,选择“另存为”。
选择“桌面”、“我的文档”或“我的电脑”,然后浏览到要将报表保存到的文件夹。
在“名称”中,用选择的名称替换默认名称。
选择“保存”。