Header and Footer (6)
How to add different header & footer for the first page
2016-12-20 06:41:53 Written by support iceblueWith the help of Spire.XLS, we could easily add header and footer to the excel page; we can also set different Header and Footer for Odd and Even Pages in Excel. This article will demonstrate how to add different header & footer for the first page on the excel worksheet.
Here comes to the code snippets of how to set different header and footer for the first page:
Step 1: Initialize an instance of Workbook and get the first worksheet.
Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0];
Step 2: Set the value of DifferentFirst as 1, which indicates that headers/footers for first page are different from the other pages.
Step 3: Set the header and footer for the first page.
sheet.PageSetup.FirstHeaderString = "Different First page"; sheet.PageSetup.FirstFooterString = "Different First footer";
Step 4: Set the other pages' header and footer. If we don't need to set the header and footer for other pages, we can ignore this step.
sheet.PageSetup.LeftHeader = "Demo of Spire.XLS"; sheet.PageSetup.CenterFooter = "Footer by Spire.XLS";
Step 5: Save the document to file.
workbook.SaveToFile("Result.xlsx", FileFormat.Version2010);
Effective screenshot:
Full codes:
using Spire.Xls; namespace SetDifferentHeaderorFooter { class Program { static void Main(string[] args) { Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; sheet.PageSetup.DifferentFirst = 1; sheet.PageSetup.FirstHeaderString = "Different First page"; sheet.PageSetup.FirstFooterString = "Different First footer"; sheet.PageSetup.LeftHeader = "Demo of Spire.XLS"; sheet.PageSetup.CenterFooter = "Footer by Spire.XLS"; workbook.SaveToFile("result.xlsx", FileFormat.Version2010); } } }
Set Different Header or Footer for Odd and Even Pages in Excel in C#, VB.NET
2016-09-22 00:59:10 Written by support iceblueHeaders or footers are added to a worksheet to deliver some extra information about each page. By default, the headers or footers on odd and even pages are the same. However, we are able to set different headers or footers for odd and even pages to display different information.
Following sections demonstrate how to create different odd and even page headers/footers using Spire.XLS.
Code Snippet:
Step 1: Initialize an instance of Workbook and get the first worksheet.
Workbook wb = new Workbook(); Worksheet sheet = wb.Worksheets[0];
Step 2: Set the value of DifferentOddEven as 1, which indicates that headers/footers for odd and even pages are different.
sheet.PageSetup.DifferentOddEven = 1;
Step 3: Set header and footer string for odd pages, and format the string.
sheet.PageSetup.OddHeaderString = "&\"Arial\"&12&B&KFFC000Odd_Header"; sheet.PageSetup.OddFooterString = "&\"Arial\"&12&B&KFFC000Odd_Footer";
Step 4: Set different header and footer string for even pages, and format the string with different color.
sheet.PageSetup.EvenHeaderString = "&\"Arial\"&12&B&KFF0000Even_Header"; sheet.PageSetup.EvenFooterString = "&\"Arial\"&12&B&KFF0000Even_Footer";
Step 5: Save the file.
wb.SaveToFile("OddEvenHeaderFooter.xlsx", ExcelVersion.Version2013);
Output:
Full Code:
using Spire.Xls; namespace SetDifferentHeaderorFooter { class Program { static void Main(string[] args) { Workbook wb = new Workbook(); Worksheet sheet = wb.Worksheets[0]; sheet.Range["A1"].Text = "Page 1"; sheet.Range["J1"].Text = "Page 2"; sheet.PageSetup.DifferentOddEven = 1; sheet.PageSetup.OddHeaderString = "&\"Arial\"&12&B&KFFC000Odd_Header"; sheet.PageSetup.OddFooterString = "&\"Arial\"&12&B&KFFC000Odd_Footer"; sheet.PageSetup.EvenHeaderString = "&\"Arial\"&12&B&KFF0000Even_Header"; sheet.PageSetup.EvenFooterString = "&\"Arial\"&12&B&KFF0000Even_Footer"; wb.SaveToFile("OddEvenHeaderFooter.xlsx", ExcelVersion.Version2013); } } }
Imports Spire.Xls Namespace SetDifferentHeaderorFooter Class Program Private Shared Sub Main(args As String()) Dim wb As New Workbook() Dim sheet As Worksheet = wb.Worksheets(0) sheet.Range("A1").Text = "Page 1" sheet.Range("J1").Text = "Page 2" sheet.PageSetup.DifferentOddEven = 1 sheet.PageSetup.OddHeaderString = "&""Arial""&12&B&KFFC000Odd_Header" sheet.PageSetup.OddFooterString = "&""Arial""&12&B&KFFC000Odd_Footer" sheet.PageSetup.EvenHeaderString = "&""Arial""&12&B&KFF0000Even_Header" sheet.PageSetup.EvenFooterString = "&""Arial""&12&B&KFF0000Even_Footer" wb.SaveToFile("OddEvenHeaderFooter.xlsx", ExcelVersion.Version2013) End Sub End Class End Namespace
Adding a watermark to an Excel spreadsheet can be a useful way to brand your documents or indicate confidentiality. MS Excel does not provide a built-in feature to insert a watermark, however, there are ways to mimic the watermark effect by inserting an image into the header or footer of your Excel worksheet, or setting an image as the background of your spreadsheet.
In this article, you will learn how to add a header or background image watermark to Excel in C# using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Header vs. Background Image Watermark
Header Image Watermark
Advantages:
- The watermark is preserved on the printed sheet, ensuring it appears in the final output.
Disadvantages:
- The watermark is invisible under the "Normal" view mode in Excel, only becoming visible in "Page Layout" or "Page Break Preview" views.
- To centrally position the watermark graphic on the Excel page, you need to carefully adjust the white margins, especially on the top and left sides of the image.
Background Image Watermark
Advantages:
- The watermark image covers the entire worksheet area, providing a consistent background appearance.
Disadvantages:
- The watermark is not preserved on the printed sheet, meaning it will not appear in the final printed output.
Add a Watermark to Excel Using a Header Image in C#
Spire.XLS for .NET provides the PageSetup class, which allows you to control various settings related to the appearance and layout of the printed worksheet. This class includes the CenterHeader and CenterHeaderImage properties, which enable you to set an image for the center section of the header.
Below are the steps to add a watermark to Excel using a header image in C#.
- Create a Workbook object.
- Load an Excel document from a give file path.
- Load an image using Image.FromFile() method.
- Get a specific worksheet from the workbook.
- Add an image field to the header center by setting Worksheet.PageSetup.CenterHeader property to "&G".
- Apply the image to the header center through Worksheet.PageSetup.CenterHeaderImage property.
- Save the workbook to a different Excel file.
- C#
using Spire.Xls; using System.Drawing; namespace AddWatermarkToExcelUsingHeader { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx"); // Load an image file Image image = Image.FromFile("C:\\Users\\Administrator\\Desktop\\confidential.png"); // Loop through all worksheets in the file for (int i = 0; i < workbook.Worksheets.Count; i++) { // Get a specific worksheet Worksheet worksheet = workbook.Worksheets[i]; // Add an image field to the header center worksheet.PageSetup.CenterHeader = "&G"; // Add the image to the header center worksheet.PageSetup.CenterHeaderImage = image; } // Save the result file workbook.SaveToFile("AddWatermark.xlsx", ExcelVersion.Version2016); // Dispose resources workbook.Dispose(); } } }
Add a Watermark to Excel Using a Background Image in C#
The PageSetup class also includes a property named BackgroundImage, which allows you to get or set the image used for the worksheet background.
Here are the steps to add a watermark to Excel using a background image in C#.
- Create a Workbook object.
- Load an Excel document from a give file path.
- Load an image file and convert it into a Bitmap image.
- Get a specific worksheet from the workbook.
- Apply the image to the worksheet as the background through Worksheet.PageSetup.BackgroundImage property.
- Save the workbook to a different Excel file.
- C#
using Spire.Xls; using System.Drawing; namespace AddWatermarkToExcelUsingBackground { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx"); // Load an image file Bitmap bitmapImage = new Bitmap(Image.FromFile("C:\\Users\\Administrator\\Desktop\\sample.png")); // Loop through all worksheets in the file for (int i = 0; i < workbook.Worksheets.Count; i++) { // Get a specific worksheet Worksheet worksheet = workbook.Worksheets[i]; // Set the image as the background of the worksheet worksheet.PageSetup.BackgoundImage = bitmapImage; } // Save the result file workbook.SaveToFile("AddWatermark.xlsx", ExcelVersion.Version2016); // Dispose resources workbook.Dispose(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
How to change the font and size for Excel header and footer in C#
2014-10-29 07:18:58 Written by support iceblueExcel 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:
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:
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"); } } }
Excel header and footer gives additional information of a spreadsheet. And it can be in text and image. We have already shows you the text header and footer in excel worksheet, this guide focuses on introducing how to insert image header and footer for Excel files in C# by using Spire.XLS for .NET.
Firstly, Download Spire.XLS for .NET (or Spire.Office for .NET) and install it on your system. The Spire.XLS installation is clean, professional and wrapped up in a MSI installer.
Then, adds Spire.XLS.dll as reference in the downloaded Bin folder thought the below path: "..\Spire.XLS\Bin\NET4.0\ Spire.XLS.dll".
Now it comes to the details of how to add image header and footer in C#:
Step 1: Create a new excel document and load from the file.
Workbook workbook = new Workbook(); workbook.LoadFromFile(@"..\..\XLS1.xlsx");
Step 2: Get a first worksheet.
Worksheet sheet = workbook.Worksheets[0];
Step 3: Load the image and set header and footer style.
Image image = Image.FromFile(@"..\..\logo.png"); //Set Header format sheet.PageSetup.LeftHeaderImage = image; sheet.PageSetup.LeftHeader = "&G"; //Set Footer format sheet.PageSetup.CenterFooterImage = image; sheet.PageSetup.CenterFooter = "&G";
Step 4: Save the document to file.
workbook.SaveToFile(@"..\..\result.xlsx", ExcelVersion.Version2010);
Effected screenshot:
The full codes:
using Spire.Xls; using System.Drawing; namespace AddImageHeaderandFooter { class Program { static void Main(string[] args) { Workbook workbook = new Workbook(); workbook.LoadFromFile(@"..\..\XLS1.xlsx"); Worksheet sheet = workbook.Worksheets[0]; Image image = Image.FromFile(@"..\..\logo.png"); sheet.PageSetup.LeftHeaderImage = image; sheet.PageSetup.LeftHeader = "&G"; sheet.PageSetup.CenterFooterImage = image; sheet.PageSetup.CenterFooter = "&G"; workbook.SaveToFile(@"..\..\result.xlsx", ExcelVersion.Version2010); } } }
In Excel files, headers and footers provide rich content at the top and bottom of worksheet pages. The content can include a logo, company name, page number, date, and time, etc. This article shows you how to add text, images, as well as fields (like page numbers) to Excel headers or footers using Spire.XLS for .NET.
- Add an Image and Formatted Text to Excel Header
- Add the Current Date and Page Numbers to Excel Footer
Spire.XLS for .NET provides the PageSetup class to work with the page setup in Excel including headers and footers. Specifically, it contains LeftHeader property, CenterHeader property, RightHeader property, LeftFooter property, etc. to represent the left section, center section and right section of a header or footer. To add fields to headers or footers, or to apply formatting to text, you’ll need to use the scripts listed in the following table.
Script | Description |
&P | The current page numbers. |
&N | The total number of pages. |
&D | The current data. |
&T | The current time. |
&G | A picture. |
&A | The worksheet name. |
&F | The file name. |
&B | Make text bold. |
&I | Italicize text. |
&U | Underline text. |
&"font name" | Represents a font name, for example, &"Arial". |
& + Integer | Represents font size, for example, &12. |
&K + Hex color code | Represents font color, for example, &KFF0000. |
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Add an Image and Formatted Text to Excel Header
The following are the steps to add an image and formatted text to the header of an existing Excel document using Spire.XLS for .NET.
- Create a Workbook object, and load a sample Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through the Workbook.Worksheets[index] property.
- Load an image, scale it and set it as the image source of the left header through the PageSetup.LeftHeaderImage property.
- Display image in the left header section by setting the PageSetup.LeftHeader property to “&G”.
- Save the workbook to another Excel file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls; using System.Drawing; namespace AddImageAndTextToHeader { class Program { static void Main(string[] args) { //Create a Workbook object Workbook wb = new Workbook(); //Load an existing Excel file wb.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx"); //Get the first worksheet Worksheet sheet = wb.Worksheets[0]; //Load an image Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\your-logo.png"); //Scale the image Bitmap bitmap = new Bitmap(image, new Size(image.Width / 2, image.Height / 2)); //Add image to header’s left section sheet.PageSetup.LeftHeaderImage = bitmap; sheet.PageSetup.LeftHeader = "&G"; //Add formatted text to header’s right section sheet.PageSetup.RightHeader = "&\"Calibri\"&B&10&K4253E2Your Company Name \n Goes Here"; //Save the file wb.SaveToFile("Header.xlsx", ExcelVersion.Version2016); } } }
Add the Current Date and Page Numbers to Excel Footer
The following are the steps to add the current date and page numbers to the footer of an existing Excel document using Spire.XLS for .NET.
- Create a Workbook object, and load a sample Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through the Workbook.Worksheets[index] property.
- Add page numbers with formatting to the left footer section by setting the PageSetup.LeftFooter property to “&\"Calibri\"&B&10&K4253E2Page &P”. You can customize the page numbers’ formatting according to your preference.
- Add the current date to the right footer section by setting the PageSetup.RightFooter property to “&\"Calibri\"&B&10&K4253E2&D”. Likewise, you can change the appearance of the date string as desired.
- Save the workbook to another Excel file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls; namespace AddDateAndPageNumberToFooter { class Program { static void Main(string[] args) { //Create a Workbook object Workbook wb = new Workbook(); //Load an exsting Excel file wb.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx"); //Get the first worksheet Worksheet sheet = wb.Worksheets[0]; //Add page number to footer's left section sheet.PageSetup.LeftFooter = "&\"Calibri\"&B&10&K4253E2Page &P"; //Add current date to footer's right section sheet.PageSetup.RightFooter = "&\"Calibri\"&B&10&K4253E2&D"; //Save the file wb.SaveToFile("Footer.xlsx", ExcelVersion.Version2016); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.