DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

E-iceblue Product Family
E-iceblue Product Family

Posted on • Updated on

Apply Fonts in Excel Cells in Java

The ability to set fonts is one of the most basic requirements of an Excel java library. In this article, I’ll show you to how to apply font styles like size, color to cells and how to make text bold, underline or italic, by using Free Spire.XLS for Java.

Add Spire.Xls.jar as dependency

Method 1: Download Free Spire.XLS for Java pack, unzip it and you’ll get Spire.Xls.jar file from the β€œlib” folder. Import the jar file in your project as a dependency.

Method 2: If you are creating a Maven project, you can easily add the jar dependency by adding the following configurations to the pom.xml.

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId> e-iceblue </groupId>
        <artifactId>spire.xls.free</artifactId>
        <version>2.2.0</version>
    </dependency>
</dependencies>

Example 1. Apply fonts in different cells

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

import java.awt.*;

public class SetFontStyle
{
    public static void main(String[] args) {

        //Create a Workbook instance
        Workbook workbook = new Workbook();

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

        //Set font name 
        sheet.getCellRange("A1").setText("Font name: Comic Sans MS");
        sheet.getCellRange("A1").getCellStyle().getExcelFont().setFontName("Comic Sans MS");

        //Set font size 
        sheet.getCellRange("A2").setText("Font size: 20");
        sheet.getCellRange("A2").getCellStyle().getExcelFont().setSize(20);

        //Set font color 
        sheet.getCellRange("A3").setText("Font color: red");
        sheet.getCellRange("A3").getCellStyle().getExcelFont().setColor(Color.red);

        //Set to bold 
        sheet.getCellRange("A4").setText("Font style: Bold");
        sheet.getCellRange("A4").getCellStyle().getExcelFont().isBold(true);

        //Set to underline 
        sheet.getCellRange("A5").setText("Font style: Underline");
        sheet.getCellRange("A5").getCellStyle().getExcelFont().setUnderline(FontUnderlineType.Single);

        //Set to italic 
        sheet.getCellRange("A6").setText("Font style: Italic");
        sheet.getCellRange("A6").getCellStyle().getExcelFont().isItalic(true);

        //Save the document 
        workbook.saveToFile("FontStyles.xlsx", ExcelVersion.Version2016);
    }
}

Alt Text

Example 2. Apply multiple fonts in a signle cell

import com.spire.xls.*;

import java.awt.*;

public class ApplyMultiFontsInCell {

    public static void main(String[] args) {

        //Create a Workbook instance
        Workbook wb = new Workbook();

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

        //Create one Excel font
        ExcelFont font1 = wb.createFont();
        font1.setFontName("Calibri");
        font1.setColor(Color.blue);
        font1.setSize(12f);
        font1.isBold(true);

        //Create another Excel font
        ExcelFont font2 = wb.createFont();
        font2.setFontName("Times New Roman");
        font2.setColor(Color.red);
        font2.setSize(14f);
        font2.isBold(true);
        font2.isItalic(true);

        //Insert text to cell B5
        RichText richText = sheet.getCellRange("B5").getRichText();
        richText.setText("This document was created with Spire.XLS for Java.");

        //Apply two fonts to the text in the cell B5
        richText.setFont(0, 30, font1);
        richText.setFont(31, 50, font2);

        //Save the document
        wb.saveToFile("output/MultiFonts.xlsx", ExcelVersion.Version2016);
    }
}

Alt Text

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Good news! You can update to dark mode in your DEV settings.