This tutorial will demonstrate how to set font and background color for Excel textbox using Spire.XLS for Java.
The following is a screenshot of a sample document:
Using the code
import java.awt.*; import com.spire.xls.*; import com.spire.xls.core.spreadsheet.shapes.*; public class SetFontAndBackground { public static void main(String[] args) { //Load an Excel sample Workbook workbook = new Workbook(); workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.xlsx"); //Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); //Get the textbox which will be edited XlsTextBoxShape shape = (XlsTextBoxShape) sheet.getTextBoxes().get(0); //Set the font and background color for the textbox //Set font ExcelFont font = workbook.createFont(); font.setFontName("Calibri"); font.setSize(14); font.isBold(false); font.setColor(Color.MAGENTA); (new RichText(shape.getRichText())).setFont(0, shape.getText().length() - 1, font); //Set background color shape.getFill().setFillType(ShapeFillType.SolidColor); shape.getFill().setForeKnownColor(ExcelColors.Yellow); //Save to file workbook.saveToFile("output/setFontAndBackgroundForTextBox.xlsx", ExcelVersion.Version2013); } }
Output