给reporting services加个条件型的格式 (轉)

http://blog.csdn.net/dujingjing1230/archive/2009/06/10/4258116.aspx 

大家用过SQL Server reporting services创建过报表的人肯定都想方设法去让一个表格或者是矩阵很美观的显示出来。不过,大多数情况下,你是一下子需要对所有的页面都得设置,而且是个静态的结果。 单元表格的列行的显示最终被设置为固定的,比如有背景颜色,字体属性,还有边框都给定义好。如果你需要一个统一格式的展示,那么这个无可非议,不过在当今社会由于信息的不断流动,你需要使某些数据模块能够展现出来比较特别,来吸引那些访问者。说的更智能点,你想要这些过程都自动进行,这样,当一些值遇到某些界限时就会自动吸引读者的注意。 所以你需要加一个条件型的格式,也就像Access和Excel这些n多年前就有的功能。

幸运的是reporting services允许你去以基础数据为准则去改变报表的显示。所以当一个销售报表的数据增加或者是减少,你可以让这些变化差异很大的数据以下面的方式吸引读者:

1.       改变某个数据的背景颜色。

2.       改变字体颜色和属性。

3.       添加或者是扩展数据的边框。

确切的说,你可以结合很多技术让你得报表给用户显示的时候不仅把数据现实出来,同时还显示了对这些数据的分析。

澄清一下,我并不是刻意去说这些东西是很强的功能,或者是reporting services一些没有被发现的东西吧。 相反的,我只是试图让你去知道一些增强你得报表显示的方法,以便能够在用reporting services来创建出自动的报表,也就是带分析。

基础的条件样式报表

不管怎么说,我们现在知道了我们的目的,下面我们来实践吧。

一个报表实例:

首先我们需要一些数据。我建议用adventureworks数据库来返回一些表示销售人员和销售图形的数据。我们会用他们来显示出来那些最好的或者是最差的销售人员。

下面这个存储过程是用来为我们的报表返回数据的。 如果你还没做这一步。我建议你下载adventureworks数据库并安装好,然后在它上面创建下面这个存储过程。

首先值得注意的一点事我使用T-sql来进行计算而不是使用reporting services来进行。这个只是个人的喜好,不过我可以肯定一点这样来更利于我们以条件的格式显示报表。

现在你需要去创建一个新的reporting service项目。然后创建一个新的数据源,它链接到adventureworks库。 最后添加一个新报表。值得注意的是使用pr_GetSalesData作为查询语句,并且存储过程作为命令格式。

然后切换到显示的tab,把那些字段都拖过去作为行来显示。

应用基本的条件格式。

现在让我们添加一些基本的条件格式。对于初学者,我们这样来说吧,我们需要标记出来那些今年销量小于去年的销售人员。那么你需要用到’sales increase’字段,我们要做到这种标记显示需要做以下几步:

  1. Click on the table cell for the detail row in the "Sales Increase" column.
  2. Display the properties window (pressing F4 will do this if it is not already visible).
  3. Click the popup box to the right of the "Color" attribute, and select "Expression". The "Edit Expression" dialog will be displayed.
  4. Enter the following code: =IIF(Fields!SalesIncrease.Value<0,"Red","Black"). You should see something like this:

  1. 点击OK.

现在你可以看到有两个人员显示为红色。

以上这些操作确实很简单而且很平常。下面我们会给你一些更精彩有趣的东西。如果你想看到那些销售额超过5000000的人员,然后让他们的数据显示为solid字体,那么你需要做以下几步:

  1. Click on the table cell for the detail row in the "Sales YTD" column.
  2. Display the properties window (pressing F4 will do this if it is not already visible).
  3. Click the popup box to the right of the "Border Style" attribute, and select "Expression". The "Edit Expression" dialog will be displayed.
  4. Enter the following code: =iif(Fields!SalesYTD.Value>5000000,"Solid","None").
  5. Click "OK"
  6. Preview the report.

如果你想看到那些奖金超过5000的销售人员的名字你需要做以下几步:

  1. Click on the table cell for the detail row in the "Person Name" column.
  2. Display the properties window (pressing F4 will do this if it is not already visible).
  3. Expand the "Font" attribute and click the popup box to the right of "FontWeight", then select "Expression". The "Edit Expression" dialog will be displayed.
  4. Enter the following code: =iif(Fields!Bonus.Value>5000,"Bold","Normal").
  5. Click "OK"
  6. Preview the report.

更加完美的条件式报表

现在让我们往更远的地方想想。 假如我们需要一些不同的背景色来显示不同性质的数据。比如不同阶段的数据显示不同的颜色。首先需要这个表格来说明一下显示的规则:

我自己对颜色并不是很敏感,没有什么天赋,所以我从来不能得到一个图形设计师的工作。但是这个不是

我们这边文章的重点,现在我们需要很好的想一下,有两件事情需要我们来做:

  1. 应用一系列的判定而不是简单的if else.
  2. 不是简单的去做个比较而是要显示出来图形的整个特征。

现在我们能够使用IIF功能来实现这些功能,但是比起来用这些复杂的且以错误的iif嵌套我们还是使用reporting services的switch函数来实现。但是这个只解决了第一个问题,那么第二个问题呢?因此我还是觉得使用t-sql来进行范围性的判断是个更好的主意,而不是使用reporting services.现在你需要添加下面这个select列到你的存储过程中。

现在会返回1-5几个数。那么我们还需要在dataset的字段中添加一个新的列方法如下:

  1. Click on the "Data" tab of your report.
  2. Click the "Edit Selected Dataset" ellipsis on the tab toolbar.
  3. The "Dataset" dialog will appear. Click the "Fields" tab.
  4. Scroll to the bottom of the list of fields and click in a blank row.
  5. Add the field name to both the "Field Name" and "Value" cells. You should end up with something like this:

现在你已经有个分析的那个标志,然后添加几个判定性的格式,这里用到reporting services的switch函数。

  1. Click on the table cell for the detail row in the "Commission" column.
  2. Display the properties window (pressing F4 will do this if it is not already visible).
  3. Click the popup box to the right of the "Background Color" attribute, and select "Expression". The "Edit Expression" dialog will be displayed.
  4. Enter the following code: =switch(Fields!CommissionFlag.Value=1,"Red",Fields!CommissionFlag.Value=2,"Yellow",Fields!CommissionFlag.Value=3,"LawnGreen",Fields!CommissionFlag.Value=4,"DarkGreen",Fields!CommissionFlag.Value=5,"Blue")
  5. Click "OK"
  6. Preview the report.

后面还有一个exception highlighting ,最后的存储过程显示如下:

而最后的报表显示如下;

原文地址:https://www.cnblogs.com/wonder223/p/2747406.html