Skip to content

Commit 7b7043a

Browse files
committed
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.
1 parent ef0e302 commit 7b7043a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/WebApiContrib.Formatting.Xlsx/XlsxMediaTypeFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class XlsxMediaTypeFormatter : MediaTypeFormatter
6666
/// <param name="cellHeight">Height of each row of data.</param>
6767
/// <param name="cellStyle">An action method that modifies the worksheet cell style.</param>
6868
/// <param name="headerStyle">An action method that modifies the cell style of the first (header) row in the worksheet.</param>
69-
public XlsxMediaTypeFormatter(bool autoFit = true, bool autoFilter = false, bool freezeHeader = false, double headerHeight = 0, Action<ExcelStyle> cellStyle = null, Action<ExcelStyle> headerStyle = null)
69+
public XlsxMediaTypeFormatter(bool autoFit = true, bool autoFilter = false, bool freezeHeader = false, double? headerHeight = null, Action<ExcelStyle> cellStyle = null, Action<ExcelStyle> headerStyle = null)
7070
{
7171
SupportedMediaTypes.Clear();
7272
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));

test/WebApiContrib.Formatting.Xlsx.Tests/XlsxMediaTypeFormatterTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,20 @@ public void WriteToStreamAsync_WithArrayOfDateTime_WritesExcelDocumentToStream()
397397
Assert.AreEqual(data[1], sheet.GetValue<DateTime>(2, 1), "Value in A2 is incorrect.");
398398
}
399399

400+
[TestMethod]
401+
public void XlsxMediaTypeFormatter_WithDefaultHeaderHeight_DefaultsToSameHeightForAllCells()
402+
{
403+
var data = new[] { new SimpleTestItem { Value1 = "A1", Value2 = "B1" },
404+
new SimpleTestItem { Value1 = "A1", Value2 = "B2" } };
405+
406+
var formatter = new XlsxMediaTypeFormatter();
407+
408+
var sheet = GetWorksheetFromStream(formatter, data);
409+
410+
Assert.AreNotEqual(sheet.Row(1).Height, 0d, "HeaderHeight should not be zero");
411+
Assert.AreEqual(sheet.Row(1).Height, sheet.Row(2).Height, "HeaderHeight should be the same as other rows");
412+
}
413+
400414
[TestMethod]
401415
public void WriteToStreamAsync_WithCellAndHeaderFormats_WritesFormattedExcelDocumentToStream()
402416
{

0 commit comments

Comments
 (0)