Set Excel Header and Footer in C#, VB.NET

Setting Excel header and footer to add information to a spreadsheet is a pretty attractive choice. This is the solution for how to set Excel header and footer in C#, VB.NET. Within this solution, you can either set Excel header and footer in C#, VB.NET at design mode or runtime mode.

Headers and Footers are the lines of text that are displayed below the top margin or above the bottom margin respectively. They can be used to display any kind of useful information on the pages like page number, author name, topic name or date/time etc.

Spire.XLS for .NET allows adding headers and footer to the worksheets and setting them at runtime. At the same time, you can also set headers and footers manually in the pre-designed file for printing and then load it as a template. For example, you can use Microsoft Excel as a GUI tool to set headers and footers easily to reduce your efforts and development time and then upload it by using Spire.XLS for .NET. The image below displays the effect of setting Excel header and footer in C#, VB.NET. You can also freely download Spire.XLS for .NET and have a trial.

Add Excel Header and Footer

The next part is the code for setting Excel header and footer in C#, VB.NET.

[C#]
using Spire.Xls;

namespace HeaderAndFooterForXls
{
    class Program
    {
        static void Main(string[] args)
        {
            //create your excel document           
            Workbook myworkbk = new Workbook();
            //Gets first worksheet
            Worksheet mysht = myworkbk.Worksheets[0];
            //Writes hello world to A1
            mysht.Range["A1"].Text = "Hello ";
            //set header&footer
            mysht.PageSetup.LeftHeader = "Header Footer Sample by E-iceblue Co.,Ltd";        
            mysht.PageSetup.CenterFooter = "footer";
            //Save workbook and preview it
            myworkbk.SaveToFile(@"..\Sample.xls");                     
            System.Diagnostics.Process.Start(@"..\Sample.xls");
        }
    }
}
[VB.NET]
Imports Spire.Xls

Namespace HeaderAndFooterForXls
	Class Program
		Private Shared Sub Main(args As String())
			'create your excel document           
			Dim myworkbk As New Workbook()
			'Gets first worksheet
			Dim mysht As Worksheet = myworkbk.Worksheets(0)
			'Writes hello world to A1
			mysht.Range("A1").Text = "Hello "
			'set header&footer
			mysht.PageSetup.LeftHeader = "Header Footer Sample by E-iceblue Co.,Ltd"
			mysht.PageSetup.CenterFooter = "footer"
			'Save workbook and preview it
			myworkbk.SaveToFile("..\Sample.xls")
			System.Diagnostics.Process.Start("..\Sample.xls")
		End Sub
	End Class
End Namespace

As for headers and footers manually in the pre-designed file, please refer how to use Excel template in C#, VB.NET.

Spire.XLS for .NET is a professional Excel component which enables developers/programmers to fast generate, read, write and modify Excel document for .NET. It supports C#, VB.NET, ASP.NET, ASP.NET MVC.