How to set or format a Header/Footer?

25 May 20232 minutes to read

Script commands are used to set header/ footer formatting. The following code snippet illustrate this. For more information on formatting the string, see Inserting and Formatting Text in Headers and Footers

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Format the header
  worksheet.PageSetup.CenterHeader = @"&""Gothic,bold""Center Header Text";

  FileStream stream = new FileStream("HeaderFormat.xlsx", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  workbook.Close();
  excelEngine.Dispose();
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Format the header
  worksheet.PageSetup.CenterHeader = @"&""Gothic,bold""Center Header Text";
  workbook.SaveAs("HeaderFormat.xlsx");
}
Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Format the header
  worksheet.PageSetup.CenterHeader = "&""Gothic,bold""Center Header Text"
  workbook.SaveAs("HeaderFormat.xlsx")
End Using

NOTE

Go to “ View -> Page Layout” option to view the header and footer in Microsoft Excel.

See Also