From 7b7043a57cec23de487554fcbb0f20d91a62548a Mon Sep 17 00:00:00 2001 From: Chris Missal Date: Fri, 6 Mar 2015 10:18:00 -0600 Subject: [PATCH] fixes #38 HeaderHeight is a nullable double on XlsxMediaTypeFormatter, but the constructor only accepts a non-nullable double. By changing this to nullable the consumer can now ignore the height and it will use the default. The lack of this feature only seemed to affect MS Excel. Previously the height was set to zero, and "hidding" it in MS Excel. Google Spreadsheets seemed unaffected. --- .../XlsxMediaTypeFormatter.cs | 2 +- .../XlsxMediaTypeFormatterTests.cs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/WebApiContrib.Formatting.Xlsx/XlsxMediaTypeFormatter.cs b/src/WebApiContrib.Formatting.Xlsx/XlsxMediaTypeFormatter.cs index f49e43d..3adbbc1 100644 --- a/src/WebApiContrib.Formatting.Xlsx/XlsxMediaTypeFormatter.cs +++ b/src/WebApiContrib.Formatting.Xlsx/XlsxMediaTypeFormatter.cs @@ -66,7 +66,7 @@ public class XlsxMediaTypeFormatter : MediaTypeFormatter /// Height of each row of data. /// An action method that modifies the worksheet cell style. /// An action method that modifies the cell style of the first (header) row in the worksheet. - public XlsxMediaTypeFormatter(bool autoFit = true, bool autoFilter = false, bool freezeHeader = false, double headerHeight = 0, Action cellStyle = null, Action headerStyle = null) + public XlsxMediaTypeFormatter(bool autoFit = true, bool autoFilter = false, bool freezeHeader = false, double? headerHeight = null, Action cellStyle = null, Action headerStyle = null) { SupportedMediaTypes.Clear(); SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")); diff --git a/test/WebApiContrib.Formatting.Xlsx.Tests/XlsxMediaTypeFormatterTests.cs b/test/WebApiContrib.Formatting.Xlsx.Tests/XlsxMediaTypeFormatterTests.cs index 2718b24..bdd81d9 100644 --- a/test/WebApiContrib.Formatting.Xlsx.Tests/XlsxMediaTypeFormatterTests.cs +++ b/test/WebApiContrib.Formatting.Xlsx.Tests/XlsxMediaTypeFormatterTests.cs @@ -397,6 +397,20 @@ public void WriteToStreamAsync_WithArrayOfDateTime_WritesExcelDocumentToStream() Assert.AreEqual(data[1], sheet.GetValue(2, 1), "Value in A2 is incorrect."); } + [TestMethod] + public void XlsxMediaTypeFormatter_WithDefaultHeaderHeight_DefaultsToSameHeightForAllCells() + { + var data = new[] { new SimpleTestItem { Value1 = "A1", Value2 = "B1" }, + new SimpleTestItem { Value1 = "A1", Value2 = "B2" } }; + + var formatter = new XlsxMediaTypeFormatter(); + + var sheet = GetWorksheetFromStream(formatter, data); + + Assert.AreNotEqual(sheet.Row(1).Height, 0d, "HeaderHeight should not be zero"); + Assert.AreEqual(sheet.Row(1).Height, sheet.Row(2).Height, "HeaderHeight should be the same as other rows"); + } + [TestMethod] public void WriteToStreamAsync_WithCellAndHeaderFormats_WritesFormattedExcelDocumentToStream() {