Example #1
0
        /// <summary>
        /// Applies custom formatting to a document. (Used if matched serialiser supports formatting.)
        /// </summary>
        /// <param name="document">The <c>XlsxDocumentBuilder</c> wrapping the document to format.</param>
        private void FormatDocument(XlsxDocumentBuilder document)
        {
            // Header cell styles
            Options.HeaderStyle?.Invoke(document.Worksheet.Row(1).Style);
            if (Options.FreezeHeader)
            {
                document.Worksheet.View.FreezePanes(2, 1);
            }

            var cells = document.Worksheet.Cells[document.Worksheet.Dimension.Address];

            // Add autofilter and fit to max column width (if requested).
            cells.AutoFilter = Options.AutoFilter;
            if (Options.AutoFit)
            {
                cells.AutoFitColumns();
            }

            // Set header row where specified.
            if (Options.HeaderHeight.HasValue)
            {
                document.Worksheet.Row(1).Height       = Options.HeaderHeight.Value;
                document.Worksheet.Row(1).CustomHeight = true;
            }
        }
Example #2
0
        public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var response    = context.HttpContext.Response;
            var elementType = _elementType(context.ObjectType) ?? context.ObjectType;

            using (var document = new XlsxDocumentBuilder())
            {
                Options.CellStyle?.Invoke(document.Worksheet.Cells.Style);
                IEnumerable data = context.Object as IEnumerable;
                if (data == null && context.Object != null)
                {
                    var array = Array.CreateInstance(elementType, 1);
                    array.SetValue(context.Object, 0);
                    data = array;
                }

                var serializer = SerialisersFactory().FirstOrDefault(x => x.CanSerialiseType(context.ObjectType, elementType));

                serializer.Serialise(elementType, data, document);

                if (document.RowCount > 0)
                {
                    if (serializer.IgnoreFormatting)
                    {
                        // Autofit cells if specified.
                        if (Options.AutoFit)
                        {
                            document.AutoFit();
                        }
                    }
                    else
                    {
                        FormatDocument(document);
                    }
                }
                using (var ms = new MemoryStream())
                {
                    await document.WriteToStream(ms);

                    ms.Seek(0, SeekOrigin.Begin);
                    await ms.CopyToAsync(response.Body);
                }
            }
        }