With the help of Spire.XLS, we could easily add header and footer to the excel page; we can also set different Header and Footer for Odd and Even Pages in Excel. This article will demonstrate how to add different header & footer for the first page on the excel worksheet.
Here comes to the code snippets of how to set different header and footer for the first page:
Step 1: Initialize an instance of Workbook and get the first worksheet.
Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0];
Step 2: Set the value of DifferentFirst as 1, which indicates that headers/footers for first page are different from the other pages.
Step 3: Set the header and footer for the first page.
sheet.PageSetup.FirstHeaderString = "Different First page"; sheet.PageSetup.FirstFooterString = "Different First footer";
Step 4: Set the other pages' header and footer. If we don't need to set the header and footer for other pages, we can ignore this step.
sheet.PageSetup.LeftHeader = "Demo of Spire.XLS"; sheet.PageSetup.CenterFooter = "Footer by Spire.XLS";
Step 5: Save the document to file.
workbook.SaveToFile("Result.xlsx", FileFormat.Version2010);
Effective screenshot:
Full codes:
using Spire.Xls; namespace SetDifferentHeaderorFooter { class Program { static void Main(string[] args) { Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; sheet.PageSetup.DifferentFirst = 1; sheet.PageSetup.FirstHeaderString = "Different First page"; sheet.PageSetup.FirstFooterString = "Different First footer"; sheet.PageSetup.LeftHeader = "Demo of Spire.XLS"; sheet.PageSetup.CenterFooter = "Footer by Spire.XLS"; workbook.SaveToFile("result.xlsx", FileFormat.Version2010); } } }