News Category

How to change the font and size for Excel header and footer in C#

2014-10-29 07:18:58 Written by  support iceblue
Rate this item
(0 votes)

Excel header and footer give additional information of an Excel sheet, such as the page number, author name, topic name and date etc. With the help of Spire.XLS, developers can easily add text header and footer into an excel spreadsheet. Sometimes, the font and size of the header and footer may be different with the font in the excel documents, and it makes the document in disorder. This article will demonstrate how to change the font and size for the text on excel header and footer.

Firstly, check the original font and size on Excel header and footer:

How to change the font and size for Excel header and footer in C#

By using Spire.XLS, we can easily set the new size and font for the header and footer text. Here comes to the steps of how to change the font and size for excel header and footer:

Step 1: Create an excel document and load the Excel with header and footer from file:

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");

Step 2: Gets the first worksheet in the Excel file

Worksheet sheet = workbook.Worksheets[0];

Step 3: Set the new font and size for the header and footer

string text = sheet.PageSetup.LeftHeader;
//"Arial Unicode MS" is font name, "18" is font size
text = "&\"Arial Unicode MS\"&18 Header Footer Sample by Spire.XLS ";
sheet.PageSetup.LeftHeader = text;
sheet.PageSetup.RightFooter = text;

Step 4: Save the document to file and preview it

workbook.SaveToFile(@"..\Result.xlsx",ExcelVersion.Version2010);
System.Diagnostics.Process.Start(@"..\Result.xlsx");

Effective screenshot after changing the font and size for the header:

How to change the font and size for Excel header and footer in C#

Full codes:

using Spire.Xls;
namespace Changefontforheaderfooter
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");
            Worksheet sheet = workbook.Worksheets[0];
            string text = sheet.PageSetup.LeftHeader;
            //"Arial Unicode MS" is font name, "18" is font size
            text = "&\"Arial Unicode MS\"&18 Header Footer Sample by Spire.XLS ";
            sheet.PageSetup.LeftHeader = text;
            sheet.PageSetup.RightFooter = text;
            //Save workbook and preview it
            workbook.SaveToFile(@"..\Result.xlsx",ExcelVersion.Version2010);
            System.Diagnostics.Process.Start(@"..\Result.xlsx");
        }
    }
}

Additional Info

  • tutorial_title: Change the font and size for Excel header and footer
Last modified on Monday, 06 September 2021 02:28