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

时间:2017-11-21 20:05:00

标签: c# openxml epplus

这似乎是一个转义字符串的简单问题,但我无法找出正确的格式,我找不到包含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

如果我进入页面设置并选择自定义页脚,则对话框会将页脚信息正确格式化为& [Page] of& [Pages],如果我单击okay并让对话框关闭打印预览然后显示页脚正确。

以下是自定义页脚对话框的屏幕抓取: Custom Footer Dialog Box

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

我已经用尽了寻找帮助的想法。

任何人都可以帮忙吗?

谢谢,

1 个答案:

答案 0 :(得分:0)

如果您查看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