Asp.Net中的Telerik报告-用程序应用过滤器
本文关键字:程序 应用 过滤器 报告 Net 中的 Telerik Asp | 更新日期: 2023-09-27 18:25:56
我的要求:通过c#编码(非设计)应用填充符,即过滤器工资超过7000。
我的项目中有一个类库和一个网页表单。我正在创建关于类库的报告,并使用web表单显示报告。
当我运行应用程序时,它总是显示未过滤的数据。我在查看器中获取筛选数据的方法。
代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Telerik.Reporting.Filter f1 = new Telerik.Reporting.Filter();
f1.Expression = "= Fields.Salary";
f1.Operator = Telerik.Reporting.FilterOperator.GreaterOrEqual;
f1.Value = "=7000";
EmpReport objEmpReport = new EmpReport(); objEmpReport.Filters.Add(f1);
TypeReportSource rptSource = new TypeReportSource(); rptSource.TypeName = typeof(EmpReport).AssemblyQualifiedName; this.ReportViewer1.ReportSource = rptSource;
}
}
工作代码:
// ...
using Telerik.Reporting;
using Telerik.Reporting.Processing;
// ...
void ExportToPDF(string reportToExport)
{
// all my reports are in trdx format - detect file type and use unpackage below for trdp files.
string currPath = HttpRuntime.AppDomainAppPath; // get the full path so deserialise works
reportToExport = currPath + @"Reports'" + reportToExport; // add folder and report name to path
UriReportSource uriReportSource = new UriReportSource { Uri = reportToExport }; // compressed to 1 line for brevity
Telerik.Reporting.Report myReport = DeserializeReport(uriReportSource); // extract report from xml format (trdx)
// Filters are client side (use params for server side) Here we work with the report object.
// set meaningful field name and values for your code, maybe even pass in as params to this function...
myReport.Filters.Add("UserId", FilterOperator.Equal , "1231");
var instanceReportSource = new Telerik.Reporting.InstanceReportSource(); // report source
instanceReportSource.ReportDocument = myReport; // Assigning Report object to the InstanceReportSource
// kinda optional, lots of examples just used null instead for deviceInfo
System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable(); // set any deviceInfo settings if necessary
ReportProcessor reportProcessor = new ReportProcessor(); // will do work
RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, deviceInfo); // GO!
if (!result.HasErrors)
{
this.Response.Clear();
this.Response.ContentType = result.MimeType;
this.Response.Cache.SetCacheability(HttpCacheability.Private);
this.Response.Expires = -1;
this.Response.Buffer = true;
this.Response.BinaryWrite(result.DocumentBytes);
this.Response.End();
}
else
{
Exception[] ex = result.Errors;
throw new Exception(ex[0].Message);
}
}
Telerik.Reporting.Report DeserializeReport(UriReportSource uriReportSource)
{
var settings = new System.Xml.XmlReaderSettings();
settings.IgnoreWhitespace = true;
using (var xmlReader = System.Xml.XmlReader.Create(uriReportSource.Uri, settings))
{
var xmlSerializer = new Telerik.Reporting.XmlSerialization.ReportXmlSerializer();
var report = (Telerik.Reporting.Report)xmlSerializer.Deserialize(xmlReader);
return report;
}
}
Telerik.Reporting.Report UnpackageReport(UriReportSource uriReportSource)
{
var reportPackager = new ReportPackager();
using (var sourceStream = System.IO.File.OpenRead(uriReportSource.Uri))
{
var report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream);
return report;
}
}