We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: XlsIO |
Published Date: April 30, 2011 |
Last Revised Date: June 13, 2019

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.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Austin Ford
Dec 30, 2017
Would it be possible to add comments to the code here to display what each of the Strings does to the header/footer?  Are there any more of these preformatted strings that can be used?

Reply
Mohan Chandran [Syncfusion]
Jan 03, 2018

Hi Austin,

 

Query

Answer

Would it be possible to add comments to the code here to display what each of the Strings does to the header/footer?

Yes, we have added the comments to each format string code.

Are there any more of these preformatted strings that can be used?

Please find the details of the preformatted strings used in this KB and other formatting strings from the following link.

https://msdn.microsoft.com/en-us/library/bb225426(v=office.12).aspx

 

Regards,

Mohan Chandran.

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon