Java add image and text header/footer to Excel
Tags: header, footer, image, text
Published: 2020-02-17 08:42:38

---
title: Java add image and text header/footer to Excel
published: true
description: Java add image and text hearder footer to excel worksheets 
tags: header, footer, image, text
---

Excel header and footer gives additional information of a spreadsheet. And it can be in text and image. By default, the headers or footers on odd and even pages are the same. We could also set different heard footer for odd and even pages. This article will show you how to insert header and footer to Excel worksheet in Java applications from the following three parts:
Add image header to Excel
Add text footer to Excel 
Add different header footer for Odd and Even pages

Add image header to Excel

```java
import com.spire.xls.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

public class ImageHeader {
      public static void main(String[] args) throws IOException {
        String imageFile = "logo.png";

        //Create a workbook and load a file
        Workbook workbook = new Workbook();

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

        //Load an image from disk
        BufferedImage image = ImageIO.read( new File(imageFile));

        //Set the image header
        worksheet.getPageSetup().setLeftHeaderImage(image);
        worksheet.getPageSetup().setLeftHeader("&G");

        //Set the view mode as layout
        worksheet.setViewMode(ViewMode.Layout);

        //Save the document to file
        workbook.saveToFile("output/ImageHeader.xlsx", ExcelVersion.Version2010);
    }
}
```
Effective screenshot of image header:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/082ks9j3o5fgvqojxbe6.png)
 
Add text footer to Excel

```java
import com.spire.xls.*;

public class TextFooter {
    public static void main(String[] args) {

        //Create a workbook and load a file
        Workbook workbook = new Workbook();

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

        //Set center footer
        worksheet.getPageSetup().setCenterFooter("Spire.XLS for Java");

        //Set the view mode as layout
        worksheet.setViewMode(ViewMode.Layout);

        //Save the document to file
        workbook.saveToFile("output/TextFooter.xlsx", ExcelVersion.Version2010);
    }
}
```
Effective screenshot of text footer on Excel:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/iecmkd9qzu4r1fuyc2dd.png)
 
Add different header and footer for Odd and Even pages:

```java
import com.spire.xls.*;

public class HeaderFooter {
    public static void main(String[] args) {

        //Create a workbook and load a file
        Workbook workbook = new Workbook();

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

        worksheet.getCellRange("A1").setText("Page 1");
        worksheet.getCellRange("J1").setText("Page 2");

        //Set different header footer for Odd and Even pages
        worksheet.getPageSetup().setDifferentOddEven((byte)1);

        //Set the header footer with font, size, bold and color
        worksheet.getPageSetup().setOddHeaderString( "&\"Arial\"&12&B&KFFC000 Odd_Header");
        worksheet.getPageSetup().setOddFooterString ( "&\"Arial\"&12&B&KFFC000 Odd_Footer");
        worksheet.getPageSetup().setEvenHeaderString ( "&\"Arial\"&12&B&KFF0000 Even_Header");
        worksheet.getPageSetup().setEvenFooterString ( "&\"Arial\"&12&B&KFF0000 Even_Footer");

        //Set the view mode as layout
        worksheet.setViewMode(ViewMode.Layout);

        //Save the document to file
        workbook.saveToFile("output/Different.xlsx", ExcelVersion.Version2010);
    }
}

```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fev8bdbttoqihft84z9q.png)