• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to Insert Text and Images to Excel Header or Footer in Java

Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Excel headers or footers are visible in Page Layout view or the printed pages. We can add the headers or footers at the top or bottom of a printed worksheet to display page number, worksheet name, current data, text or a picture. This article will demonstrates how to programmatically add headers or footers to a worksheet by using a free java API for Excel.


This solution relies on Free Spire.XLS for Java, which provides a PageSetup class to deal with all page setup settings in Excel. Particularly, it offers setLeftHeader method, setLeftHeaderImage method, etc. to handle text and image in header or footer. In addtion, some special script commands are provided for you to display dynamic content like page number and current date, or to format the text to be showed.

Here is a list of scripts and their usage.  
&PThe current page number
&NThe total number of pages
&DThe current date
&TThe current time
&GA picture
&AThe worksheet name
&FThe file name
&BMake text bold
&IItalicize text
&UUnderline text
&”font name”Represents a font name, for example, &”Aril”
&font sizeRepresents font size, for example, &12
&K<HEX color>Represents font color, for example, &KFF0000


Download Free Spire.XLS for Java pack, unzip it and you’ll get Spire.Xls.jar file from the “lib” folder. Import the jar file in your project as a dependency. If you are creating a Maven project, you can easily add the jar dependency by adding the following configurations to the pom.xml.

Example 1. Insert image and page number in header

It’s very commen that we add our company logo or another meanful image to a document at the header space. The following code snippets show you how to insert an image as well as page number in the Excel header.

Example 2. Insert formatted text in footer

It is easy to insert plain text in header or footer by assigning values to header or footer. To change the look of the text, you can use the script commends listed in the table, mentioned above to format the text.

Example 3. Set different header and footer for the first page

In some cases, the first page will be used as the cover of your printed document. As a result, you may want the header and footer of the fisrt page to be different.

When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic