Aspose.Cells WorkSheet.PageSetup.GetCommands() method is not returning Text

We are trying to read the Headers and Footers, but when the text contains the commands, like Page number or SheetName, we’re getting null as Text even though the Type is proper.

ws.Name is “Activity”
s being passed as &A

ws.PageSetup.GetCommands(s) is returning one item with Type as SheetName but Text as null

private String GetHeaderFooterText(Worksheet ws, String s)
{
	StringBuilder ret = new StringBuilder();
	IgnoreExceptionHelper.Do(() =>
	{
		if (s != null && ws.PageSetup.GetCommands(s) != null)
		{
			foreach (HeaderFooterCommand hfc in ws.PageSetup.GetCommands(s))
			{
				ret.Append(hfc.Text);
			}
		}
	});
	return ret.ToString();
}

Can you help us out here?

Or let us know if we have to follow different approach to achieve the expected result.

@shivanandchikkalli,
Would you like to provide sample files and runnable console projects? We will check it as soon as possible.

Aspose.Header.Footer.Issue.zip (6.0 MB)

@shivanandchikkalli,
We can’t find your sample file “testing-header-footer-issue-e4.xlsx” in the compressed package. Would you like to provide it?

testing-header-footer-issue-e4.zip (8.9 KB)

@shivanandchikkalli
If HeaderFooterCommand.Type is not HeaderFooterCommandType.Text , HeaderFooterCommand.Text should be null for Its type represents its meaning.
Please add text as the following codes:
private static String GetHeaderFooterText(Worksheet ws, String s)
{
StringBuilder ret = new StringBuilder();
if (s != null && ws.PageSetup.GetCommands(s) != null)
{
foreach (HeaderFooterCommand hfc in ws.PageSetup.GetCommands(s))
{
switch(hfc.Type)
{
case HeaderFooterCommandType.Text:
ret.Append(hfc.Text);
break;
case HeaderFooterCommandType.SheetName:
ret.Append(ws.Name);
break;
case HeaderFooterCommandType.FileName:
string f = ws.Workbook.FileName;
if(!string.IsNullOrEmpty(f))
{
ret.Append(Path.GetFileName(f));
}
else
{
ret.Append(“Book1.xlsx”);
}
break;
//…
}

                }
            }
            return ret.ToString();
        }

@shivanandchikkalli
We can not simply return text for some command types of header and footer.: such as CurrentPage, Pagecount, Picture.

Thanks for the quick response. @simon.zhao and @John.He

@shivanandchikkalli,

You are welcome.