コード例 #1
0
        /// <summary>
        /// SetValueForSinggleCellSingle
        /// </summary>
        /// <param name="propAttribute"></param>
        /// <param name="property"></param>
        /// <param name="obj"></param>
        private static void SetValueForSinggleCellSingle(ImportedAttribute propAttribute, PropertyInfo property, object obj, uint offSetRow, uint offSetCol)
        {
            var propValue = property.GetValue(obj);

            if (propValue == null)
            {
                throw new NullReferenceException(nameof(obj));
            }
            var propType = propValue.GetType();
            var oriCell  = FindTheOriginalLocation(propAttribute);
            var newCol   = oriCell.Column + offSetCol;

            if (propType.IsArray == false)                       // Import Others
            {
                var newRow = oriCell.Row + offSetRow;
                if (property.PropertyType.Assembly.GetName().Name != "mscorlib")                         // Import User-defined Types
                {
                    Import(propValue, newRow, newCol);
                }
                else                                                                                    // Import Basic Types
                {
                    propValue = TryToChangeType(_theSheet.Cells[newRow, newCol].Value, propType);
                }
                // Set imported value
                property.SetValue(obj, propValue);
            }
            else                                                                                        // Invalid Format
            {
                throw new Exception("Type of Attribute is invalid: SetValueForSinggleCellSingle");
            }
        }
コード例 #2
0
        /// <summary>
        /// FindTheOriginalLocation
        /// </summary>
        /// <param name="propAttribute"></param>
        /// <returns></returns>
        /// <remarks>
        /// Split string by character ','
        /// Find Row in Dictionary
        /// Find Column indictionary
        /// </remarks>
        private static ExLocation FindTheOriginalLocation(ImportedAttribute propAttribute)
        {
            var startingCell = propAttribute.StartingCell;

            // when starting cell contain character ','
            if (startingCell.Contains(","))
            {
                // Split string by character ','
                var  splitArray = startingCell.Split(',');
                uint row, column;
                bool isRownummeric = uint.TryParse(splitArray[0].Trim(), out row);
                if (!isRownummeric)
                {
                    // Find Row in Dictionary
                    row = HeaderCell[splitArray[0].Trim()].Row;
                }
                var isColnummeric = uint.TryParse(splitArray[1].Trim(), out column);
                if (!isColnummeric)
                {
                    // Find Column indictionary
                    column = HeaderCell[splitArray[0].Trim()].Column;
                }

                return(new ExLocation(row, column));
            }

            // case starting cell normal string
            var oriCell = _theSheet.Range[propAttribute.StartingCell].Cells[1, 1];

            return(new ExLocation((uint)oriCell.Row, (uint)oriCell.Column));
        }
コード例 #3
0
        /// <summary>
        /// SetValueForVerticalArray
        /// </summary>
        /// <param name="propAttribute"></param>
        /// <param name="property"></param>
        /// <param name="obj"></param>
        private static void SetValueForVerticalArray(ImportedAttribute propAttribute, PropertyInfo property, object obj, uint offSetRow, uint offSetCol)
        {
            var propValue = property.GetValue(obj);
            var propType  = propValue.GetType();

            if (propType.IsArray)
            {
                var  array       = propValue as Array;
                var  elementType = propType.GetElementType();
                uint offSet      = 0;
                var  oriCell     = FindTheOriginalLocation(propAttribute);
                var  newCol      = oriCell.Column + offSetCol;

                for (int i = 0; i < array.Length; i++)
                {
                    var newRow = oriCell.Row + offSet + offSetRow;

                    object value = array.GetValue(i);
                    if (elementType.Assembly.GetName().Name != "mscorlib")
                    {
                        ImportArrayElement(value, newRow, newCol);
                    }
                    else
                    {
                        value = TryToChangeType(_theSheet.Cells[newRow, newCol].Value, elementType);
                    }
                    offSet += propAttribute.Size;
                    array.SetValue(value, i);
                }
            }
            else
            {
                throw new Exception("Invalid Import Format of Array!");
            }
        }
コード例 #4
0
        /// <summary>
        /// SetValueforVerticalList
        /// </summary>
        /// <param name="propAttribute"></param>
        /// <param name="property"></param>
        /// <param name="obj"></param>
        /// <param name="offSetRow"></param>
        /// <param name="offSetCol"></param>
        private static void SetValueforVerticalList(ImportedAttribute propAttribute, PropertyInfo property, object obj, uint offSetRow, uint offSetCol)
        {
            var propValue = property.GetValue(obj);
            var propType  = propValue.GetType();

            if (propType.IsArray)
            {
                var  array       = propValue as Array;
                var  elementType = propType.GetElementType();
                uint offSet      = 0;
                var  oriCell     = FindTheOriginalLocation(propAttribute);
                var  newCol      = oriCell.Column + offSetCol;

                for (int i = 0; i < array.Length; i++)
                {
                    var newRow = oriCell.Row + offSet + offSetRow;
                    if (propAttribute.EndSign == "")
                    {
                        if (_theSheet.Cells[newRow, newCol].Value == null)
                        {
                            break;
                        }
                    }
                    else
                    {
                        if (HeaderCell.ContainsKey(propAttribute.EndSign))
                        {
                            var endLocation = HeaderCell[propAttribute.EndSign];
                            if (endLocation != null)
                            {
                                if (newRow >= endLocation.Row)
                                {
                                    break;
                                }
                            }
                        }
                    }

                    object value;
                    if (elementType.Assembly.GetName().Name != "mscorlib")
                    {
                        value = Activator.CreateInstance(elementType);
                        array.SetValue(value, i);
                        ImportArrayElement(value, newRow, newCol);
                    }
                    else
                    {
                        value = TryToChangeType(_theSheet.Cells[newRow, newCol].Value, elementType);
                    }
                    offSet += propAttribute.Size;
                    array.SetValue(value, i);
                }
            }
            else
            {
                throw new Exception("Invalid Import Format of Array!");
            }
        }