Header and Footer in Excel Addin c#

Header and Footer in Excel Addin c#

Header & Footer:

Excel offers Headers and Footers in a worksheet. You can add headers or footers at the top or bottom of a printed worksheet in Excel. For example, you might create a footer that has page numbers, the date, and the name of your file. You can create your own, or use many built-in headers and footers provided by Excel. Let’s see how manually you can create headers and footers in Excel sheet:

On the Insert tab, in the Text group, click Header & Footer. Excel displays the worksheet in Page Layout view. To add or edit a header or footer, click the left, center, or right header or footer text box at the top or the bottom of the worksheet page (under Header, or above Footer). Let’s write some code around to automate the header and footer insertion process.

Prerequisites:

  • Visual Studio 2015 or above having Microsoft Office for Developer Tool installed
  • Create Excel Addin in C# code style (Visual Studio Tools for Office)
  • Create a ribbon designer and put button

Code example:

private void btnHeaderFooter_Click(object sender, RibbonControlEventArgs e)
{
	//Adding Workbook reference
	excel.Workbook oWorkbook = Globals.ThisAddIn.Application.ActiveWorkbook;
	//Adding active worksheet reference
	excel.Worksheet oWorksheet = oWorkbook.ActiveSheet;
	
	//Setting header footer
	oWorksheet.PageSetup.LeftHeader = "www.vbaoverall.com";
	oWorksheet.PageSetup.CenterHeader = "Page &P of &N";
	oWorksheet.PageSetup.RightHeader = "Printed &D &T";
	oWorksheet.PageSetup.LeftFooter = "www.infoextract.in";
	oWorksheet.PageSetup.CenterFooter = "header footer demo";
	oWorksheet.PageSetup.RightFooter = "This is right footer";
}

Output:

To see the output click on File Menu>> Print>> Print preview

Next : Insert Text Effects in Worksheet using Excel C#

Leave a Reply

Your email address will not be published.