Beispiel #1
0
        public void Read(object destination, string excelFileName)
        {
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }

            using (ExcelManager em = new ExcelManager())
            {
                em.Open(excelFileName);

                HandleCommonClassAttributes(em, destination);

                // Check attributes of Properties in class
                Type objType = destination.GetType();
                foreach (PropertyInfo propInfo in objType.GetProperties())
                {
                    object[] attributes = propInfo.GetCustomAttributes(true);
                    if (attributes == null)
                    {
                        continue;
                    }

                    foreach (Attribute att in attributes)
                    {
                        HandleCommonPropertiesAttributes(em, destination, att, propInfo);

                        // Read Specific Attributes:

                        // [FromCell]
                        FromCellAttribute fromCell = att as FromCellAttribute;
                        if (fromCell != null)
                        {
                            object value = em.GetValue(fromCell.CellAddress, fromCell.Category);
                            propInfo.SetValue(destination, value, null);
                        }

                        // [FromRange]
                        FromRangeAttribute fromRange = att as FromRangeAttribute;
                        if (fromRange != null)
                        {
                            ArrayList values = em.GetRangeValues(fromRange.StartCellAddress, fromRange.EndCellAddress, fromRange.Category);
                            propInfo.SetValue(destination, values, null);
                        }
                    }
                }
            }
        }