ASP.NET中动态控制RDLC报表.docx

上传人:rrsccc 文档编号:9186546 上传时间:2021-02-06 格式:DOCX 页数:9 大小:17.01KB
返回 下载 相关 举报
ASP.NET中动态控制RDLC报表.docx_第1页
第1页 / 共9页
ASP.NET中动态控制RDLC报表.docx_第2页
第2页 / 共9页
ASP.NET中动态控制RDLC报表.docx_第3页
第3页 / 共9页
ASP.NET中动态控制RDLC报表.docx_第4页
第4页 / 共9页
ASP.NET中动态控制RDLC报表.docx_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《ASP.NET中动态控制RDLC报表.docx》由会员分享,可在线阅读,更多相关《ASP.NET中动态控制RDLC报表.docx(9页珍藏版)》请在三一文库上搜索。

1、北大青鸟中关村ASP.NET中动态控制RDLC报表在程序中,可以选择使用水晶报表,功能确实强大。但是web版的水晶报表好像存在版权的问题。如果所作报表不是复杂的一塌糊涂的话,可以使用微软自带的Rdlc报表。Rdlc优点:1:Rdlc报表设计简单2:结果存成xml,易于控制3:导出格式作的很不错这里所说的动态控制报表所指的是:在一些时候,制作了报表之后希望在运行中可以动态的做一些小修改,比如说列的位置,用户控制显示那些列等等。控制方法,尝试了这么几种:1:控制微软提供的报表对象的属性;2:报表全部自动生成3:修改报表源文件,然后加载。控制微软提供的报表对象的属性:基于这个功能需求,一开始我想到的

2、方法是通过控制微软提供的这些报表对象的属性来实现。因为这种方法最人道了。但是事与愿违,微软的ReportViewer对象是用来显示Report的,自然不行;我使用的report是自己设计的,localReport,找到Report对象,里面方法有这个几个:report.GetDefaultPageSettings();report.GetDocumentMap()等,第一个是获取打印纸张德设置,第二个是获取doc文档(但是始终出错),都是只读属性;所以,第一种尝试失败。第二种方法就是报表全部自动生成。可以找到一个完整的例子,在这里:http:/ this.reportViewer1.Reset

3、();this.reportViewer1.LocalReport.LoadReportDefinition(m_rdl);this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource(MyData, m_dataSet.Tables0);this.reportViewer1.RefreshReport();这是自动生成xml的代码:private MemoryStream GenerateRdl(Liststring allFields, Liststring selectedFields)MemoryStream m

4、s = new MemoryStream();RdlGenerator gen = new RdlGenerator();gen.AllFields = allFields;gen.SelectedFields = selectedFields;gen.WriteXml(ms);ms.Position = 0;return ms;这是完全ReportDefinition的一部分定义:namespace Rdl using System.Xml.Serialization;/*/ remarks/System.CodeDom.Compiler.GeneratedCodeAttribute(xsd

5、, 2.0.50727.42)System.SerializableAttribute()System.Diagnostics.DebuggerStepThroughAttribute()System.ComponentModel.DesignerCategoryAttribute(code)System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)System.Xml.Serialization.XmlRootAttribute(Namespace=_http:/ IsNullable=false)public partial

6、class Report private object itemsField;但是几经考虑之后,这个方案也不让人满意,原因是:所有的报表对象都得自己生成,一下子回到了解放前,没有可视化工具的设计既繁琐又复杂。特别是如果设计几个line,然后再来上几个分组的话,工作量巨大。于是乎尝试第三种方法:ReportVivwer加载报表前在内存中修改报表源文件。这个方法比较狠,其实可以解决很多问题,包括设计自定义的打印纸张等(这里有另外一种设置打印纸张的方法http:/ MemoryStream GenerateRdlc()XmlDocument sourceDoc = new XmlDocument()

7、;string path = AppDomain.CurrentDomain.BaseDirectory + Test/OrderList.rdlc;sourceDoc.Load(path);Hashtable reportColumns = GetReportColumns(sourceDoc.LastChild);/just removefor (int i = 0; i reportColumns.Count; i+)if (!FindReportCoulmns(reportColumns.ToString()RemoveColumnFromRdlc(sourceDoc.LastChil

8、d, i);MemoryStream ms = new MemoryStream();XmlSerializer serializer = new XmlSerializer(typeof(XmlDocument);serializer.Serialize(ms, sourceDoc);ms.Position = 0;return ms;至于如何GetReportColumns和RemoveColumnFromRdlc,那就很简单了,就是一个操作xml对象的过程。比方说:private Hashtable GetReportColumns(XmlNode root)Hashtable cols

9、 = new Hashtable();/XmlNamespaceManager s=new XmlNamespaceManager(XmlNode cells = FindChildNode(root,Body/ReportItems/Table/Header/TableRows/TableRow/TableCells);for (int i = 0; i cells.ChildNodes.Count; i+)XmlNode cell =FindChildNode( cells.ChildNodes,ReportItems/Textbox/DataElementName);cols= cell

10、.InnerText;return cols;这是使用这一段的代码:this.ReportViewer1.LocalReport.LoadReportDefinition(this.Report.GenerateRdlc();this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource(DataSet1, result.Tables0);this.ReportViewer1.LocalReport.Refresh();这个方法终于成功了。附:rdlc文件的xml一段结构xml结构1?xml version=1.0 enc

11、oding=utf-8?2Report xmlns=http:/ xmlns:rd=http:/ DataSources4 DataSource Name=ConnectionString5 ConnectionProperties6 ConnectString /7 DataProviderSQL/DataProvider8 /ConnectionProperties9 rd:DataSourceID073016a7-6cb0-4e06-a6fd-f5882a039188/rd:DataSourceID10 /DataSource11 /DataSources12 BottomMargin2.5cm/BottomMargin13 RightMargin2.5cm/RightMargin14 PageWidth21cm/PageWidth15 rd:DrawGridtrue/rd:DrawGrid16 InteractiveWidth21cm/InteractiveWidth17 rd:GridSpacing0.25cm/rd:GridSpacing18 rd:SnapToGridtrue/rd:SnapToGrid19 Body20 ColumnSpacing1cm/ColumnSpacing21 ReportItems22 Chart Name=chart1

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1