DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

Java add image and text header/footer to Excel

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

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

Add text footer to Excel

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

Add different header and footer for Odd and Even pages:

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

Top comments (0)