Java Excel settings background color and background image

In this paper, I will introduce how to set background color and background image in Excel document using Spire.XLS for Java.

Set the background color of the Excel workbook


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

import java.awt.*;

public class BackgroundColor{
    public static void main(String[] args){
        //Create a workbook object
        Workbook workbook = new Workbook();
        //Import an Excel document
        workbook.loadFromFile("input.xlsx");

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

        //Set the background color for the cell range used in the sheet
        sheet.getAllocatedRange().getStyle().setColor(Color.green);
        //Sets the background color to the specified cell range
        //sheet.getCellRange("A1:E19").getStyle().setColor(Color.yellow);

        //Save document
        workbook.saveToFile("SetBackColor.xlsx", ExcelVersion.Version2013);
    }
}

Background color.png

Set background image for Excel workbook


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.File;
import java.io.IOException;

public class BackgroundImage {
    public static void main(String[] args) throws IOException {
        //Create a workbook object
        Workbook workbook = new Workbook();
        //Import an Excel document
        workbook.loadFromFile("input.xlsx");

        //Get the first sheet
        Worksheet sheet = workbook.getWorksheets().get(0);
        //Load image
        BufferedImage image = ImageIO.read( new File("background.jpg "));
        //Set the image as the background of the sheet
        sheet.getPageSetup().setBackgoundImage(image);

        //Save document
        workbook.saveToFile("SetBackImage.xlsx", ExcelVersion.Version2013);
    }
}

Background image.png

Recommended Posts

Java Excel settings background color and background image
Java settings word Page background color
Java Excel Insertion and Image Extraction
Java sets the background color and background image for PowerPoint documents
Java adds and reads Excel formulas
Java management and STS (Eclipse) build path settings
Java Development Basics ~ Development Environment Settings and Project Creation ~
Add, read, and delete Excel comments in Java
Java and JavaScript
XXE and Java
Vectorize and image MNIST handwritten digit image data in Java
Background image transparency (layering the image with your favorite color)
Getters and setters (Java)
Web design color image
[Java] Thread and Runnable
Java true and false
[Java] String comparison and && and ||
Print Java Excel Worksheet
[Java] PDF viewing settings
Java --Serialization and Deserialization
[Java] Arguments and parameters
timedatectl and Java TimeZone
[Java] Branch and repeat
[Java] Variables and types
java (classes and instances)
[Java] Overload and override
Java sets different background colors for adjacent rows in Excel