Search code examples
c#excelheaderautomationfooter

Excel 2013 header and footer is dropping the ampersand and bracket when automated


I have a program that generates reports in Excel 2013. Code snippet looks something like this:

Worksheet sheet = Excel.Workbooks.Add();
string[] footer = new string[] { @"&[Tab]", ProgramName, @"&[Date]" };

sheet.PageSetup.LeftFooter = footer[0];
sheet.PageSetup.CenterFooter = footer[1];
sheet.PageSetup.RightFooter = footer[2];

When I close and reopen the workbook it looks like:

Tab] ------------------------- PROGRAM ------------------------- Date]

Grrr, insert image is not working and I can't have multiple adjacent white space (just imagine it without the hyphens)

Important: If I immediately try to print the worksheet, the document looks as shown above. When I simply click in the footer area to edit the footer, it immediately realizes that the value is &[Date]. When the editor leaves focus, the footer correctly displays 4/7/2016.

So my question is why is this happening? Why/how is Excel getting confused? Has anyone seen this behavior or anything similar?

Just before I posted, I thought it might be because my Excel application is invisible, but no.

Thanks.


Solution

  • Not sure about your language, but in VBA the code would be, for date, "&D". Using "&[Date]" results in similar problems to what you write –