狗尾草

导航

report services 报表开发和部署,集成到解决方案中 全解析

report services 报表开发和部署,集成到解决方案中全解析

---- 对于report services简介

---- 通过vs2003,来设计报表

---- 管理报表服务器上的报表(通过报表服务器)

---- 通过report services报表平台,利用编程接口,将报表功能集成到自定义的解决方案中

 

对于report services简介

Reporting Services是一个基于报表服务器的报表平台。可以创建,管理,来自不同数据的

 表格报表,图片报表,其他的格式的报表,并内置了多个呈现扩展插件(web,Execl,PDF….),提供API ,供程序员将报表功能添加到自己的解决方案中,显示自己想要的各种格式的报 表,

    这里面还要注意的是report services API,是通过web服务来实现的,在web服务提供的方法中,我们可以获得各种格式的表报(流的形式),报表的目录,设置数据源,添加新的报表功能。

 

   通过vs2003,来设计报表

  设计报表的目的就是生成一个报表定义语言 (RDL) 文件。

   vs2003中提供了一个设计报表的项目-----报表项目

 clip_image002.jpg

    文件---〉新建项目-----〉出现上面的选择页面  选择报表项目 点击“确定”生成一个报表项目  在解决方案资源管理器中包含两个文件夹 共享数据源 reports

 

  下面我们就考虑设计一个报表了,在设计报表前我们需要了解以下几个方面

    1 生成报表的数据源。

    2 报表的显示的形式

    3 报表其他的设置:参数,报表的页眉,页脚,显示页面的大小等等。

  下面以一个学校数据库生成学生的简历报表为例子生成学生的简历报表。

   第一步 :添加一个共享数据源

   右击 虚拟文件夹共享数据源 ,选择 “添加新的共享数据源”,显示下图:

 

  clip_image003.jpg

 

在这里,我们报表的数据的来源是 data source=(local);initial catalog=shool ; User ID=xiaoxu;

 PassWord =123

 

  第二步:页面设计

         1添加报表

         右击Reports虚拟文件架 ,选择 添加à添加新项。弹出报表项目选择业面

    addreport.JPG

                        报表项目选择页面

  选择 报表,在名称的文本框中填写Student_Resume” 点击打开按钮完成一个新的报表的添加,并转到报表的编辑页面

 

  注:报表的编辑页面分 数据,布局,预览 三个部分,其中各部分的功能分别是

  数据   用来创建数据集 生成报表的填充的数据。

  布局   通过工具栏中的相关的控件生成报表的现实页面,并填充相关的数据。

  预览   显示现在报表的生成后的样式 

      

 2页面布局

   打印页面  : 纸张长度页宽21cm页高 29.7cmA4纸的大小)

   其它的一些设置和office的设置一样

   reportbugu.jpg

                               页面设置对话框

  页面设计

 学生基本情况页面设计:

 

文本框

文本框

 

 

 

 

。。。。

。。。。

 

 

 

 

。。。。

 

 

 

。。。。

 

 

 

                    基本情况显示

简历情况设计

 

文本框

 

 

文本框

                      简历的显示

