Articles in this section
Category / Section

Add Excel headers and footers in C#, VB.NET

3 mins read

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can add Excel headers and footers in C#, VB.NET.

What are headers and footers in Excel?

Header in Excel

It is a section of the worksheet that appears at the top of each page in the Excel worksheet. Header remains constant across all the pages.

Footer in Excel

It is a section of the worksheet that appears at the bottom of each page in the Excel worksheet. Footer remains constant across all the pages.

Purpose of headers and footers in Excel

You can include a header or footer on each page of your worksheet to make your printed Excel documents look more stylish and professional. Generally, header or footer contains basic information about the spreadsheet such as page number, current date, workbook name, file path, etc.

Header or footer can be displayed only on printed pages, in Print Preview and Page Layout view. In the normal worksheet view, they are not visible.

Steps to add Excel headers and footers, programmatically:

Step 1: Create a new C# console application project.

Create new console application project

Create a new C# console application

Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.

Install NuGet package

Install NuGet package

Step 3: Include the following namespace in the Program.cs file.

C#

using Syncfusion.XlsIO;

 

VB.NET

Imports Syncfusion.XlsIO

 

Step 4: Use the following code snippet to add Excel headers and footers in C#, VB.NET.

C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
    //Instantiate the application object
    IApplication application = excelEngine.Excel;
 
    //Open a sample workbook
    IWorkbook workbook = excelEngine.Excel.Workbooks.Open(@"../../Data/Sample.xlsx");
                
    //The first worksheet object in the worksheets collection is accessed
    IWorksheet worksheet = workbook.Worksheets[0];
            
    //Create headers
    //Add page headers with page number, current time and current date
    worksheet.PageSetup.CenterHeader = "&P";
    worksheet.PageSetup.LeftHeader = "&T"; 
    worksheet.PageSetup.RightHeader = "&D"; 
                
    //Create footers
    //Add page footers with name of the document, name of the workbook tab and file path
    worksheet.PageSetup.CenterFooter = "&F";                
    worksheet.PageSetup.LeftFooter = "&A";
    worksheet.PageSetup.RightFooter = "&Z";
                
    //Save the file
    workbook.SaveAs("D://temp/Output.xlsx");
}

 

VB.NET

Using excelEngine As ExcelEngine = New ExcelEngine()
    'Instantiate the application object
    Dim application As IApplication = excelEngine.Excel
 
    'Open a sample workbook
    Dim workbook As IWorkbook = excelEngine.Excel.Workbooks.Open("../../Data/Sample.xlsx")
 
    'The first worksheet object in the worksheets collection is accessed
    Dim sheet As IWorksheet = workbook.Worksheets(0)
 
    'Create headers
    'Add page headers with page number, current time and current date
    sheet.PageSetup.CenterHeader = "&P"
    sheet.PageSetup.LeftHeader = "&T"
    sheet.PageSetup.RightHeader = "&D"
 
    'Create footers
    'Add page footers with name of the document, name of the workbook tab and file path
    sheet.PageSetup.CenterFooter = "&F"
    sheet.PageSetup.LeftFooter = "&A"
    sheet.PageSetup.RightFooter = "&Z"
 
    'Save the file
    workbook.SaveAs("Output.xlsx")
End Using

 

Strings that the header or footer takes are script commands used to set header or footer formatting. For more information on formatting the string, see Inserting and Formatting Text in Headers and Footers.

A complete Windows Forms working example of how to add Excel headers and footers in C# and VB can be downloaded from Add Excel Headers And Footers.zip.

By executing the program, you will get the output Excel file as shown below.

Output Excel document

Output Excel document

Refer here to explore the rich set of Syncfusion Excel (XlsIO) library features.

See Also:

Add image to Excel header in C#, VB.NET

How do I keep the headers visible while scrolling down through data?

Can we set font color for Footer through XlsIO?

How to get column names of a named range with column headers in C#, VB.NET?

How to set Freeze panes in Excel using C#, VB.NET?

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied