• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overview

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.

Introduction

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.  
ScriptDescription
&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


Installation

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.


 
I promise I will be the best, most loyal friend ever! All for this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic