Is it possible to conditionally place a header onto an excel page?
@tkurian
Excel does not support setting conditions on the header/footer. But through Aspose.Cells, you can set different header/footers in the program by using conditional statements.
The sample code as follows:
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Adding a new worksheet to the Workbook object
WorksheetCollection worksheets = workbook.Worksheets;
Worksheet worksheet = worksheets.Add("My Worksheet");
//Obtaining the reference of the PageSetup of the worksheet
PageSetup pageSetup = worksheet.PageSetup;
//Setting worksheet name at the left header
pageSetup.SetHeader(0, "&A");
// Based on your logic, set the conditions to determine
bool condtional = false;
if (condtional)
{
//Setting current date and current time at the central header and changing the font of the header
pageSetup.SetHeader(1, "&\"Times New Roman,Bold\"&D-&T");
}
else
{
//Setting current date at the central header and changing the font of the header
pageSetup.SetHeader(1, "&\"Times New Roman,Bold\"&D");
}
//Setting current file name at the right header and changing the font of the header
pageSetup.SetHeader(2, "&\"Times New Roman,Bold\"&12&F");
//Setting a string at the left footer and changing the font of the footer
pageSetup.SetFooter(0, "Hello World! &\"Courier New\"&14 123");
//Setting current date and current time at the central footer
pageSetup.SetFooter(1, "&\"Times New Roman,Bold\"&D-&T");
workbook.Save(filePath + "headerfooter.xlsx");
Hope helps a bit.