Reporting Service Tips 101(#4) - 使用RS实现报表的自动生成以及在程序中调用RS

    一般来说,我们把报表分为两类,一类是ad-hoc的报表,用于实时查询,客户可以输入特定的参数,得到他们感兴趣的报表,还有一类是scheduling的报表,用于自动生成,一般包括daliy,monthly,quarterly和yearly的报表,这种定制类的报表,可以在指定的时间,生成到指定的目录,他们生成的内容也会提前定制,参数不可更改。一般在报表的需求定义中,客户都会要求报表能够做到自动生成,这也就是我们所说的第二类报表,有时候,客户还会要求能够在自动生成的同时,实现自动打印。下面我们来谈谈如何实现报表的自动生成以及自动打印。

       首先来谈谈解决方案,由于RS提供web service式的调用,因此我们可以写一个remoting service或者windows service或者仅仅是一段程序,然后由job之类的调用,来实现报表的自动。在程序中,我们调用RS,来实现报表的生成。需要生成的报表列表,报表的参数(时间参数),导出文件的格式,我们定义在数据库里,解决方案简单的用图表来表现如下:

image

下面我们来谈具体的步骤

第一步:创建项目,添加web引用(C#项目),URL为http://localhost/ReportServer/ReportService.asmx

clip_image004

第二步,取参数以及定义其他的参数。

参数表的内容为:

ReportName PeriodType PromptName1 Prompttype1 Format

SupplierD D date datetime PDF

SupplierM M date datetime PDF

SupplierQ Q date datetime PDF

SupplierY Y date datetime PDF

ReportName为报表的名称,PeriodType为报表的类别,分别是日报,月报,季度报和年报,PromptName1和Prompttype1为参数的名字和类别,如果有需要,可以加更多的参数。Format为报表导出的格式。(为方便描述,下述代码中的参数都写死,实际操作中,会从数据库或者从注册表中读取。)

定义参数的代码为:

ParameterValue[] parameters = null;

parameters[1] = new ParameterValue();

parameters[1].Name = “date”;

parameters[1].Value =”01/01/2008”;

 

定义其他参数:

string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";

string encoding;

string mimeType;

Warning[] warnings = null;

string[] streamIDs = null;

DataSourceCredentials[] credentials = null;

ParameterValue[] used = null;

 

第三步,生成报表并得到报表的二进制流

ReportingService rep = new ReportingService();

rep.Credentials = rep.Credentials = System.Net.CredentialCache.DefaultCredentials;

byte[] ss = null;

得到报表:

ss = rep.Render(“SupplierD” , “PDF”, null, devInfo, parameters, credentials, null, out encoding, out mimeType, out used, out warnings, out streamIDs);

使用的方法是RS提供的

Render ( Report As string ,  Format As string ,  HistoryID As string ,  DeviceInfo As string ,  Parameters As ArrayOfParameterValue ,  Credentials As ArrayOfDataSourceCredentials ,  ShowHideToggle As string ) As base64Binary

 

第四步:将生成的报表保存到服务器

FileStream repFile = new FileStream(@“D:\Report\ SupplierD”, FileMode.Create);

repFile.Write(ss, 0, ss.Length);

repFile.Close();

 

第五步:自动打印生成的报表

System.Diagnostics.ProcessStartInfo startInfo = new ProcessStartInfo();

startInfo.Arguments = "/p /h " + @“D:\Report\ SupplierD”;

startInfo.FileName = @"C:\Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe"; ; //Replace this with the path of the Acrobat Reader executable file, i.e. AcroRd32.exe

startInfo.UseShellExecute = true;

System.Diagnostics.Process process = Process.Start(startInfo);

process.WaitForExit(10000);

if (process.HasExited == false)

{

process.Kill();

}

       这里的方法其实是打印已经生成的PDF文件,所以要求该机器必须装有Acrobat Reader。不选用直接打印二进制数据流,是因为这种打印方法无法设置横向打印(Landscape)或者是竖向打印(Portrait),而报表肯定是有横向有竖向的。不选用打印生成html文件的方式,是因为RS不支持导出HTML,只支持MHTML,而MHTML转成HTML太麻烦。

       最后,有几点需要注意的是,RS导出的报表必须是已经发布到Report server上的,调用render方法时,第一个参数Report As string必须和reportserver上的路径一致,比如报表的发布路径是Report/SupplierD,那么这个参数的值为”Report/SupplierD”。还有一个要注意的是如果制作报表时,参数设置成下拉框式(如下图),那么传入的值必须是这些值其中之一或者之几(看是否是Multi-value)。

clip_image006


文章来源:http://blog.softexchange.cn/uestc95/archive/2008/07/12/reporting-service-tips-1014---syrssxbbdzdscyjzcxzdyrs.aspx

posted on 2008-07-12 23:44  Uestc95  阅读(1477)  评论(2)    收藏  举报

导航