利用Microsoft.ReportingServices.RdlObjectModel.dll文件对rdl进行动态更改
Microsoft.ReportingServices.RdlObjectModel.dll
装完 ReportBuilder 2.0 后,可以在C:\Program Files\Microsoft SQL Server\Report Builder 2.0中找到对应的文件
C:\Program Files\Microsoft SQL Server\Report Builder 2.0中还有一些其它的dll,利用这些dll完可以实现一个web版方式的 RDL Report Designer
有了此dll,可以相对方便的对.rdl文件进行操作,需要注意的是
默认VS2010 或 ReportBuilder 2.0 操作后的定义为:
<Report xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner" xmlns="http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition">
仅用 RdlObjectModel.dll 处理完后的文件定义为:
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition">
少了
xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"
这也就意味这,rdl文件中的 <rd:> 的元素都将丢失 但不影响报表正常解析。
下面是工作中写的一些类作为参考
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Microsoft.ReportingServices.RdlObjectModel; namespace www.cnblogs.com.rock_chen..Report.RDL2008 { public static class ReportExtension { static ReportUtility reportUtility = new ReportUtility(); public static Tablix GetTablix(this Microsoft.ReportingServices.RdlObjectModel.Report report) { return reportUtility.FirstItem<Tablix>(report.Body.ReportItems); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Xml.Serialization; using System.IO; using Microsoft.ReportingServices.RdlObjectModel; using Microsoft.ReportingServices.RdlObjectModel.Serialization; using System.Collections; namespace www.cnblogs.com.rock_chen..Report.RDL2008 { public class ReportUtility { RdlSerializer serializer = new RdlSerializer(); public Microsoft.ReportingServices.RdlObjectModel.Report Load(string rdlPath) { Stream reader = new FileStream(rdlPath, FileMode.Open, FileAccess.Read); var result = serializer.Deserialize(reader); reader.Close(); return result; } public void Save(Microsoft.ReportingServices.RdlObjectModel.Report report, string rdlPath) { Stream reader = new FileStream(rdlPath, FileMode.Truncate); Save(report, reader); reader.Flush(); reader.Close(); } public void Save(Microsoft.ReportingServices.RdlObjectModel.Report report, Stream stream) { serializer.Serialize(stream, report); } public MemoryStream Save(Microsoft.ReportingServices.RdlObjectModel.Report report) { MemoryStream ms = new MemoryStream(); Save(report, ms); ms.Position = 0; return ms; } public T[] Items<T>(IList<ReportItem> items) { return items.OfType<T>().ToArray(); } public T FirstItem<T>(IList<ReportItem> items) { return Items<T>(items).FirstOrDefault(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Microsoft.ReportingServices.RdlObjectModel; using System.Text.RegularExpressions; namespace www.cnblogs.com.rock_chen..Report.RDL2008 { public static class TablixExtension { public static Tablix AddTablixColumn(this Tablix tablix, double width) { var column = new TablixColumn(); column.Width = new ReportSize(width); tablix.TablixBody.TablixColumns.Add(column); tablix.TablixColumnHierarchy.TablixMembers.Add(new TablixMember()); foreach (var row in tablix.TablixBody.TablixRows) { var cell = new TablixCell(); cell.CellContents = new CellContents(); var textBox = new Textbox(); textBox.Name = string.Format("textbox{0}", Guid.NewGuid().ToString("N")); cell.CellContents.ReportItem = textBox; row.TablixCells.Add(cell); } return tablix; } public static Tablix CopyColumn(this Tablix tablix, int index) { tablix.TablixBody.TablixColumns.Add((TablixColumn)tablix.TablixBody.TablixColumns[index].DeepClone()); tablix.TablixColumnHierarchy.TablixMembers.Add((TablixMember)tablix.TablixColumnHierarchy.TablixMembers[index].DeepClone()); foreach (var row in tablix.TablixBody.TablixRows) { var cell = (TablixCell)row.TablixCells[index].DeepClone(); var textBox = cell.CellContents.ReportItem as Textbox; if (textBox != null) { textBox.Name = string.Format("textbox{0}", Guid.NewGuid().ToString("N")); } row.TablixCells.Add(cell); } return tablix; } public static Tablix RemoveColumn(this Tablix tablix, int index) { tablix.TablixBody.TablixColumns.RemoveAt(index); tablix.TablixColumnHierarchy.TablixMembers.RemoveAt(index); foreach (var row in tablix.TablixBody.TablixRows) { row.TablixCells.RemoveAt(index); } return tablix; } /// <summary> /// 跟据字段名称获取索引号,没有找到,就异常 /// </summary> /// <param name="tablix"></param> /// <param name="columnName"></param> public static int GetColumnIndex(this Tablix tablix, string columnName) { var expression = string.Format("=Fields!{0}.Value", columnName); foreach (var row in tablix.TablixBody.TablixRows) { for (int index = 0; index < tablix.TablixBody.TablixColumns.Count; index++) { if (row.TablixCells[index].CellContents == null) { continue; } var textBox = row.TablixCells[index].CellContents.ReportItem as Textbox; if (textBox == null) { continue; } if (textBox.Paragraphs.Count != 1) { //邦定的字段的Paragraphs的count为1 continue; } if (!textBox.Paragraphs[0].TextRuns[0].Value.IsExpression) { //非表达式,不用处理 continue; } var value = textBox.Paragraphs[0].TextRuns[0].Value.Value; if (string.Equals(expression, value)) { return index; } } } throw new Shiji.Core.Services.ShijiException("没有找到列:{0}", columnName); } public static Tablix ReplaceExpression(this Tablix tablix, int index, string newFiledName) { var patten1 = @"(?<=(Fields|Parameters)\!)([^\.]+)"; var patten2 = @"(?<=Fields\!)([^\.]+)"; var oldFiledName = ""; List<TextRun> runList = new List<TextRun>(); foreach (var row in tablix.TablixBody.TablixRows) { var textBox = row.TablixCells[index].CellContents.ReportItem as Textbox; if (textBox == null) { continue; } if (textBox.Paragraphs.Count != 1) { //邦定的字段的Paragraphs的count为1 continue; } if (!textBox.Paragraphs[0].TextRuns[0].Value.IsExpression) { //非表达式,不用处理 continue; } var value = textBox.Paragraphs[0].TextRuns[0].Value.Value; if (Regex.IsMatch(value, patten1, RegexOptions.IgnoreCase)) { runList.Add(textBox.Paragraphs[0].TextRuns[0]); } if (string.IsNullOrEmpty(oldFiledName)) { var match = Regex.Match(value, patten2, RegexOptions.IgnoreCase); if (match != null) { oldFiledName = match.Value; } } } runList.ForEach(p => { var value = p.Value; value.Value = Regex.Replace(p.Value.Value, oldFiledName, newFiledName, RegexOptions.IgnoreCase); p.Value = value; }); return tablix; } public static double GetMaxWidth(this Tablix tablix) { var left = tablix.Left.ToCentimeters(); var pageWidht = ((Microsoft.ReportingServices.RdlObjectModel.Report)tablix.Parent.Parent).Width.ToCentimeters(); return pageWidht - left * 2; } public static Tablix AutoColumnWidth(this Tablix tablix) { return AutoColumnWidth(tablix, 0, tablix.TablixBody.TablixColumns.Count - 1); } public static Tablix AutoColumnWidth(this Tablix tablix, int startIndex, int endIndex) { var maxIndex = tablix.TablixBody.TablixColumns.Count - 1; startIndex = 0 > startIndex ? 0 : startIndex; endIndex = endIndex > maxIndex ? maxIndex : endIndex; if (startIndex > endIndex) { return tablix; } var allWidht = tablix.GetMaxWidth(); for (int i = 0; i < tablix.TablixBody.TablixColumns.Count; i++) { if (i < startIndex || i > endIndex) { allWidht -= tablix.TablixBody.TablixColumns[i].Width.ToCentimeters(); } } var avgWidht = allWidht / (endIndex - startIndex + 1); for (int i = startIndex; i <= endIndex; i++) { tablix.TablixBody.TablixColumns[i].Width = new ReportSize(avgWidht, SizeTypes.Cm); } return tablix; } } }
-----------------------------------
http://www.cnblogs.com/rock_chen/