From 83360dd7c67dd51f9e0dfbcc9f87219a8959e93c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?AdrianParn=C3=A9us?= Date: Tue, 19 Aug 2025 09:56:17 +0200 Subject: [PATCH 1/3] Added null check for text in HeaderFooter --- .../ExcelHeaderFooterTextCollection.cs | 4 ++-- src/EPPlusTest/Issues/LegacyTests/Issues.cs | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/EPPlus/Style/HeaderFooterTextFormat/ExcelHeaderFooterTextCollection.cs b/src/EPPlus/Style/HeaderFooterTextFormat/ExcelHeaderFooterTextCollection.cs index c46691d5c..0ff5fc00d 100644 --- a/src/EPPlus/Style/HeaderFooterTextFormat/ExcelHeaderFooterTextCollection.cs +++ b/src/EPPlus/Style/HeaderFooterTextFormat/ExcelHeaderFooterTextCollection.cs @@ -739,7 +739,7 @@ private string ParseToHeaderFooterFormat(ExcelHeaderFooterTextItem current, Exce if (current.FontSize != prev.FontSize) { hfstring += "&" + current.FontSize; - if (char.IsDigit(current.Text[0])) + if (current.Text != null && char.IsDigit(current.Text[0])) { hfstring += " "; } @@ -748,7 +748,7 @@ private string ParseToHeaderFooterFormat(ExcelHeaderFooterTextItem current, Exce else if (current.FontSize != null && prev.FontSize == null) { hfstring += "&" + current.FontSize; - if (char.IsDigit(current.Text[0])) + if (current.Text != null && char.IsDigit(current.Text[0])) { hfstring += " "; } diff --git a/src/EPPlusTest/Issues/LegacyTests/Issues.cs b/src/EPPlusTest/Issues/LegacyTests/Issues.cs index 994045002..88d4f35d8 100644 --- a/src/EPPlusTest/Issues/LegacyTests/Issues.cs +++ b/src/EPPlusTest/Issues/LegacyTests/Issues.cs @@ -6219,5 +6219,14 @@ public void SumIfsRangeCriteriaArray() Assert.AreEqual(0d, ws.Cells["F6"].Value); } } + + [TestMethod] + public void i2078() + { + using var p = new ExcelPackage(); + var ws =p.Workbook.Worksheets.Add("Sheet 1"); + ws.HeaderFooter.OddHeader.LeftAlignedText = "&12&A"; + p.SaveAs("c:\\epplustest\\testoutput\\HeaderFooterIssue2078.xlsx"); + } } } From ad8c14f66d97b62195ed6fd7a91b9ec1f65158a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?AdrianParn=C3=A9us?= Date: Tue, 19 Aug 2025 10:01:32 +0200 Subject: [PATCH 2/3] check if null or empty --- .../ExcelHeaderFooterTextCollection.cs | 4 ++-- src/EPPlusTest/Issues/LegacyTests/Issues.cs | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/EPPlus/Style/HeaderFooterTextFormat/ExcelHeaderFooterTextCollection.cs b/src/EPPlus/Style/HeaderFooterTextFormat/ExcelHeaderFooterTextCollection.cs index 0ff5fc00d..f6171845c 100644 --- a/src/EPPlus/Style/HeaderFooterTextFormat/ExcelHeaderFooterTextCollection.cs +++ b/src/EPPlus/Style/HeaderFooterTextFormat/ExcelHeaderFooterTextCollection.cs @@ -739,7 +739,7 @@ private string ParseToHeaderFooterFormat(ExcelHeaderFooterTextItem current, Exce if (current.FontSize != prev.FontSize) { hfstring += "&" + current.FontSize; - if (current.Text != null && char.IsDigit(current.Text[0])) + if (!string.IsNullOrEmpty(current.Text) && char.IsDigit(current.Text[0])) { hfstring += " "; } @@ -748,7 +748,7 @@ private string ParseToHeaderFooterFormat(ExcelHeaderFooterTextItem current, Exce else if (current.FontSize != null && prev.FontSize == null) { hfstring += "&" + current.FontSize; - if (current.Text != null && char.IsDigit(current.Text[0])) + if ( !string.IsNullOrEmpty(current.Text) && char.IsDigit(current.Text[0])) { hfstring += " "; } diff --git a/src/EPPlusTest/Issues/LegacyTests/Issues.cs b/src/EPPlusTest/Issues/LegacyTests/Issues.cs index 88d4f35d8..1f8d6585b 100644 --- a/src/EPPlusTest/Issues/LegacyTests/Issues.cs +++ b/src/EPPlusTest/Issues/LegacyTests/Issues.cs @@ -44,6 +44,7 @@ Date Author Change using OfficeOpenXml.FormulaParsing.Logging; using OfficeOpenXml.Sparkline; using OfficeOpenXml.Style; +using OfficeOpenXml.Style.HeaderFooterTextFormat; using OfficeOpenXml.Table; using OfficeOpenXml.Table.PivotTable; using OfficeOpenXml.Utils.CompundDocument; @@ -6225,7 +6226,13 @@ public void i2078() { using var p = new ExcelPackage(); var ws =p.Workbook.Worksheets.Add("Sheet 1"); - ws.HeaderFooter.OddHeader.LeftAlignedText = "&12&A"; + //ws.HeaderFooter.OddHeader.LeftAlignedText = "&12&A"; + ws.HeaderFooter.OddHeader.LeftAligned.Add(new() + { + FontSize = 12, + FormatCode = ExcelHeaderFooterFormattingCodes.SheetName, + Text = "" + }); p.SaveAs("c:\\epplustest\\testoutput\\HeaderFooterIssue2078.xlsx"); } } From 3ae2c4a2647c065e4f02dd0019f0c39f9da5de91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?AdrianParn=C3=A9us?= Date: Mon, 25 Aug 2025 14:27:12 +0200 Subject: [PATCH 3/3] Added test for issue2088 --- src/EPPlusTest/Issues/LegacyTests/Issues.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/EPPlusTest/Issues/LegacyTests/Issues.cs b/src/EPPlusTest/Issues/LegacyTests/Issues.cs index 1f8d6585b..e438a6cb3 100644 --- a/src/EPPlusTest/Issues/LegacyTests/Issues.cs +++ b/src/EPPlusTest/Issues/LegacyTests/Issues.cs @@ -59,6 +59,7 @@ Date Author Change using System.Reflection; using System.Text; using System.Threading; +using static Microsoft.ApplicationInsights.MetricDimensionNames.TelemetryContext; namespace EPPlusTest { @@ -6235,5 +6236,21 @@ public void i2078() }); p.SaveAs("c:\\epplustest\\testoutput\\HeaderFooterIssue2078.xlsx"); } + + [TestMethod] + public void i2088() + { + using var p = new ExcelPackage(); + var ws = p.Workbook.Worksheets.Add("Sheet 1"); + var fh = ws.HeaderFooter.FirstHeader; + FileInfo pic = new FileInfo(Resources.GetImageFullFileName("epplusobject.png")); + var drawing = ws.HeaderFooter.FirstHeader.InsertPicture(pic, PictureAlignment.Left); + drawing.Height = 40; + + ws.HeaderFooter.FirstHeader.CenteredText = "pageTitle"; + ws.HeaderFooter.FirstFooter.LeftAlignedText = $"{ExcelHeaderFooter.CurrentDate} {ExcelHeaderFooter.CurrentTime}"; + ws.HeaderFooter.FirstFooter.RightAlignedText = $"Printing: {"user.DisplayName"} - {ExcelHeaderFooter.PageNumber}/{ExcelHeaderFooter.NumberOfPages}"; + p.SaveAs("c:\\epplustest\\testoutput\\HeaderFooterIssue2088.xlsx"); + } } }