silverlight Telerik.ReportViewer 实现打印

来源:转载

1.首先创建一个silverlight项目  如图


2.silverlight 工程需要引用三个Telerik的dll


Telerik的ReportViewer  控件就在Telerik.ReportViewer.Silverlight 下面。 另外两个也需要引用 不然运行时候会报错。

3.创建一个ChildWindow 子窗体

<controls:ChildWindow x:Class="ReportViewer.ChildWindow1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" xmlns:silverlight="clr-namespace:Telerik.ReportViewer.Silverlight;assembly=Telerik.ReportViewer.Silverlight"> <Grid x:Name="LayoutRoot"> <silverlight:ReportViewer x:Name="reportViewer" Width="850"
<span style="white-space:pre"></span> RenderBegin="ReportViewer1_RenderBegin" Report="ReportViewer.Web.Demo, ReportViewer.Web" UseNativePrinting="False" /> </Grid></controls:ChildWindow>
Report="<span style="font-family: Arial, Helvetica, sans-serif;">Report报表所在的位置以及报表名称</span><span style="font-family: Arial, Helvetica, sans-serif;">,</span><span style="font-family: Arial, Helvetica, sans-serif;">Report报表工程生成后dll名称</span><span style="font-family: Arial, Helvetica, sans-serif;">"</span>
</pre></p><pre name="code" class="csharp">UseNativePrinting 是否启用本地打印
RenderBegin 这个方法中向后台<span style="font-family: Arial, Helvetica, sans-serif;">Report 报表传递参数</span>
4.ChildWindow 后台代码
namespace ReportViewer { public partial class ChildWindow1 { public <span style="font-family: Arial, Helvetica, sans-serif;">ChildWindow1</span><span style="font-family: Arial, Helvetica, sans-serif;">() {</span> InitializeComponent(); this.ReportViewer.ReportServiceUri = DcsUtils.MakeServerUri("ReportService.svc");//Report 服务 } private void ReportViewer1_RenderBegin(object sender, RenderBeginEventArgs args) { args.ParameterValues["Id"] =1;//参数传递 }
<span style="white-space:pre"></span>//获取使用服务的绝对路径
<span style="white-space:pre"></span> public static Uri MakeServerUri(string relativePath) { if(string.IsNullOrEmpty(relativePath)) return null; if(Application.Current.Host.Source == null) throw new InvalidOperationException(); var uri = Application.Current.Host.Source.OriginalString; var index = uri.IndexOf("ClientBin/", StringComparison.InvariantCultureIgnoreCase); if(index > -1) uri = uri.Remove(index); else { var parts = uri.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); if(parts.Length > 0) { index = uri.IndexOf(parts[parts.Length - 1], StringComparison.InvariantCultureIgnoreCase); uri = uri.Remove(index); } } return new Uri(string.Concat(uri, relativePath.StartsWith("/") ? relativePath.Substring(1) : relativePath), UriKind.Absolute); } }}

5.重新新建一个普通类库或者直接在 web端工程中添加Report 如图
需要引用 

右边工具箱Telerik Reporting 。使用里面控件拖放 实现想要的界面就行了。数据源我使用的的是ObjectDataSource 直接拖进来就行了。
然后设置 ObjectDataSource 的数据源DataSouce  选择你的服务端查询数据的方法DataMember 查询方法名称Parameters 参数6  指定参数   首先Report 接受由前端silverlight 传入的参数  ReportParameters 中设置 设置传入的名称 Name 。和类型Type 
7选择Report 中的列表 设置列表的DataSouce 为 ObjectDataSource  并且ObjectDataSource 和   Report 中的 Parameters 一致。基本弄好了。 然后配置   Report 的配置文件
<configuration> <system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" > <serviceActivations> <add relativeAddress="ReportService.svc" service="Telerik.Reporting.Service.ReportService, Telerik.Reporting.Service, Version=7.1.13.802, Culture=neutral, PublicKeyToken=A9D7983DFCC261BE" /> </serviceActivations> </serviceHostingEnvironment> <bindings/> <services> <service name="Telerik.Reporting.Service.ReportService" behaviorConfiguration="ReportServiceBehavior"> <endpoint address="" binding="basicHttpBinding" contract="Telerik.Reporting.Service.IReportService"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="resources" binding="webHttpBinding" behaviorConfiguration="WebBehavior" contract="Telerik.Reporting.Service.IResourceService"/> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="ReportServiceBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="WebBehavior"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel></configuration>
记录今天笔记。  

分享给朋友:
您可能感兴趣的文章:
随机阅读: