教程:设置文本格式(报表生成器)
在本教程中,您可以练习以不同方式设置文本格式。 在设置了具有数据源和数据集的空白报表后,您可以选择要完成的步骤。
下图显示与您将创建的报表类似的报表。
在一个步骤中,您可以故意犯某个错误,以便可以了解错误的成因。 然后,您可以纠正该错误以便实现预期效果。
在本教程中创建的报表的增强版本作为示例SQL Server 2014 Report Builder 报表提供。 有关下载此示例报表和其他报表的详细信息,请参阅Report Builder示例报表。
学习内容
设置报表
选取和选择
完成本教程的估计时间:20 分钟。
要求
有关要求的详细信息,请参阅教程先决条件(报表生成器)。
创建具有数据源和数据集的一个空白报表
创建空白报表
单击“开始”,指向“程序”,指向“Microsoft SQL Server 2014Report Builder”,然后单击“Report Builder”。
注意
此时应显示 “入门” 对话框。 如果未显示该对话框,则从“报表生成器”按钮,单击 “新建”。
在 “入门” 对话框的左窗格中,确保已选择 “新建报表” 。
在右窗格中,单击 “空白报表” 。
创建数据源
在“报表数据”窗格中,单击“ 新建”,然后单击“ 数据源”。
在“名称”框中键入:“TextDataSource”
单击 “使用我的报表中嵌入的连接” 。
请确认连接类型为 Microsoft SQL Server,然后在“连接字符串”框中键入:Data Source = <servername>
注意
表达式 <servername>(例如 Report001)指定安装了 SQL Server 数据库引擎实例的计算机。 本教程不需要特定数据;它只需要连接到 SQL Server 2014 数据库。 如果你已经具有在“数据源连接”下列出的某一数据源连接,则可以选择该连接并且转到下一过程“创建数据集”。有关详细信息,请参阅获取数据连接的备选方法(报表生成器)。
单击 “确定” 。
创建数据集
在“报表数据”窗格中,单击“ 新建”,然后单击“ 数据集”。
确保数据源为 TextDataSource。
在“名称”框中键入:“TextDataset”。
确保已选择 “文本” 查询类型,然后单击 “查询设计器” 。
单击 “编辑为文本” 。
将以下查询粘贴到查询窗格中:
SELECT CAST('2009-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, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-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, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-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 Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2009-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, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-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, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-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 Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2009-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, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-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, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-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 Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2009-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, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-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, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-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 Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2009-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, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-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, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-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 Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2009-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, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-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, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-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 Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2009-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, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-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, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-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 Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2009-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, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-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, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-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 Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2009-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, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-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, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-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 Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL UNION SELECT CAST('2009-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, 'Installing Report Builder' as LinkText, 'https://go.microsoft.com/fwlink/?LinkId=154882' AS URL UNION SELECT CAST('2009-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, 'Getting Started with Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=160556' AS URL UNION SELECT CAST('2009-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 Report Builder' as Link, 'https://go.microsoft.com/fwlink/?LinkId=165064' AS URL
单击“运行” ( ! ) 以运行查询。
查询结果将是可用于在您的报表中显示的数据。
单击“确定”。
向报表设计图面添加字段
如果您希望来自您的数据集的字段出现在报表中,则第一感可能是要将其直接拖到设计图面。 本练习指出为什么无法这样做以及相应替代步骤。
向报表添加字段(获得错误结果)
将 FullName 字段从“报表数据”窗格中拖到设计图面中。
Report Builder创建一个包含表达式的文本框,表示为<Expr>.
单击 “运行” 。
请注意,只有一条记录 ,Fernando Ross,这是查询中的第一条记录。 该字段并不重复以便显示该字段中的其他记录。
单击 “设计” 返回设计视图。
在文本框中选择表达式 <Expr> 。
在“属性”窗格中,对于“值”属性,将看到以下内容(如果没有看到“属性”窗格,则在“视图”选项卡上,选中“属性”):
=First(Fields!FullName.Value, "TextDataSet")
First
函数设计为只检索字段中的第一个值,这就是您所看到的内容。将该字段直接拖到设计图面上创建了一个文本框。 文本框本身并非数据区域,因此它们不显示来自报表数据集的数据。 数据区域(例如表、矩阵和列表)中的文本框显示数据。
选择文本框(如果您已经选择了表达式,则按下 Esc 以便选择该文本框),然后按 Delete 键。
向报表添加字段(获得正确结果)
在功能区的“插入”选项卡的“数据区域”区域中,单击“列表” 。 单击设计图面,然后拖动以便创建一个框,该框大约两英寸宽、一英寸高。
将 FullName 字段从“报表数据”窗格中拖到列表框中。
此时,报表生成器将创建一个文本框,表达式
[FullName]
将位于该文本框中。单击 “运行” 。
请注意,此时该框将重复以便显示查询中的所有记录。
单击 “设计” 返回设计视图。
在文本框中选择该表达式。
在“属性”窗格中,对于“值”属性,将看到以下内容:
=Fields!FullName.Value
通过将文本框拖到列表数据区域,将显示处于数据集中的数据。
选择列表框并按 Delete 键。
向报表设计图面添加表
创建此表,以便有一个放置超链接和旋转文本的位置。
将表添加到报表中
在“ 插入 ”菜单上,单击“ 表”,然后单击“ 表向导”。
在“新建表或矩阵”向导的“ 选择数据集 ”页上,单击“ 选择此报表或共享数据集中的现有数据集”,单击“ 在此报表) 中的 TextDataset (”,然后单击“ 下一步”。
在 “排列字段 ”页上,将 “区域”、“ 链接文本”和“ 产品 ”字段拖动到 “行”组,将“ 销售额 ”字段拖到 “值”,然后单击“ 下一步”。
在“选择布局”页上,清除“展开/折叠组检查框,以查看整个表,然后单击”下一步”。
在 “选择样式 ”页上,单击“ 石板”,然后单击“ 完成”。
拖动该表以便该表位于标题块之下。
单击 “运行” 。
该表看起来没问题,但具有两个总计行。 LinkText 字段不需要 Total 行。
单击 “设计” 返回设计视图。
右键单击包含 的
[LinkText]
文本框,然后单击“ 拆分单元格”。选择单元格下方的
[LinkText]
空单元格,然后按住 Shift 键并选择右侧的两个单元格:“产品”列中的“总计”单元格和“销售额”列中的[Sum(Sales)]
单元格。选中这三个单元格后,右键单击其中一个单元格,然后单击“ 删除行”。
单击 “运行” 。
向报表添加超链接
在本节中,您将向前一节中的表中的文本添加超链接。
向报表添加超链接
单击 “设计” 返回设计视图。
右键单击包含
[LinkText]
的单元格,然后单击“文本框属性”。在 “文本框属性 ”框中,单击“ 操作”。
单击“ 转到 URL”。
在 “选择 URL” 框中,单击“ [URL]”,然后单击“ 确定”。
请注意,文本在外观上看不出任何区别。 您需要使其看起来像链接文本。
选择
[LinkText]
。在“开始”选项卡的“字体”部分中,单击“下划线”按钮,然后单击“颜色”按钮旁边的下拉箭头,然后单击“蓝色”。
单击 “运行” 。
文本现在看起来像链接了。
单击某一链接。 如果计算机已连接到 Internet,则浏览器将打开到报表生成器的帮助主题。
旋转报表中的文本
在本节中,您将旋转前一节的表中的某些文本。
旋转文本
单击 “设计” 返回设计视图。
单击包含 的单元格。
[Territory].
在“开始”选项卡上的“字体”部分中,单击“加粗”按钮。
如果“属性”窗格未打开,请在“视图”选项卡上选中“属性”复选框 。
在“属性”窗格中找到 WritingMode 属性。
注意
对“属性”窗格中的属性进行分类时,WritingMode 位于“本地化”类别中。 请确保您选择的是单元,而非文本。 WritingMode 是文本框的属性,而非文本的属性。
在列表框中,单击“ 旋转 270”。
在“段落”部分中的“开始”选项卡上,单击“中间”和“居中”按钮,以垂直和水平方式查找单元格中心的文本。
单击“运行”( ! )。
现在, [Territory]
单元中的文本将从单元的底部到顶部垂直放置。
使用 HTML 格式显示文本
以 HTML 格式显示文本
单击 “设计” 切换到设计视图。
在“插入”选项卡上,单击“文本框”,然后在设计图面上,单击并拖动以便在表的下方创建一个宽度为四英寸、高度为三英寸的文本框。
复制此文本并将其粘贴到文本框中:
<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 标记解释为样式”。
单击“确定”。
单击“运行” ( ! ) 以预览报表。
文本框中的文本将显示为标题、段落和带项目符号的列表。
设置货币格式
将数字设为货币格式
单击 “设计” 切换到设计视图。
单击包含
[Sum(Sales)]
的顶部表单元,按住 Shift 键,然后单击包含[Sum(Sales)]
的底部表单元。在“开始”选项卡上的“数字”组中,单击“货币”按钮 。
(可选) 在“ 开始 ”选项卡上的“ 数字 ”组中,单击“ 占位符样式 ”按钮,然后单击“ 示例值 ”以查看数字的格式。
(可选)在“开始”选项卡上的“数字”组中,单击两次“减少小数位数”按钮,显示不带美分的美元数字 。
单击“运行” ( ! ) 以预览报表。
报表现在将显示设置了格式的数据并且更易于阅读。
保存报表
您可以将报表保存到报表服务器、SharePoint 库或本地计算机。
在本教程中,将报表保存到报表服务器。 如果您没有对报表服务器的访问权限,则可以保存到您的计算机。
将报表保存到报表服务器
从 “报表生成器” 按钮,单击 “另存为” 。
单击 “最近使用的站点和服务器” 。
选择或键入您拥有保存报表权限的报表服务器的名称。
此时将显示“正在连接到报表服务器”消息。 当连接完成时,您将看到报表服务器管理员指定为报表默认位置的报表文件夹的内容。
在“名称”中,用选择的名称替换默认名称。
单击“ 保存”。
报表即已保存至报表服务器。 您连接的报表服务器的名称将显示在窗口底部的状态栏中。
将报表保存到计算机上
从 “报表生成器” 按钮,单击 “另存为” 。
单击 “桌面” 、 “我的文档” 或 “我的电脑” ,然后浏览到要将报表保存到的文件夹。
在“名称”中,用选择的名称替换默认名称。
单击“ 保存”。
后续步骤
Report Builder教程:创建自由格式报表 (Report Builder) 包含更多示例,可通过多种方式设置文本格式。