/// <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 if (HeaderStyle != null) { HeaderStyle(document.Worksheet.Row(1).Style); } if (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). if (AutoFilter) { cells.AutoFilter = AutoFilter; } if (AutoFit) { cells.AutoFitColumns(); } // Set header row where specified. if (HeaderHeight.HasValue) { document.Worksheet.Row(1).Height = HeaderHeight.Value; document.Worksheet.Row(1).CustomHeight = true; } }
/// <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 if (HeaderStyle != null) HeaderStyle(document.Worksheet.Row(1).Style); if (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). if (AutoFilter) cells.AutoFilter = AutoFilter; if (AutoFit) cells.AutoFitColumns(); // Set header row where specified. if (HeaderHeight.HasValue) { document.Worksheet.Row(1).Height = HeaderHeight.Value; document.Worksheet.Row(1).CustomHeight = true; } }
public override Task WriteToStreamAsync(Type type, object value, System.IO.Stream writeStream, System.Net.Http.HttpContent content, System.Net.TransportContext transportContext) { // Create a document builder. var document = new XlsxDocumentBuilder(writeStream); var valueType = value.GetType(); // Apply cell styles. if (CellStyle != null) CellStyle(document.Worksheet.Cells.Style); // Get the item type. var itemType = (util.IsSimpleType(valueType)) ? null : util.GetEnumerableItemType(valueType); // If a single object was passed, treat it like a list with one value. if (itemType == null) { itemType = valueType; value = new object[] { value }; } // Used if no other matching serialiser can be found. IXlsxSerialiser serialiser = DefaultSerializer; // Determine if a more specific serialiser might apply. foreach (var s in Serialisers) { if (s.CanSerialiseType(valueType, itemType)) { serialiser = s; break; } } serialiser.Serialise(itemType, value, document); // Only format spreadsheet if it has content. if (document.RowCount > 0) { if (serialiser.IgnoreFormatting) { // Autofit cells if specified. if (AutoFit) document.AutoFit(); } else FormatDocument(document); } return document.WriteToStream(); }
public override Task WriteToStreamAsync(Type type, object value, System.IO.Stream writeStream, System.Net.Http.HttpContent content, System.Net.TransportContext transportContext) { // Create a document builder. var document = new XlsxDocumentBuilder(writeStream); if (value == null) { return(document.WriteToStream()); } var valueType = value.GetType(); // Apply cell styles. if (CellStyle != null) { CellStyle(document.Worksheet.Cells.Style); } // Get the item type. var itemType = (util.IsSimpleType(valueType)) ? null : util.GetEnumerableItemType(valueType); // If a single object was passed, treat it like a list with one value. if (itemType == null) { itemType = valueType; value = new object[] { value }; } // Used if no other matching serialiser can be found. IXlsxSerialiser serialiser = DefaultSerializer; // Determine if a more specific serialiser might apply. foreach (var s in Serialisers) { if (s.CanSerialiseType(valueType, itemType)) { serialiser = s; break; } } serialiser.Serialise(itemType, value, document); // Only format spreadsheet if it has content. if (document.RowCount > 0) { if (serialiser.IgnoreFormatting) { // Autofit cells if specified. if (AutoFit) { document.AutoFit(); } } else { FormatDocument(document); } } return(document.WriteToStream()); }