If you are a frequent user of working with Excel workbooks, you must be familiar with adding Headers and Footers to your file. As the name indicates, the Header in an Excel file represents the repetitive information added to the top of a file, whereas the Footer is used to add information to the bottom of each page in the file. Header and footer information is repeated on each page of the file. As a .NET application developer, you can add header and footer information to an Excel spreadsheet using NPOI API with C# or VB.NET as explained in this article.
In our series of articles on working with NPOI in .NET, you can find examples of:
- Creating, saving, and loading an Excel file using NPOI in C#
- Adding a Comment to an Excel file using NPOI in C#
- Adding a Formula to an Excel file using NPOI in C#
In this article, we’ll show how to add a header and footer to an Excel file with NPOI using C#/VB.NET. But first, let us have a look at how to add a Header and Footer to a Workbook using Microsoft Excel.
Insert Header and Footer in Spreadsheet using Microsoft Excel
Microsoft Excel allows adding header and footer information to a workbook easily. You can add different information to the header and footers of a file such as page numbers, date, time, file name, or any other customized text.
Steps to Insert Header and Footer in Spreadsheet with Microsoft Excel
If you want to add a header and footer to an Excel workbook, you can use the following steps:
- In Microsoft Excel, go to Insert Menu
- Select Header & Footer option. This should open the Header and Footer sections of the existing spreadsheet.
- Add desired information in these sections
You should be able to see the header and footer information on each page in the workbook.
Add Header and Footer to Excel using NPOI in C#
Now that we have seen how to add header and footer to a spreadsheet file using Microsoft Excel, let’s have a look at how you can do the same using the NPOI in C#. But before that, you need to install NPOI in your .NET project to get started. You can learn about this in our detailed instructions guide for Installing NPOI for .NET.
Add Header and Footer in Excel Spreadsheet using C#
Now that your development environment is ready for working with NPOI API in C#, create a console-based project and add the following lines of code.
//Load workbook
IWorkbook wb = new XSSFWorkbook(new FileStream("Input.xlsx", FileMode.Open));
//Get the first worksheet
ISheet ws = wb.GetSheetAt(0);
//Set header
IHeader header = ws.Header;
header.Center = "FileFormat.com! Your knowledgebase for File Formats!";
//Set footer
IFooter footer = ws.Footer;
footer.Center = "FileFormat.com";
//Save the file
FileStream file = File.Create("HeaderFooter.xlsx");
wb.Write(file, false);
file.Close();
This will generate an output HeaderFooter.xlsx file that contains the Header and Footer information.
Conclusion
In this article, we showed how to add Header and Footer information in an Excel workbook using NPOI API. The sample code is written in C#, but you can also re-write it with VB.NET and it should work equally. If you would like to know more about working with Excel workbooks using NPOI, stay tuned for more examples in this section.