Example #1
0
        public void TestPrimitiveCollectionSerialization()
        {
            var source = new List <string>();

            for (var i = 0; i < 10; i++)
            {
                source.Add($"item_{i + 1}");
            }

            List <string> deserialized = null;

            ExcelPackageSaveAndLoad.SaveAndLoadSheet(s =>
            {
                XlsCollectionSerializerCore.SerializeCollection(typeof(string), source, s, 0, 0, new XlsxSerializerSettings());
            },
                                                     s =>
            {
                deserialized = XlsCollectionDeserializerCore.DeserializeCollection(typeof(string), s, null, 0, 0, new XlsxSerializerSettings())
                               .OfType <string>().ToList();
            });

            Assert.Equal(source.Count, deserialized.Count);

            for (var i = 0; i < source.Count; i++)
            {
                Assert.Equal(source[i], deserialized[i]);
            }
        }
        public void DeserializeTo(object model, ExcelPackage package)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (ReflectionHelper.GetIsCollection(model.GetType(), out var itemType, false))
            {
                var rawCollection = XlsCollectionDeserializerCore.DeserializeCollection(itemType,
                                                                                        XlsSheetSerializerCore.GetDefaultWorksheet(package), () => Activator.CreateInstance(itemType), 0,
                                                                                        0, m_settings);

                ReflectionHelper.PopulateCollection(rawCollection, model);

                return;
            }

            if (!XlsWorkbookDeserializerCore.DeserializeWorkbook(model, package.Workbook, m_settings))
            {
                XlsSheetDeserializerCore.Deserialize(model,
                                                     XlsSheetSerializerCore.GetDefaultWorksheet(package),
                                                     m_settings);
            }
        }
Example #3
0
        public void TestCollectionSerialization(int startRow, int startCol)
        {
            var sourceCollection = ComplexCollectionItemModel.Generate(100).ToList();
            List <ComplexCollectionItemModel> deserialized = null;

            ExcelPackageSaveAndLoad.SaveAndLoadSheet(sheetToWrite =>
            {
                XlsCollectionSerializerCore.SerializeCollection(typeof(ComplexCollectionItemModel),
                                                                sourceCollection,
                                                                sheetToWrite,
                                                                startRow,
                                                                startCol,
                                                                new XlsxSerializerSettings());
            },
                                                     sheetToLoad =>
            {
                deserialized = XlsCollectionDeserializerCore.DeserializeCollection(
                    typeof(ComplexCollectionItemModel),
                    sheetToLoad,
                    () => new ComplexCollectionItemModel(),
                    startRow,
                    startCol, new XlsxSerializerSettings())
                               .OfType <ComplexCollectionItemModel>()
                               .ToList();
            }, $"collection_{startRow}_{startCol}.xlsx");

            Assert.NotNull(deserialized);

            Assert.Equal(sourceCollection.Count, deserialized.Count);

            for (var i = 0; i < sourceCollection.Count; i++)
            {
                var a = sourceCollection[i];
                var b = deserialized[i];

                Assert.Equal(a.Bool, b.Bool);
                Assert.Equal(a.Index, b.Index);
                Assert.Equal(a.Money1, b.Money1);
                Assert.Equal(a.Str1, b.Str1);
                Assert.Equal(a.Str2, b.Str2);
                Assert.Null(b.NotColumn);
            }
        }
Example #4
0
        public void ReadCellValue(ExcelRange cell, object owner, PropertyInfo targetProperty, XlsxSerializerSettings settings)
        {
            if (ReflectionHelper.GetIsCollection(targetProperty.PropertyType, out var collectionItemType, true))
            {
                var collection = XlsCollectionDeserializerCore.DeserializeCollection(collectionItemType, cell.Worksheet,
                    () => Activator.CreateInstance(collectionItemType), cell.Start.Row - 1, cell.Start.Column - 1, settings).OfType<object>().ToList();

                ReflectionHelper.PopulateCollectionProperty(owner, targetProperty, collection);

                return;
            }

            var pao = new PropertyAndOwnerInstance(targetProperty, owner);

            Func<PropertyAndOwnerInstance, bool> defaultShouldProcessCellDecision = (p) => p.Property.CanWrite;

            Func<ExcelRange, Type, PropertyAndOwnerInstance, IValueConverter, object> defaultConvertCellValueToType = (cl, tp, p, converter) =>
            {
                var cellValue = cl.Value;
                if (cellValue == null)
                {
                    return null;
                }

                return converter.FromCellValue(tp, cellValue); 
            };

            Action<PropertyAndOwnerInstance, object> defaultPropertySetter = (p, v) => p.Property.SetValue(p.PropertyOwner, v);

            settings.CellReaderInterceptor.ReadCell(cell,
                owner,
                targetProperty,
                defaultShouldProcessCellDecision,
                defaultConvertCellValueToType,
                defaultPropertySetter,
                settings);
        }