上面的两个显示的基本模块都是通过多行表头组合而成的(单独的文本框相组合文本框窜行

 

将上面基本情况显示简历的显示两部分通过矩形组合起来。
form_bujiu.JPG

 

                                 页面的基本布局

第三步设计数据集,并将数据添加到报表相对应的页面中

  报表的参数

在这个报表中我们设置一个参数@id 来做为学生选择的接口。

设置流程:报表-à报表参数-à报表参数设置页面  名称:id ;提示:学生的学号

          数据类型:整形---à  点击“确定“按钮,完成参数的添加

 

 

设计数据集

  数据集分两个部分

  一部分显示人员的基本的信息,一部分显示学生的简历情况

 显示学生的基本集的SQL  语句为下所示:

 select name as '姓名',

case sex when '1' then''  when '0' then '' else '' end as '性别' -- translate the sex to chinese

,birthday +char(10)+'('+cast(age as varchar(4))+')' as '出生年月'

,nation as '民族'

,native_place as '籍贯'

,health_static as '健康状况'

,degree as '学历'

,specialty as '专业'

,schoolname as '学校名称'

,address as'地址'

from student where id=@id                            ---@id  is param which need input

--  from user

 

下面是显示学生简历的SQL 语句

这里我们也是通过将要显示的相关学生简历数据组合成需要显示的字符串形式。

  declare @resume  varchar(2000)     --define the param to storage the information of student resume

  declare @period   varchar(100)     --define the param to storage the period information of every resume

  declare @content  varchar(500)     --define the param to storage the content information of every resume

  set @resume=char(10)               -- initialize the params

  set @period=''

  set @content=''

 

  declare resumecursor cursor for select Begin_time+'-'+End_time ,'工作地点:'+Work_place+',职位:'+tastName+','+hortation_punish from stu_resume

         where id0='1' order by Begin_time   --define the cursor

 

  open resumecursor

  fetch next from  resumecursor into @period,@content

  while @@FETCH_STATUS = 0

     begin

          set @resume=@resume+char(32)+char(32)+char(32)+char(32)

          set  @resume =@resume+@period      -- and the period information and the space before into resume

          if len(@period)>12

             begin

                 declare @index1   int

                 set @index1 =1

                 while @index1<7

                       begin

                           set @resume=@resume+char(32)

                           set @index1= @index1+1

                       end            

             end

          else

             begin

                 declare @index   int

                 set @index =1

                 while @index<9

                       begin

                           set @resume=@resume+char(32)

                           set @index= @index+1

                       end

              end       

 

         declare @cur_index int              --define and initialize the params  and need to deal with content

         declare @All_Length int

         declare @NewLineLength int

         set @cur_index=1

         set @All_Length=0

         set @NewLineLength=45

        

         while @cur_index<=len(@content)

              begin

                 if (ascii(substring(@content,@cur_index,1))<=128)

                    begin

                    set  @All_Length=@All_Length+1

                    end                  

                 else

                     begin

                     set @All_Length=@All_Length+2

                     end

                 if (@All_Length>@NewLineLength)

                     begin

                     set @resume=@resume+char(10) 

                     declare @index2   int

                     set @index2 =1

                     while @index2<24

                       begin

                           set @resume=@resume+char(32)

                           set @index2= @index2+1

                       end

                      set @resume=@resume+substring(@content,@cur_index,1)

                      set  @NewLineLength=@NewLineLength+45

                     end   

                 else

                     begin

                     set @resume=@resume+substring(@content,@cur_index,1)

                     end

 

                set  @cur_index=@cur_index+1

              end

 

 

           

           set @resume=@resume+char(10) 

          fetch next from  resumecursor into @period,@content

 

     end

 

   select ''+char(10)+char(10)+char(10)+char(10)+char(10)+char(10)+char(10)+'' as '简历标题', @resume as '简历'

   close resumecursor

   deallocate resumecursor

 

这段代码的效果为下图:

 

  jianli.JPG

第四步  发布报表到报表服务器上面

右击解决方案中的报表项目,选择属性,弹出报表属性页面

 

TargetServerURL 中填写目标报表服务器的地址

 shuxing.JPG

F5 运行.

 

 

 

管理报表服务器上的报表(通过报表服务器)

参照帮助文件。

 

通过report services报表平台,利用编程接口,将报表功能集成到自定义的解决方案中

 

添加报表服务

右击工程文件的属性,---〉添加web应用

http://localhost/ReportServer/ReportService.asmx

 

在这里面我们主要是通过一个下拉框来来绑定通过报表服务器上面的读取的报表目录

并根据下拉框的选择来导出Excel格式的报表

 


reportform.JPG
代码部分

 

 

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using WebApplication5.localhost;
namespace WebApplication5
{
    
/// <summary>
    
/// reportServer 的摘要说明。
    
/// </summary>

    public class reportServer : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.DropDownList Report_list;
        
protected System.Web.UI.WebControls.Button Bt_Export;
        
protected System.Web.UI.WebControls.Label Label1;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            if!this.IsPostBack)
            
{
            WebApplication5.localhost.ReportingService rs
=new ReportingService();
            rs.Credentials 
=System.Net.CredentialCache.DefaultCredentials ;
            CatalogItem[] catalogItems;
            catalogItems
=rs.ListChildren("/"true);
  
            
foreach(CatalogItem ci in catalogItems)    //读报表的目录
            {
                
if     (ci.Type ==ItemTypeEnum.Report)
                
{
                Report_list.Items.Add(
new ListItem(ci.Name ,ci.Path));
                }

                
            }


            }

        }


        
Web 窗体设计器生成的代码

        
private void Bt_Export_Click(object sender, System.EventArgs e)
        
{
            ReportingService rService 
= new ReportingService();  
            rService.Credentials 
= System.Net.CredentialCache.DefaultCredentials;
            
byte[] result;
      
//render arguments
            string ReportPath=Report_list.SelectedItem.Value;
            
string format ="Excel";
            
string historyID = null;
            
string devInfo=null;
            
// Prepare report parameter.
            ParameterValue[] parameters = new ParameterValue[1];
            parameters[
0= new ParameterValue();
            parameters[
0].Name = "id";
            parameters[
0].Value = "1";

            DataSourceCredentials[] credentials 
= null;
            
string showHideToggle = null;
            
string encoding;
            
string mimeType;
            Warning[] warnings 
= null;
            ParameterValue[] reportHistoryParameters 
= null;
            
string[] streamIDs = null;
            SessionHeader sh 
= new SessionHeader();
            rService.SessionHeaderValue 
= sh;
            
try
            
{
                result 
= rService.Render(ReportPath, format, historyID, devInfo, parameters, credentials, 
                    showHideToggle, 
out encoding, out mimeType, out reportHistoryParameters, out warnings,
                    
out streamIDs);                //读取报表excel格式
            }

            
catch             
            
{
                
throw new Exception("报表导出异常!"); 
            }

            HttpContext.Current.Response.ContentType 
= "application/vnd.ms-excel";  //发送给用户
            HttpContext.Current.Response.AppendHeader("Content-disposition","attachment;filename="+HttpContext.Current.Server.UrlEncode(Report_list.SelectedItem.Text)+".xls");
            HttpContext.Current.Response.BinaryWrite(result);
            HttpContext.Current.Response.End();

        
        }

    }

}



posted on 2006-04-21 09:25  狗尾草-大数据收割基  阅读(3900)  评论(0编辑  收藏  举报