How to add WinForms XlsIO headers and footers in C#, VB.NET?
Syncfusion® Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It also converts Excel documents to PDF files. Using this library, you can add Excel headers and footers in C# and VB.NET on our WinForms XlsIO feature tour page.
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. The 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. The 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, headers or footers contain basic information about the spreadsheet, such as page number, current date, workbook name, file path, etc.
Headers or footers 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 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
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.NET 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
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?
Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.
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 to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to add WinForms XlsIO headers and footers in C# and VB.NET.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!