Exemplo n.º 1
0
            /// <summary>
            /// Read worksheet and return data in class T
            /// with properties set to the values in worksheet.
            /// </summary>
            /// <typeparam name="T">
            /// Class that as properties matching column names. Properties might be decorated with
            /// a CollumnAttribute
            /// </typeparam>
            /// <returns>
            /// Returns instances of type T with the porperties set
            /// that match a column name of the sheet
            /// </returns>
            public IEnumerable <T> Entities <T>() where T : class, new()
            {
                var t         = typeof(T);
                var columnMap = new ColumnMap(t);

                foreach (var row in ElementsOfDataRows)
                {
                    var entity   = new T();
                    var hasValue = false;
                    foreach (var cell in KeyedValues(row, (XElement e, int i) => {
                        var name = i < Header.Length ? Header[i] : null;
                        if (columnMap.GetContentType(name) == ColumnAttribute.ContentTypes.Data)
                        {
                            return(this._xlsx.CellContents.Value(e));
                        }
                        return(this._xlsx.CellContents.Formula(e));
                    }))
                    {
                        var destination = columnMap.GetPropertyInfo(cell.Key);
                        if (destination == null)
                        {
                            continue;
                        }
                        destination.SetValue(entity, Convert.ChangeType(cell.Value, destination.PropertyType));
                        hasValue = true;
                    }
                    if (hasValue)
                    {
                        yield return(entity);
                    }
                }
            }