public static bool DeserializeWorkbook(object model, ExcelWorkbook source, XlsxSerializerSettings settings)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var mainsheetProcessed = false;

            foreach (var sheetAssociation in SheetAssociation.GetSheetAssociations(model.GetType()))
            {
                var sheet = source.Worksheets[sheetAssociation.SheetName];
                if (sheet == null)
                {
                    continue;
                }

                if (sheetAssociation.BoundProperty == null)
                {
                    mainsheetProcessed = true;
                    XlsSheetDeserializerCore.Deserialize(model, sheet, settings);
                    continue;
                }

                if (ReflectionHelper.GetIsCollection(sheetAssociation.BoundProperty.PropertyType, out var itemType,
                                                     true))
                {
                    var collection = XlsCollectionDeserializerCore
                                     .DeserializeCollection(itemType, sheet, () => Activator.CreateInstance(itemType), 0, 0, settings)
                                     .OfType <object>().ToList();

                    ReflectionHelper.PopulateCollectionProperty(model, sheetAssociation.BoundProperty, collection);

                    continue;
                }

                var sheetModel = sheetAssociation.BoundProperty.GetValue(model, null);

                if (sheetModel == null)
                {
                    if (!sheetAssociation.BoundProperty.CanWrite)
                    {
                        continue;
                    }

                    sheetModel = Activator.CreateInstance(sheetAssociation.BoundProperty.PropertyType);
                    sheetAssociation.BoundProperty.SetValue(model, sheetModel, null);
                }

                XlsSheetDeserializerCore.Deserialize(sheetModel, sheet, settings);
            }

            return(mainsheetProcessed);
        }
示例#2
0
        public static void SerializeWorkbook(object model, ExcelWorkbook target, XlsxSerializerSettings settings)
        {
            if (model == null)
            {
                return;
            }

            foreach (var sheetAssociation in SheetAssociation.GetSheetAssociations(model.GetType()))
            {
                var sheet = target.Worksheets[sheetAssociation.SheetName];

                if (sheet == null)
                {
                    sheet = target.Worksheets.Add(sheetAssociation.SheetName);

                    foreach (var setup in sheetAssociation.SheetSetup)
                    {
                        setup.SetupSheet(sheet);
                    }
                }


                object sheetModelValue;

                if (sheetAssociation.BoundProperty == null)
                {
                    sheetModelValue = model;
                    XlsSheetSerializerCore.Serialize(model.GetType(), model, sheet, settings);
                    continue;
                }

                if ((!sheetAssociation.BoundProperty.CanRead) ||
                    ((sheetModelValue = sheetAssociation.BoundProperty.GetValue(model)) == null))
                {
                    continue;
                }

                if (ReflectionHelper.GetIsCollection(sheetAssociation.BoundProperty.PropertyType,
                                                     out var collectionItemType, true))
                {
                    XlsCollectionSerializerCore.SerializeCollection(collectionItemType, (IEnumerable)sheetModelValue, sheet, 0, 0, settings);
                }