-
Notifications
You must be signed in to change notification settings - Fork 270
Header and Footer
Mats Alm edited this page Jan 19, 2024
·
12 revisions
Headers and Footers of printed worksheets are set via the ExcelWorksheet.HeaderFooter
property.
If you want the same header on all pages when printing the worksheet, use the ExcelWorksheet.HeaderFooter.OddHeader
property only.
using(var package = new ExcelPackage())
{
var sheet = package.Workbook.Worksheets.Add("Sheet1");
// add a logo to upper left corner
using(var fs = new FileStream(@"c:\images\myLogo.jpg", FileMode.Open))
{
var drawing = sheet.HeaderFooter.OddHeader.InsertPicture(fs, ePictureType.Jpg, PictureAlignment.Left);
// you can use the drawing instance to change Height, Width, etc.
}
// add todays date to the upper right corner
sheet.HeaderFooter.OddHeader.RightAlignedText = ExcelHeaderFooter.CurrentDate;
}
When setting values both ExcelWorksheet.HeaderFooter.OddHeader
and ExcelWorksheet.HeaderFooter.EvenHeader
these will be used on every second page respectively, starting with OddHeader. See example below
// for odd page numbers - add an image in the upper left corner
using var fs1 = new FileStream(imgPath1, FileMode.Open);
sheet.HeaderFooter.OddHeader.InsertPicture(fs1, ePictureType.Jpg, PictureAlignment.Left);
// for even page numbers - add an image in the upper right corner
using var fs2 = new FileStream(imgPath2, FileMode.Open);
sheet.HeaderFooter.EvenHeader.InsertPicture(fs2, ePictureType.Jpg, PictureAlignment.Right);
sheet.HeaderFooter.OddHeader.RightAlignedText = ExcelHeaderFooter.CurrentDate;
sheet.HeaderFooter.EvenHeader.LeftAlignedText = ExcelHeaderFooter.CurrentDate;
As demonstrated in the examples above you can insert various values by using static constants from the ExcelHeaderFooter
class. See table below:
Field | Description |
---|---|
PageNumber |
Page number of the current printed page |
NumberOfPages |
Total number of printed pages |
FontColor |
Text font color |
SheetName |
The name of the printed worksheet |
FilePath |
File path of the workbook |
FileName |
File name of the workbook |
CurrentDate |
The current date |
CurrentTime |
The current time |
Image |
Background image of the printed sheet |
OutlineStyle |
Outline style |
ShadowStyle |
Shadow style |
Works as headers (see above), but use ExcelWorksheet.HeaderFooter.OddFooter
and ExcelWorksheet.HeaderFooter.EvenFooter
instead.
- Sample 1.1-C# and sample 1.1-VB has more examples on how to add headers/footers.
EPPlus Software AB - https://epplussoftware.com
- Getting Started
- The Sample Project
- EPPlus and Docker
- EPPlus and Blazor
- EPPlus and Linux
- Security
- Configuration
- Releases/versioning
- What is new in EPPlus 5+
- Breaking Changes in EPPlus 5
- Breaking Changes in EPPlus 6
- Breaking Changes in EPPlus 7
- Addressing a worksheet
- Dimension/Used range
- Copying ranges/sheets
- Insert/Delete
- Filling ranges
- Sorting ranges
- Taking and skipping columns/rows
- Data validation
- Comments
- Freeze and Split Panes
- Header and Footer
- Autofit columns
- Grouping and Ungrouping Rows and Columns
- Formatting and styling
- Conditional formatting
- Using Themes
- Working with custom named table- or slicer- styles