예제 #1
0
 /// <summary>
 /// 更新查询参数
 /// </summary>
 /// <param name="p_row"></param>
 public void UpdateParams(Core.Row p_row)
 {
     if (p_row != null)
     {
         Params = p_row.ToDict();
         Sheet  = null;
     }
 }
예제 #2
0
파일: RptTextInst.cs 프로젝트: Daoting/dt
        /// <summary>
        /// 解析文本内容
        /// </summary>
        void ParseValue()
        {
            RptText item = _item as RptText;
            List <RptExpression> exps = item.Expressions;

            if (exps == null || exps.Count == 0)
            {
                if (item.IsScriptRender)
                {
                    // 通过外部脚本绘制单元格内容和样式
                    string tbl = null;
                    if (item.Parent is RptTblPartRow tblRow)
                    {
                        tbl = tblRow.Table.Tbl;
                    }
                    else if (item.Parent is RptMtxRow ib && ib.Parent is RptMatrix mtx)
                    {
                        // 矩阵其他位置无当前数据行概念,需要数据源可在脚本中通过 info.GetData 获取
                        tbl = mtx.Tbl;
                    }

                    RptData src;
                    if (!string.IsNullOrEmpty(tbl) && (src = Inst.Info.GetData(tbl).Result) != null)
                    {
                        Data = src.CurrentRow;
                    }
                }
                else
                {
                    // 静态文本
                    Text = item.Val;
                }
                return;
            }

            StringBuilder sb = new StringBuilder();

            foreach (var exp in exps)
            {
                string col = exp.VarName;
                if (exp.Func == RptExpFunc.Param)
                {
                    Dict dtParams = Inst.Info.Params;
                    if (dtParams.ContainsKey(col))
                    {
                        object val = dtParams[col];
                        var    row = item.Root.Params[col];
                        if (row != null &&
                            row.Str("type") == "DateTime" &&
                            val != null)
                        {
                            // 根据格的设置格式化日期
                            DateTime date   = (DateTime)dtParams[col];
                            string   ct     = row.Str("ct");
                            string   format = "yyyy-MM-dd";
                            if (!string.IsNullOrEmpty(ct))
                            {
                                Match match = Regex.Match(ct, "Format=\"(\\S*)\"");
                                if (match.Success && match.Groups.Count == 2)
                                {
                                    format = match.Groups[1].Value;
                                }
                            }
                            sb.Append(date.ToString(format));
                        }
                        else
                        {
                            sb.Append(val);
                        }
                    }
                }
                else if (exp.Func == RptExpFunc.Global)
                {
                    switch (col)
                    {
                    case "页号":
                        // 输出过程中
                        sb.Append("##");
                        break;

                    case "总页数":
                        // 占位符,最后渲染时才知道总页数
                        sb.Append("$$");
                        break;

                    case "水平页号":
                        sb.Append(_page.X + 1);
                        break;

                    case "垂直页号":
                        sb.Append(_page.Y + 1);
                        break;

                    case "报表名称":
                        sb.Append(Inst.Info.Name);
                        break;

                    case "日期":
                        sb.Append(DateTime.Now.ToString("yyyy-MM-dd"));
                        break;

                    case "时间":
                        sb.Append(DateTime.Now.ToString("HH:mm"));
                        break;

                    case "日期时间":
                        sb.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
                        break;
                    }
                }
                else if (exp.Func == RptExpFunc.Unknown)
                {
                    sb.Append(col);
                }
                else
                {
                    RptData src;
                    string  tbl = exp.DataName;
                    if (string.IsNullOrEmpty(tbl) || (src = Inst.Info.GetData(tbl).Result) == null)
                    {
                        continue;
                    }

                    Data = src.CurrentRow;
                    switch (exp.Func)
                    {
                    case RptExpFunc.Val:
                        if (Data != null)
                        {
                            sb.Append(Data.Str(col));
                        }
                        break;

                    case RptExpFunc.Avg:
                        sb.Append(GetRows(src.Data).Average((row) => row.Double(col)));
                        break;

                    case RptExpFunc.Max:
                        sb.Append(GetRows(src.Data).Max((row) => row.Double(col)));
                        break;

                    case RptExpFunc.Min:
                        sb.Append(GetRows(src.Data).Min((row) => row.Double(col)));
                        break;

                    case RptExpFunc.Sum:
                        sb.Append(GetRows(src.Data).Sum((row) => row.Double(col)));
                        break;

                    case RptExpFunc.Count:
                        sb.Append(GetRows(src.Data).Count());
                        break;

                    case RptExpFunc.Index:
                        sb.Append(src.Current);
                        break;
                    }
                }
            }
            Text = sb.ToString();
        }