Skip to content

Headers/Footers not working properly after updating EPPlus 8.1.0 #2078

@CyberBotX

Description

@CyberBotX

EPPlus usage

Personal use

Environment

Windows

Epplus version

8.1.0

Spreadsheet application

LibreOffice

Description

Today, I updated my EPPlus to 8.1.0 and found that the changes made to headers and footers have made my code that I've been using forever no longer work. I'd normally set a left header like this:

ws.HeaderFooter.OddHeader.LeftAlignedText = "&12&A";

Now, EPPlus 8.1.0 will throw an exception when it gets around to calling WriteHeaderFooterFormat(), and digging into the code with a debugger shows that it seems to be that the Text property of the 2nd ExcelHeaderFooterTextItem that was added is null, and that makes ParseToHeaderFooterFormat(current, prev) throw an exception because it tries to get current.Text[0], specifically a NullReferenceException is what is thrown.

If I use:

ws.HeaderFooter.OddHeader.LeftAligned.Text = "&12&A";

I get no exception but I get a file that claims it has no header enabled (LibreOffice in particular says the Header is off) and it doesn't seem to even be there at all even if I do enable the header. I can see that the 1st (and only) item in the collection on LeftAligned does have a FormatCode of Text and the text is exactly what I gave.

I've also tried:

ws.HeaderFooter.OddHeader.LeftAligned[0].FontSize = 12;
ws.HeaderFooter.OddHeader.LeftAligned[0].FormatCode = ExcelHeaderFooterFormattingCodes.SheetName;

This doesn't throw an exception either, it keeps the Text as "&L", but the same problem of it thinking there is no header persists.

If I try to do:

ws.HeaderFooter.OddHeader.LeftAligned.Add(new()
{
	FontSize = 12,
	FormatCode = ExcelHeaderFooterFormattingCodes.SheetName,
	Text = ""
});

I get an exception, this time IndexOutOfRangeException, also still in ParseToHeaderFooterFormat(current, prev), this time because of an empty string. If I don't include the Text field in this one, I get the same NullReferenceException as earlier. And finally, if I include any text in the Text field, everything at that point finally seems to work (and the text seems to be ignored).

Overall, I'd prefer if either the way I showed at the start still worked as expected, or even the second form if the first is something that might get deprecated in the future. While I understand that the last one is most likely going to be the path going forward, I do not currently wish to increase the complexity of my program.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Status

Backlog

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions