News Category

Header and Footer

Header and Footer (2)

This article demonstrates how to set different header and footer for the fisrt page using Spire.XLS for Java.

import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class SetDifferentHeaderFooter {

    public static void main(String[] args) {

        //Create a Workbook instance
        Workbook workbook = new Workbook();

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Insert text in A1 and J1
        sheet.getCellRange("A1").setText("page 1");
        sheet.getCellRange("J1").setText("page 2");

        //Set different first page
        sheet.getPageSetup().setDifferentFirst((byte)1);

        //Set header string and footer string for the first page
        sheet.getPageSetup().setFirstHeaderString("First header");
        sheet.getPageSetup().setFirstFooterString("First footer");

        //Set header string and footer string for other pages
        sheet.getPageSetup().setCenterHeader("Header of other pages");
        sheet.getPageSetup().setCenterFooter("Footer of other pages");

        //Save the document
        workbook.saveToFile("DifferentFirstPage.xlsx", FileFormat.Version2016);
    }
}

Set Different Header and Footer for the First Page in Java

Headers and footers in Excel are the text or images placed at the top and bottom of each page, respectively. These texts/images give basic information about the pages or document, such as the file name, company logo, page number, date/time, and so on. In this article, you will learn how to programmatically add text, images, as well as fields (like page number) to Excel headers or footers using Spire.XLS for Java.

Spire.XLS for Java provides the PageSetup class to work with the page setup in Excel including headers and footers. Specifically, it offers the setLeftHeader() method, setCenterHeader() method, setRightHeader() method, setLeftFooter() method, etc. to add content to 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 Java

First of all, you’re required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls</artifactId>
        <version>14.3.2</version>
    </dependency>
</dependencies>
    

Add Images and Formatted Text to Header

The steps to add images and formatted text an Excel header using Spire.XLS for Java are as follows.

  • Create a Workbook object.
  • Load a sample Excel file using Workbook.loadFromFile() method.
  • Get a specific worksheet using Workbook.getWorksheets().get() method.
  • Load an image using ImageIO.read() method.
  • Set the image as the image source of the header’s left section using PageSetup.setLeftHeaderImage() method.
  • Display image in the left header section by passing the value “&G” to PageSetup.setLeftHeader() method as a parameter.
  • Save the workbook to another Excel file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;

public class AddImageAndTextToHeader {

    public static void main(String[] args) throws IOException {

        //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.getWorksheets().get(0);

        //Load an image
        BufferedImage bufferedImage = ImageIO.read(new FileInputStream("C:\\Users\\Administrator\\Desktop\\your-logo.png"));

        //Add image to header’s left section
        sheet.getPageSetup().setLeftHeaderImage(bufferedImage,0.4f);
        sheet.getPageSetup().setLeftHeader("&G");

        //Add formatted text to header’s right section
        sheet.getPageSetup().setRightHeader("&\"Calibri\"&B&10&K4253E2X Information Technology, Inc.  \n www.xxx.com");

        //Save the file
        wb.saveToFile("output/Header.xlsx", ExcelVersion.Version2016);
    }
}

Java: Add Headers and Footers to Excel

Add the Current Date and Page Number to Footer

The following are the steps to add the current date and page number to an Excel footer using Spire.XLS for Java.

  • Create a Workbook object.
  • Load a sample Excel file using Workbook.loadFromFile() method.
  • Get a specific worksheet using Workbook.getWorksheets.get() method.
  • Add page numbers with formatting to the footer’s left section by passing the value “&\"Calibri\"&B&10&K4253E2Page &P” to PageSetup.setLeftFooter() method.  You can customize the page numbers’ formatting according to your preference.
  • Add the current date to the footer’s right section by passing the value “&\"Calibri\"&B&10&K4253E2&D” to PageSetup.setRightFooter() method. Likewise, you can change the appearance of the date string as desired.
  • Save the workbook to another Excel file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet; 

public class AddDateAndPageNumberToFooter {

    public 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.getWorksheets().get(0);

        //Add page number to footer's left section
        sheet.getPageSetup().setLeftFooter("&\"Calibri\"&B&10&K4253E2Page &P");

        //Add current date to footer's right section
        sheet.getPageSetup().setRightFooter("&\"Calibri\"&B&10&K4253E2&D");

        //Save the file
        wb.saveToFile("output/Footer.xlsx", ExcelVersion.Version2016);
    }
}

Java: Add Headers and Footers to Excel

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.