Saving Excel file crashes when text is added to the Page headers
I require filling out the 3 sections of the page headers, Left, Center and Right with information that needs printing on every page. Adding any form of text to this area of the worksheet results in an error.
The lines of code which is giving me this problem are as follows: ws.PageSetup.Header.Left.AddText("&"Arial,Bold"&10 " + ourDay + "\n" + Convert.ToDateTime(ourDayset.Rows[0]["StartDate"]).ToString("d MMMM") + " to " + Convert.ToDateTime(ourDayset.Rows[0]["StartDate"]).ToString("d MMMM yyyy"), XLHFOccurrence.AllPages); ws.PageSetup.Header.Center.AddText("&"Calibri,Regular"&10 OFFICIAL\n" + Convert.ToDateTime(ourDayset.Rows[0]["StartDate"]).ToString("d MMMM") + " to " + Convert.ToDateTime(ourDayset.Rows[0]["StartDate"]).ToString("d MMMM yyyy"), XLHFOccurrence.AllPages); ws.PageSetup.Header.Center.AddText("&"Arial,Bold"&12 " + (string)ourDayset.Rows[0]["TableID"] + " - " + MainWindow.CurrentTimetable, XLHFOccurrence.AllPages); ws.PageSetup.Header.Right.AddText("&"Arial,Bold"&12 " + (string)ourDayset.Rows[0]["RouteDescription"], XLHFOccurrence.AllPages);
Even adding simple text as follows results in the same error: ws.PageSetup.Header.Center.AddText("Simple Text", XLHFOccurrence.AllPages);
The text I think is being added without issue, it only appears when I attempt to save the Excel file.
Here is the error:
System.ArgumentOutOfRangeException HResult=0x80131502 Message=Specified argument was out of the range of valid values. Arg_ParamName_Name Source=ClosedXML StackTrace: at ClosedXML.Excel.XLHeaderFooter.GetText(XLHFOccurrence occurrence) at ClosedXML.Excel.IO.WorksheetPartWriter.GetWorksheetDom(Boolean partIsEmpty, WorksheetPart worksheetPart, XLWorksheet xlWorksheet, SaveOptions options, SaveContext context) at ClosedXML.Excel.IO.WorksheetPartWriter.GenerateWorksheetPartContent(Boolean partIsEmpty, WorksheetPart worksheetPart, XLWorksheet xlWorksheet, SaveOptions options, SaveContext context) at ClosedXML.Excel.XLWorkbook.CreateParts(SpreadsheetDocument document, SaveOptions options) at ClosedXML.Excel.XLWorkbook.CreatePackage(String filePath, SpreadsheetDocumentType spreadsheetDocumentType, SaveOptions options) at ClosedXML.Excel.XLWorkbook.SaveAs(String file, SaveOptions options) at ClosedXML.Excel.XLWorkbook.SaveAs(String file, Boolean validate, Boolean evaluateFormulae) at ClosedXML.Excel.XLWorkbook.SaveAs(String file) at Trainset_V5.UserControls._WorkingTimetableDaysets.exportSelectedDaysetToExcelToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Users\sean\Documents\Visual Studio 2022\Projects\RailSmart - TrainSet\UserControls_WorkingTimetableDaysets.cs:line 600 at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e) at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ToolStrip.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(HWND hWnd, MessageId msg, WPARAM wparam, LPARAM lparam)