/// <summary>
        /// 对cell索引表达式进行计算
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public string CalculateCellName(object data)
        {
            var matches = Regex.Matches(CellName, @"\{\+([\w\s+.]+)\}");

            if (matches.Count == 0)
            {
                return(CellName);
            }


            var length  = CellName.Length;
            int rowGrow = 0;
            int colGrow = 0;

            // 从cell表达式中获取原始cell坐标
            foreach (Match item in matches)
            {
                CellName = CellName.Replace(item.Value, string.Empty);
            }
            var location = ExcelHandler.CellNameToLocation(CellName);


            if (matches.Count == 1)
            {
                var match = matches[0];
                var grow  = RazorTemplateHelper.RunRazorSnippet <int>(match.Groups[1].Value, data);
                if (matches[0].Length + matches[0].Index == length)
                {
                    rowGrow = grow;
                }
                else
                {
                    colGrow = grow;
                }
            }
            else
            {
                if (matches.Count == 2)
                {
                    colGrow = RazorTemplateHelper.RunRazorSnippet <int>(matches[0].Groups[1].Value, data);
                    rowGrow = RazorTemplateHelper.RunRazorSnippet <int>(matches[1].Groups[1].Value, data);
                }
            }

            location.RowIndex    += rowGrow;
            location.ColumnIndex += colGrow;

            return(ExcelHandler.LocationToCellName(location.ColumnIndex, location.RowIndex));
        }
예제 #2
0
        private async Task <string> ViewAsyncInternal <T>(bool useModel, T model = default(T), string methodCaller = null, string viewName = null, bool searchOnlyInResources = false)
        {
            var template = new RazorTemplateHelper();

            if (viewName != null)
            {
                return(await template.ProcessByViewNameAsync <T>(model, viewName, useModel, searchOnlyInResources));
            }
            else
            {
                var executeInfo = new RazorTemplateHelper.ExecuteInfo
                {
                    Method = methodCaller,
                    Type   = this.GetType()
                };

                return(await template.ProcessByViewNameAsync <T>(model, executeInfo, useModel, searchOnlyInResources));
            }
        }
예제 #3
0
        /// <summary>
        /// 创建列表属性映射
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="config">属性映射配置</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ExcelEntityPropertyMapping CreateListPropertyMapping <T>(ExcelEntityPropertyMappingConfig config, T data)
        {
            // Func<string, int> getIncrease = null
            var dir        = config.Direction;
            var propName   = config.PropertyName;
            var originCell = config.CalculateCellName(data);
            var lineLimit  = RazorTemplateHelper.RunRazorSnippet <int>(config.LineLimit, data);
            var properties = config.ItemProperties;
            var listCount  = data.GetPropertyValue <IList>(propName).Count;

            var result         = new ExcelEntityPropertyMapping(propName, originCell);
            var originLocation = ExcelHandler.CellNameToLocation(originCell);

            var currLocation = originLocation;

            result.Items = new List <ExcelEntityMapping>();

            for (int i = 0; i < listCount; i++)
            {
                var itemMapping = new ExcelEntityMapping();
                var max         = 0;

                // 为每个属性赋值
                for (int j = 0; j < properties.Count; j++)
                {
                    var itemPropName = properties[j].PropertyName;
                    var increase     = j == 0 ? 0 : 1;

                    if (!string.IsNullOrEmpty(properties[j].Increase))
                    {
                        increase = RazorTemplateHelper.RunRazorSnippet <int>(properties[j].Increase, data);
                    }

                    if (dir == RenderDirection.Horizontal)
                    {
                        currLocation.ColumnIndex += increase;
                        max = Math.Max(max, currLocation.ColumnIndex);
                    }
                    else
                    {
                        currLocation.RowIndex += increase;
                        max = Math.Max(max, currLocation.RowIndex);
                    }

                    var cellName = ExcelHandler.LocationToCellName(currLocation.ColumnIndex, currLocation.RowIndex);

                    var mapping = new ExcelEntityPropertyMapping(itemPropName, cellName, properties[j].Value);

                    itemMapping.Add(mapping);
                }

                result.Items.Add(itemMapping);

                if (dir == RenderDirection.Horizontal)
                {
                    // 超出限制
                    if (lineLimit != 0 && (currLocation.RowIndex - originLocation.RowIndex + 1) >= lineLimit)
                    {
                        currLocation.RowIndex      = originLocation.RowIndex;
                        originLocation.ColumnIndex = currLocation.ColumnIndex = max + 1;
                    }
                    else
                    {
                        currLocation.RowIndex   += 1;
                        currLocation.ColumnIndex = originLocation.ColumnIndex;
                    }
                }
                else
                {
                    if (lineLimit != 0 && (currLocation.ColumnIndex - originLocation.ColumnIndex + 1) >= lineLimit)
                    {
                        currLocation.ColumnIndex = originLocation.ColumnIndex;
                        originLocation.RowIndex  = currLocation.RowIndex = max + 1;
                    }
                    else
                    {
                        currLocation.ColumnIndex += 1;
                        currLocation.RowIndex     = originLocation.RowIndex;
                    }
                }
            }

            return(result);
        }
예제 #4
0
        /// <summary>
        /// Parse a razor view from the content string without model
        /// </summary>
        /// <param name = "content" > Razor content</param>
        /// <param name = "model" > Model object</param>
        /// <returns>Text for view</returns>
        public virtual async Task <string> ViewContentAsync(string content)
        {
            var template = new RazorTemplateHelper();

            return(await template.ProcessByContentAsync(content, default(object), false));
        }
예제 #5
0
        /// <summary>
        /// Parse a razor view from the content string with model
        /// </summary>
        /// <typeparam name = "T" > Type of model</typeparam>
        /// <param name = "content" > Razor content</param>
        /// <param name = "model" > Model object</param>
        /// <returns>Text for view</returns>
        public virtual async Task <string> ViewContentAsync <T>(string content, T model = default(T))
        {
            var template = new RazorTemplateHelper();

            return(await template.ProcessByContentAsync(content, model, true));
        }