Asp.net Report动态生成

时间:2023-03-10 03:19:34
Asp.net Report动态生成

rdlc报表实质上是一个xml文件,如果要实现动态报表,就需要动态生成rdlc文件,实质上就是读写xml文件:

  protected XmlDocument GenerationAddReportColumn(IList<ReportColumn> columnList,
string fromRdlcPath, string toRdlcPath)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(fromRdlcPath)); XmlNodeList fileds = xmlDoc.GetElementsByTagName("Fields");
XmlNodeList tablixColumns = xmlDoc.GetElementsByTagName("TablixColumns");
XmlNodeList tablixMembers = xmlDoc.GetElementsByTagName("TablixColumnHierarchy");
XmlNodeList tablixRows = xmlDoc.GetElementsByTagName("TablixRows"); foreach (var item in columnList)
{
//添加Field节点
XmlNode filedNode = fileds.Item().FirstChild.CloneNode(true);
if (!fileds.Item().ChildNodes.Cast<XmlNode>().Any(c=>c.Attributes["Name"].Value==item.Field))
{
filedNode.Attributes["Name"].Value = item.Field;
filedNode.FirstChild.InnerText = item.Field;
filedNode.ChildNodes[].InnerText = string.IsNullOrWhiteSpace(item.FieldType) ? "System.String" : item.FieldType;
fileds.Item().AppendChild(filedNode);
} //添加TablixColumn
XmlNode tablixColumn = tablixColumns.Item().FirstChild;
XmlNode newtablixColumn = tablixColumn.CloneNode(true);
newtablixColumn.FirstChild.InnerText = item.Width;
tablixColumns.Item().AppendChild(newtablixColumn); //TablixMember
XmlNode tablixMember = tablixMembers.Item().FirstChild.FirstChild;
XmlNode newTablixMember = tablixMember.CloneNode(true);
tablixMembers.Item().FirstChild.AppendChild(newTablixMember); var tablixRowsRowCells1 = tablixRows.Item().FirstChild.ChildNodes[];
XmlNode tablixRowCell1 = tablixRowsRowCells1.FirstChild;
XmlNode newtablixRowCell1 = tablixRowCell1.CloneNode(true);
var textBox1 = newtablixRowCell1.FirstChild.ChildNodes[];
textBox1.Attributes["Name"].Value = "Textbox_"+item.Field; var paragraphs = textBox1.ChildNodes.Cast<XmlNode>().Where(c => c.Name == "Paragraphs").FirstOrDefault();
paragraphs.FirstChild.FirstChild.FirstChild.FirstChild.InnerText = item.ColumnName;
var defaultName1 = textBox1.ChildNodes.Cast<XmlNode>().Where(c => c.Name == "rd:DefaultName").FirstOrDefault().InnerText = "Textbox_" + item.Field; tablixRowsRowCells1.AppendChild(newtablixRowCell1); var tablixRowsRowCells2 = tablixRows.Item().ChildNodes[].ChildNodes[];
XmlNode tablixRowCell2 = tablixRowsRowCells2.FirstChild;
XmlNode newtablixRowCell2 = tablixRowCell2.CloneNode(true);
var textBox2 = newtablixRowCell2.FirstChild.ChildNodes[];
textBox2.Attributes["Name"].Value = item.Field; var paragraphs2 = textBox2.ChildNodes.Cast<XmlNode>().Where(c => c.Name == "Paragraphs").FirstOrDefault();
paragraphs2.FirstChild.FirstChild.FirstChild.FirstChild.InnerText = "=Fields!" + item.Field + ".Value";
var defaultName2 = textBox2.ChildNodes.Cast<XmlNode>().Where(c => c.Name == "rd:DefaultName").FirstOrDefault().InnerText = item.Field; tablixRowsRowCells2.AppendChild(newtablixRowCell2); } xmlDoc.Save(Server.MapPath(toRdlcPath));
return xmlDoc; }
    /// <summary>
/// 报表列描述
/// </summary>
public class ReportColumn
{
public string Field { get; set; } public string ColumnName { get; set; } public string Width { get; set; } public string FieldType { get; set; }
}