Exemplo n.º 1
0
        /// <summary>
        /// 将Excel数据直充到指定的Collection中
        /// </summary>
        /// <typeparam name="T">数据模型</typeparam>
        /// <typeparam name="TCollection">待待充Collection 必须实现ICollection<T> 接口</typeparam>
        /// <param name="collection">待待充Collection</param>
        /// <param name="tbDesp">Table相关描述信息</param>
        /// <param name="param">填充时所准备的相关参数,包括初始化每一个对象委托,根据相关数据设置相关属性值委托(默认反射)</param>
        /// <returns></returns>
        public string GetCollectionFromTable <T, TCollection>(TCollection collection, TableDescription tbDesp, SpreadGetTableCollectionParams <T> param) where TCollection : ICollection <T>
        {
            tbDesp.NullCheck("tbDesp");
            collection.NullCheck("数据集合不能为空");
            param.ExportRow.NullCheck("exportRow");
            this.CheckTableExists(tbDesp.TableName);

            StringBuilder customLog = new StringBuilder();
            Table         tb        = this.Tables[tbDesp.TableName];
            int           rowIndex  = 0;

            foreach (TableRow tr in tb.Rows)
            {
                ExcportRowContext context = new ExcportRowContext();
                context.RowIndex = rowIndex;
                foreach (TableColumnDescription tc in tbDesp.AllColumns)
                {
                    if (tb.Columns.ContainsKey(tc.ColumnName))
                    {
                        context.PropertyDescriptions.Add(new ExportCellDescription(tc.PropertyName)
                        {
                            TableColumnName = tc.ColumnName, Value = tr[tb.Columns[tc.ColumnName]].Value, Address = CellAddress.Parse(tb.Columns[tc.ColumnName].Position + tb.Address.StartColumn, tr.RowIndex + tb.Address.StartRow + 1).ToString()
                        });
                    }
                }

                ExportRowResult <T> result = param.ExportRow(context);

                if (result.Validated == true && result.CurrentObject != null)
                {
                    collection.Add(result.CurrentObject);
                }

                customLog.Append(result.ErrorLog);
                rowIndex++;

                if (result.Validated == false && param.ValidationOperator == ValidationErrorStopMode.Stop)
                {
                    break;
                }
            }

            return(customLog.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// 逐行获取Table数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tbDesp"></param>
        /// <param name="getemptyObj"></param>
        /// <param name="validationOperator"></param>
        public string ForEachTableRows <T>(TableDescription tbDesp, SpreadGetTableCollectionParams <T> param, out int upCount)
        {
            tbDesp.NullCheck("tbDesp");
            param.ExportRow.NullCheck("exportRow");
            this.CheckTableExists(tbDesp.TableName);

            StringBuilder customLog = new StringBuilder();
            Table         tb        = this.Tables[tbDesp.TableName];
            int           rowIndex  = 0; upCount = 0;

            foreach (TableRow tr in tb.Rows)
            {
                ExcportRowContext context = new ExcportRowContext()
                {
                    RowIndex = rowIndex
                };
                foreach (TableColumnDescription tc in tbDesp.AllColumns)
                {
                    if (tb.Columns.ContainsKey(tc.ColumnName))
                    {
                        context.PropertyDescriptions.Add(new ExportCellDescription(tc.PropertyName)
                        {
                            TableColumnName = tc.ColumnName, Value = tr[tb.Columns[tc.ColumnName]].Value, Address = CellAddress.Parse(tb.Columns[tc.ColumnName].Position + tb.Address.StartColumn, tr.RowIndex + tb.Address.StartRow + 1).ToString()
                        });
                    }
                }
                ExportRowResult <T> result = param.ExportRow(context);
                customLog.Append(result.ErrorLog);
                rowIndex++;

                if (result.Validated)
                {
                    upCount++;
                }
                else
                if (param.ValidationOperator == ValidationErrorStopMode.Stop)
                {
                    break;
                }
            }

            return(customLog.ToString());
        }