摘要: 一般来说,我们把报表分为两类,一类是ad-hoc的报表,用于实时查询,客户可以输入特定的参数,得到他们感兴趣的报表,还有一类是scheduling的报表,用于自动生成,一般包括daliy,monthly,quarterly和yearly的报表,这种定制类的报表,可以在指定的时间,生成到指定的目录,他们生成的内容也会提前定制,参数不可更改。一般在报表的需求定义中,客户都会要求报表能够做到自动生成,这也就是我们所说的第二类报表,有时候,客户还会要求能够在自动生成的同时,实现自动打印。下面我们来谈谈如何实现报表的自动生成以及自动打印。
首先来谈谈解决方案,由于RS提供web service式的调用,因此我们可以写一个remoting service或者windows service或者仅仅是一段程序,然后由job之类的调用,来实现报表的自动。在程序中,我们调用RS,来实现报表的生成。需要生成的报表列表,报表的参数(时间参数),导出文件的格式,我们定义在数据库里,解决方案简单的用图表来表现如下:
下面我们来谈具体的步骤
第一步:创建项目,添加web引用(C#项目),URL为http://localhost/ReportServer/ReportService.asmx
第二步,取参数以及定义其他的参数。
参数表的内容为:
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......[
阅读全文]