使用 OpenXML 生成电子表格并尝试让 &[Page] of &[Pages] 在自定义页脚中工作

Using OpenXML to generate a spreadsheet and trying to get &[Page] of &[Pages] working in a custom footer

这似乎是转义字符串的一个简单问题,但我无法找出正确的格式,也找不到包含在 Excel 页脚中使用字段示例的文档。

有问题的属性是:

xlSheet.HeaderFooter.OddFooter.LeftAlignedText = "&[Page] of &[Pages]";

下面是为附加上下文生成工作表的较大代码片段:

 using (ExcelPackage pckExport = new ExcelPackage())
            {
                ExcelWorksheet xlSheet = pckExport.Workbook.Worksheets.Add(SystemEnum.GetEnumDescription(typeof(SystemEnum.Reports), SystemEnum.Reports.GroomingCalendarReport.GetHashCode()));
                try
                {
                    int miRow = 1;
                    string pageHeader = "Calendar Report";
                    xlSheet.Cells.Style.Font.Size = 9;
                    xlSheet.Cells.Style.Font.Name = "Times New Roman";
                    DateTime reportDate = Convert.ToDateTime(rptDate);
                    ////set the report header
                    xlSheet.HeaderFooter.OddHeader.CenteredText = "&\"Times New Roman,Bold\"&12" + "\n" + pageHeader + "\n" + " &11for&12 " + reportDate.Date.DayOfWeek.ToString() + " , " + reportDate +
                                                                   "\n" + Location;
                    xlSheet.HeaderFooter.OddFooter.LeftAlignedText = "&[Page] of &[Pages]";
                    xlSheet.HeaderFooter.OddFooter.RightAlignedText = DateTime.Now.ToShortDateString();

生成并打开电子表格并导航到 File\Print 菜单后,您会看到页脚不太正确:

这是页脚的屏幕截图: Page of Pages

如果我转到页面设置和 select 自定义页脚,对话框会显示页脚信息的格式正确为 &[Page] of &[Pages],如果我单击确定并让对话框关闭然后打印预览会正确显示页脚。

这是自定义页脚对话框的屏幕截图: Custom Footer Dialog Box

我尝试使用显式字符串并使用 &[Page],但它们在预览时都是 return Pages 的 [Page],并且完全是自定义页脚框中发送的文本。

我已经用尽了所有的想法来寻求这方面的帮助。

有人能帮忙吗?

谢谢,

如果您查看 ExcelHeaderFooter 对象,您会看到一大堆非常有用的 const 来帮助您完成您想要的事情:

var wb = pck.Workbook;
var ws = wb.Worksheets.Add("Sheet1");
ws.Cells[1, 1].Value = "Test";

var footer = $"Page {ExcelHeaderFooter.PageNumber} of {ExcelHeaderFooter.NumberOfPages}";
ws.HeaderFooter.EvenFooter.CenteredText = footer;
ws.HeaderFooter.OddFooter.CenteredText = footer;

以下是 4.1 版列出的内容:

#region Static Properties
/// <summary>
/// The code for "current page #"
/// </summary>
public const string PageNumber = @"&P";
/// <summary>
/// The code for "total pages"
/// </summary>
public const string NumberOfPages = @"&N";
/// <summary>
/// The code for "text font color"
/// RGB Color is specified as RRGGBB
/// Theme Color is specified as TTSNN where TT is the theme color Id, S is either "+" or "-" of the tint/shade value, NN is the tint/shade value.
/// </summary>
public const string FontColor = @"&K";
/// <summary>
/// The code for "sheet tab name"
/// </summary>
public const string SheetName = @"&A";
/// <summary>
/// The code for "this workbook's file path"
/// </summary>
public const string FilePath = @"&Z";
/// <summary>
/// The code for "this workbook's file name"
/// </summary>
public const string FileName = @"&F";
/// <summary>
/// The code for "date"
/// </summary>
public const string CurrentDate = @"&D";
/// <summary>
/// The code for "time"
/// </summary>
public const string CurrentTime = @"&T";
/// <summary>
/// The code for "picture as background"
/// </summary>
public const string Image = @"&G";
/// <summary>
/// The code for "outline style"
/// </summary>
public const string OutlineStyle = @"&O";
/// <summary>
/// The code for "shadow style"
/// </summary>
public const string ShadowStyle = @"&H";
#endregion