コード例 #1
0
        public static void SerializeCollection(Type itemType, IEnumerable collection, ExcelWorksheet target, int startRow, int startColumn, XlsxSerializerSettings settings)
        {
            var mapping = DocumentMapper.GetMapping(itemType);

            var sheetRow = 1 + startRow;

            if (mapping.HasHeader)
            {
                for (var i = mapping.StartColumn; i <= mapping.EndColumn; i++)
                {
                    var headerCell = target.Cells[sheetRow, i + 1 + startColumn];
                    mapping.SetupHeader(i, headerCell);
                }

                sheetRow++;
            }

            foreach (var item in collection)
            {
                foreach (var columnMapping in mapping.Values)
                {
                    columnMapping.WriteCell(item, target.Cells[sheetRow, columnMapping.CellLocation.Column + 1 + startColumn], settings);
                }

                sheetRow++;
            }
        }
コード例 #2
0
        public static void Serialize(Type modelType, object model, ExcelWorksheet target, XlsxSerializerSettings settings)
        {
            var mapping = DocumentMapper.GetMapping(modelType);

            foreach (var m in mapping.Values)
            {
                if (m.CellLocation.Row == null)
                {
                    continue;
                }

                var cell = target.Cells[m.CellLocation.Row.Value + 1, m.CellLocation.Column + 1];

                m.WriteCell(model, cell, settings);
            }
        }
コード例 #3
0
        public static void Deserialize(object modelInstance, ExcelWorksheet source, XlsxSerializerSettings settings)
        {
            if (modelInstance == null)
            {
                throw new ArgumentNullException(nameof(modelInstance));
            }

            var mapping = DocumentMapper.GetMapping(modelInstance.GetType());

            foreach (var rule in mapping.Values)
            {
                if (rule.CellLocation.Row == null)
                {
                    continue;
                }

                var sourceCell = source.Cells[rule.CellLocation.Row.Value + 1, rule.CellLocation.Column + 1];
                rule.ReadCell(() => modelInstance, sourceCell, settings);
            }
        }
コード例 #4
0
        public static IEnumerable DeserializeCollection(Type collectionItemType, ExcelWorksheet source, Func <object> newItemFactory, int startRow, int startColumn, XlsxSerializerSettings settings)
        {
            var mapping = DocumentMapper.GetMapping(collectionItemType);

            var sheetRow = (mapping.HasHeader ? 2 : 1) + startRow;

            for (; sheetRow <= (source.Dimension?.End?.Row ?? 0); sheetRow++)
            {
                object item = null;

                foreach (var cellMapping in mapping.Values)
                {
                    var cell = source.Cells[sheetRow, cellMapping.CellLocation.Column + 1 + startColumn];
                    try
                    {
                        var readingResult = cellMapping.ReadCell(item == null ? newItemFactory : () => item, cell, settings);

                        if ((item == null) || (readingResult != null))
                        {
                            item = readingResult;
                        }

                        if (item is IHasSourceRowIndex rowReference)
                        {
                            rowReference.SourceRowIndex = sheetRow;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new XlsxSerializerException(cell, ex);
                    }
                }

                if (item == null)
                {
                    yield break;
                }

                yield return(item);
            }
        }