Ejemplo n.º 1
0
        public static XLWorkbook ToXlsx <T>(
            [NotNull, ItemNotNull] this T[] data,
            [NotNull] XlsxExport export,
            [NotNull] string title)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (data.Any(x => x == null))
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (export == null)
            {
                throw new ArgumentNullException(nameof(export));
            }
            if (export.Columns == null)
            {
                throw new ArgumentNullException(nameof(export));
            }
            if (export.Columns.Any(x => x == null))
            {
                throw new ArgumentNullException(nameof(export));
            }
            if (export.Columns.Any(x => string.IsNullOrWhiteSpace(x.Property) || x.Property.Trim().Length != x.Property.Length))
            {
                throw new ArgumentNullException(nameof(export));
            }
            if (title == null)
            {
                throw new ArgumentNullException(nameof(title));
            }

            var tableData = CreateTableData(export, data.ToArray());

            return(CreateXlsx(title, tableData));
        }
Ejemplo n.º 2
0
        public static object[][] CreateTableData <T>(
            [NotNull] XlsxExport export,
            [NotNull, ItemNotNull] T[] data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (data.Any(x => x == null))
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (export == null)
            {
                throw new ArgumentNullException(nameof(export));
            }
            if (export.Columns == null)
            {
                throw new ArgumentNullException(nameof(export));
            }
            if (export.Columns.Any(x => x == null))
            {
                throw new ArgumentNullException(nameof(export));
            }
            if (export.Columns.Any(x => string.IsNullOrWhiteSpace(x.Property) || x.Property.Trim().Length != x.Property.Length))
            {
                throw new ArgumentNullException(nameof(export));
            }

            var compiledColumns = export.Columns.Select((c, idx) =>
            {
                Func <T, object> accessor;
                try
                {
                    accessor = PropertiesHelper.GetDottedPropertyGetter <T, object>(c.Property);
                }
                catch (Exception e)
                {
                    throw new CreateTableException($"{nameof(XlsxExport.Columns).FromCamelCaseToJsonCamelCase()}/{idx}/{nameof(XlsxColumn.Property).FromCamelCaseToJsonCamelCase()}", $"Could not access path '{c.Property}'.", e);
                }
                return(new
                {
                    Column = c,
                    Accessor = accessor
                });
            }).ToArray();

            var tableData = new object[1 + data.Length][];

            tableData[0] = new object[compiledColumns.Length];
            for (var colIdx = 0; colIdx < compiledColumns.Length; colIdx++)
            {
                tableData[0][colIdx] = compiledColumns[colIdx].Column.Caption;
            }

            for (var rowIdx = 0; rowIdx < data.Length; rowIdx++)
            {
                tableData[1 + rowIdx] = new object[compiledColumns.Length];
                for (var colIdx = 0; colIdx < compiledColumns.Length; colIdx++)
                {
                    tableData[1 + rowIdx][colIdx] = compiledColumns[colIdx].Accessor(data[rowIdx]);
                }
            }

            return(tableData);
        }