动态RDLC报表(五)

动态RDLC报表类DynamicReport:数据表节点数据填充

       数据表部分是最麻烦的,可以添加多个数据表,可以设置多维表头,可以设置多字段的统计行,不同的列可以采用不同的样式。在传入数据表前需要先传入列样式、多维表头,数据表和统计列是同一时间传入的,页眉和页脚则需要在数据表后传入。

       在这里最多可以设置三行表头,从上到下依次是hearderTop、hearderMerge和数据表中的列。表头的样式包含如下几部分:

        public string ColoumName { get; set; }     //列名
        public float ColoumWidth { get; set; }       //列宽
        public TextAlign TextAlign { get; set; }      //对齐
        public ConsoleColor ConsoleColor { get; set; }    //颜色
        public Boolean IsShortDate { get; set; }       //显示短日期格式

       多维表头的数据表结构是:

            DataTable hearderMerge = new DataTable();
            hearderMerge.Columns.Add("CellNumber", Type.GetType("System.Int32"));
            hearderMerge.Columns.Add("CellValue", Type.GetType("System.String"));


       CellNumber代表占用多少列宽度,1为一列,2为合并二列,总列数不能超过或少过数据表的列数;CellValue就是显示的名称。

        统计行可以是单列或多列,以string方式传入,不同列用逗号分隔,如“列名1,列名3”,可以是任意列,可以相邻或间隔,但必须是数据表中存在的列并且值可以相加。当只有一列统计时在此列的前面显示“统计:”,如果是第一列则不显示。

       数据表会生成DataSets节点和Tablix节点。

        private List<ReportColoumStyle> _coloumStyle = new List<ReportColoumStyle>();
        internal const float ColoumWidth = 1.6F; //列宽
        private float tabelTopPosition = 0.0F;
        private DataTable hearderName = new DataTable();
        private DataTable hearderMerge = new DataTable();
        private DataTable hearderTop = new DataTable();

        public void SetColoumStyle(List<ReportColoumStyle> coloumStyle)
        {
            this._coloumStyle = coloumStyle;
        }

        public void AddData(DataTable dataTable, string totalColumn)
        {
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                var coloumNames = new List<string>();
                foreach (DataColumn dataColumn in dataTable.Columns)
                {
                    var protertyName = dataColumn.ColumnName;
                    coloumNames.Add(protertyName);
                }
                AddReportItemPattern(coloumNames.ToArray(), dataTable, totalColumn);
            }
        }

        protected void AddReportItemPattern(string[] coloumNames, dynamic data, string totalColumn = "")
        {
            StringBuilder fields = new StringBuilder();
            StringBuilder coloums = new StringBuilder();
            StringBuilder tablixHearders = new StringBuilder();
            StringBuilder tablixCells = new StringBuilder();
            StringBuilder tablixMembers = new StringBuilder();
            float currentNamePrefix = _reportItemPatterns.Count + _reportHeadPatterns.Count + 1;
            float tableWidth = 0F;
            float dataRows = ((DataTable)data).Rows.Count; //数据行数
            int totalNumber = 0;
            string totalWidth = "";
            string totalCellPattern = "";
            string totalMemberPattern = "";
            string mergeMemberPattern = "";
            Boolean isHearderMerge = true;
            float hearderHeight = 0.0F;

            foreach (var coloumName in coloumNames)
            {
                var coloumWidth = ColoumWidth;
                var textAlign = TextAlign.Right;
                var consoleColor = ConsoleColor.Black;
                var isShortDate = false;
                var reportColoumStyle = _coloumStyle.FirstOrDefault(r => r.ColoumName == coloumName);
                if (reportColoumStyle != null)
                {
                    textAlign = reportColoumStyle.TextAlign;
                    coloumWidth = reportColoumStyle.ColoumWidth;
                    consoleColor = reportColoumStyle.ConsoleColor;
                    isShortDate = reportColoumStyle.IsShortDate;
                }
                tableWidth += coloumWidth;
                if (totalColumn != "")
                {
                    if (totalColumn.Split(',').Length > 1)
                    {
                        for (int i = 0; i < totalColumn.Split(',').Length; i++)
                        {
                            if (totalColumn.Split(',')[i] == coloumName)
                            {
                                if (totalWidth == "")
                                {
                                    totalWidth = totalNumber.ToString();
                                }
                                else
                                {
                                    totalWidth += "," + totalNumber.ToString();
                                }
                            }
                        }
                    }
                    else
                    {
                        if (totalColumn == coloumName)
                        {
                            totalWidth = totalNumber.ToString();
                        }
                    }
                }
                totalNumber += 1;

                var bottomBorder = string.Empty; //每个单元格底部border
                if (dataRows == 0)
                {
                    bottomBorder = "<BottomBorder><Style>None</Style></BottomBorder>";
                }
                var coloumValue = coloumName;
                if (hearderName.Rows.Count > 0)
                {
                    foreach (DataColumn dc in hearderName.Columns)
                    {
                        if (dc.ColumnName == coloumName)
                        {
                            if (hearderName.Rows[0][coloumName].ToString().Trim() != "") coloumValue = hearderName.Rows[0][coloumName].ToString();
                        }
                    }
                }
                //例外,如果coloumName包含Coloum之类的字段,则将value设成空
                if (coloumName.IndexOf("Column", System.StringComparison.Ordinal) > -1)
                {
                    coloumValue = " ";
                }

                fields.AppendFormat(
                    "<Field Name=\"{0}\"><DataField>{0}</DataField><rd:TypeName>System.String</rd:TypeName></Field>",
                    coloumName);
                coloums.AppendFormat("<TablixColumn><Width>{0}cm</Width></TablixColumn>", coloumWidth);
                if (hearderMerge.Rows.Count > 0)
                {
                    if (isHearderMerge)
                    {
                        hearderHeight += 0.5F;
                        mergeMemberPattern = "<TablixMember><KeepWithGroup>After</KeepWithGroup><RepeatOnNewPage>true</RepeatOnNewPage></TablixMember>";
                        if (hearderTop.Rows.Count > 0)
                        {
                            hearderHeight += 0.5F;
                            mergeMemberPattern += "<TablixMember><KeepWithGroup
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xgh815

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值