예제 #1
0
        public BundleResult <T> Read(Stream stream, string fileName)
        {
            //   var set = Read(file);
            var dt = ExcelHandler.ToDataTable(stream, ExcelHandler.GetExcelVersion(fileName));

            return(GetCollection(dt));
        }
예제 #2
0
        public BundleResult <T> Read(string fileName)
        {
            // var set = Read(fileName);
            var dt = ExcelHandler.ToDataTable(fileName);

            return(GetCollection(dt));
        }
        /// <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));
        }
예제 #4
0
        /// <summary>
        /// 获取Cell
        /// </summary>
        /// <param name="sheet"></param>
        /// <param name="xlsIndex">cell的索引,例 "E5"</param>
        /// <param name="createIt">是否在cell不存在时创建它</param>
        /// <returns></returns>
        public static ICell GetCell(this ISheet sheet, string xlsIndex, bool createIt = true)
        {
            var xy  = ExcelHandler.CellNameToLocation(xlsIndex);
            var row = sheet.GetRow(xy.RowIndex);

            if (row == null && createIt)
            {
                row = sheet.CreateRow(xy.RowIndex);
            }
            if (row != null)
            {
                var cell = row.GetCell(xy.ColumnIndex);
                if (cell == null && createIt)
                {
                    cell = row.CreateCell(xy.ColumnIndex);
                }
                return(cell);
            }
            return(null);
        }
예제 #5
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);
        }