在使用C#.NET生成的Excel表格中显示页码

有没有人有一个想法,如何包括或input在使用C#代码生成的Excel表中的页码。

我使用Microsoft.Office.Interop.Excel中提供的库来生成文件。

但是在默认的输出中,我看不到页码。 我知道要启用此通过

Excel的选项(查看 – >页眉和页脚…),但我想通过C#自动化。

这是可能的,如果是的,请分享相同的片段。

感谢不断学习者

如果我不知道如何在Office中编写代码,我将其操作logging为macros,然后在内置的Visual Basic编辑器中查看生成的代码。 这是它添加页脚页码所产生的相关代码:

ActiveSheet.PageSetup.CenterFooter = "Page &P of &N" 

LeftFooter和RightFooter也可用。

我遇到的问题是input以下内容,这是手动添加Excel时如何显示的;

  ws.PageSetup.CenterFooter = "&[Pages]/&[Pages]"; // This did not work 

这不起作用,但是下面做了;

  ws.PageSetup.CenterFooter = "&P/&N"; // This worked correctly 

input文件名和date时发现相同。

  ws.PageSetup.LeftHeader = "&[File]"; // This did not work ws.PageSetup.RightHeader = "&[Date]"; // This did not work ws.PageSetup.LeftHeader = "&F"; // This worked correctly ws.PageSetup.RightHeader = "&D"; // This worked correctly 

希望这有助于如果你尝试了我第一次尝试的方法。