Thursday, 13 February 2020 08:13

Java add image and text header/footer to Excel

Written by  support iceblue
Rate this item
(0 votes)

Spire.XLS for Java supports to insert image and text header and footer to Excel files. This article will demonstrate how to insert header and footer to Excel worksheet 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:

Java add image and text header/footer to Excel

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:

Java add image and text header/footer to Excel

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);
    }
}

Java add image and text header/footer to Excel

Additional Info

  • tutorial_title: