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