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(0).FirstChild.CloneNode(true);
                if (!fileds.Item(0).ChildNodes.Cast<XmlNode>().Any(c=>c.Attributes["Name"].Value==item.Field))
                {
                    filedNode.Attributes["Name"].Value = item.Field;
                    filedNode.FirstChild.InnerText = item.Field;
                    filedNode.ChildNodes[1].InnerText = string.IsNullOrWhiteSpace(item.FieldType) ? "System.String" : item.FieldType;
                    fileds.Item(0).AppendChild(filedNode);
                }

                //添加TablixColumn
                XmlNode tablixColumn = tablixColumns.Item(0).FirstChild;
                XmlNode newtablixColumn = tablixColumn.CloneNode(true);
                newtablixColumn.FirstChild.InnerText = item.Width;
                tablixColumns.Item(0).AppendChild(newtablixColumn);

                //TablixMember
                XmlNode tablixMember = tablixMembers.Item(0).FirstChild.FirstChild;
                XmlNode newTablixMember = tablixMember.CloneNode(true);
                tablixMembers.Item(0).FirstChild.AppendChild(newTablixMember);

                var tablixRowsRowCells1 = tablixRows.Item(0).FirstChild.ChildNodes[1];
                XmlNode tablixRowCell1 = tablixRowsRowCells1.FirstChild;
                XmlNode newtablixRowCell1 = tablixRowCell1.CloneNode(true);
                var textBox1 = newtablixRowCell1.FirstChild.ChildNodes[0];
                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(0).ChildNodes[1].ChildNodes[1];
                XmlNode tablixRowCell2 = tablixRowsRowCells2.FirstChild;
                XmlNode newtablixRowCell2 = tablixRowCell2.CloneNode(true);
                var textBox2 = newtablixRowCell2.FirstChild.ChildNodes[0];
                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; }
    }

 

posted @ 2019-05-29 10:09 _York 阅读(...) 评论(...) 编辑 收藏