Brief introduction
We can do a lot of things with code programming for Excel worksheets, and in the following example, we'll show you how to add Excel headers and footers. In the header, we can add text, such as company name, page number, worksheet name, date, etc., can also add pictures, such as logos, tags, etc., the same as the footer. In addition, for some personalized needs, we can also set the odd and even page header footer, text formatting (such as setting the font, font size, font color, font bold, etc.) operation.
Tools used for the example:
PS: Spire.xls provides the following special scripting language to dynamically display content, such as the current page number, total pages, date, worksheet name, and so on, and can also be formatted with the text.
Sample code (for reference) 1. Insert Header Footer
"C #"
Using spire.xls;using system.drawing;using System.io;namespace insertheader_xls{class Program {static void Main (string[] args) {//Creates a Workbook, loads the document Workbook WB = new Workbook (); Wb. LoadFromFile ("test.xlsx"); Gets the first worksheet Worksheet sheet = wb. Worksheets[0]; Add text to header, footer string Text1 = sheet. Pagesetup.rightheader; String text2 = Sheet. Pagesetup.centerfooter; Set text font, font size, color, etc. text1 = "&\" Chinese amber \ "&15 & Tianfu Culture Media"; Text2 = "&\" Arial Unicode ms\ "&9 &kff0000 Note: Data interpretation rights belong to the company copyright"; Apply text to the header, footer sheet. Pagesetup.leftheader = Text1; Sheet. Pagesetup.rightfooter = Text2; Load the picture and set the picture size to image image = Image.FromFile ("Logo.png"); Bitmap Bitmap = new Bitmap (image, new Size (image). WIDTH/6, image. HEIGHT/6)); Add a picture to the right of the header of cell sheet. Pagesetup.rightheaderimage = BiTMap Sheet. Pagesetup.rightheader = "&g"; Save and open the document WB. SaveToFile ("header footer. xlsx", excelversion.version2013); System.Diagnostics.Process.Start ("header footer. xlsx"); } }}
Add Effect:
Header:
Footer:
2. Set different header and footer for odd and even pages
"C #"
Using Spire.xls;namespace insertheaderfooter2_xls{class Program {static void Main (string[] args) { Create a Workbook and get the first worksheet Workbook wb = new Workbook (); Worksheet sheet = wb. Worksheets[0]; Add text to A1 (generate first page) sheet. range["A1"]. Text = "First page"; Add text to K1 (Generate second page) sheet. range["K1"]. Text = "Second page"; Set the value of Differentoddeven to 1, which means that the header and footer sheet can be set separately for odd and even pages. Pagesetup.differentoddeven = 1; Set the odd Page header footer and set the text format sheet. pagesetup.oddheaderstring = "&\" arial\ "&12 &b &k000000 Odd _ Header"; Sheet. pagesetup.oddfooterstring = "&\" arial\ "&12 &b &k000000 Odd _ Footer"; Set the even Page header footer and set the text format sheet. pagesetup.evenheaderstring = "&\" arial\ "&12 &b &k000000 even _ header"; Sheet. pagesetup.evenfooterstring = "&\" arial\ "&12 &b &k000000 even _ Footer"; Save and open the document WB. SaveToFile ("Odd even header footer.Xlsx ", excelversion.version2013); System.Diagnostics.Process.Start ("Odd even header footer. xlsx"); } }}
Add Effect:
Header
Footer
Note : Excel headers or footers are not visible in normal browse mode, they are only visible in page layout mode or Print preview mode.
All of the above is for the C # Add Excel header, footer content.
Thanks for reading!
(If you need to reprint, please specify the source!) )
C # Insert Excel header, footer