logo

Apache POI Excel Cell Fonts

Java Code Examples for:

  • org.apache.poi.ss.usermodel.Font class
  • org.apache.poi.ss.usermodel.CellStyle class

In Java code examples below we show you how to use Apache POI library to generate Excel file with custom font style for Excel cells such as bold, italic, underline, strikeout, type offset, font name, font height and color.

Apache POI Library Dependencies

Define below dependencies in build.gradle if you are using Gradle build.

compile group: 'org.apache.poi', name: 'poi', version: '4.0.1'

Define this dependencies in pom.xml if you are using Maven build.

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.1</version>
</dependency>

Example application to set Font Name, Heigh and Color

package simplesolution.dev;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FontNameHeightColorExample {

    public static void main(String... args) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("fonts-example");
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("SimpleSolution.dev");

        Font font = workbook.createFont();
        font.setFontHeightInPoints((short)24);
        font.setFontName("Arial");
        font.setColor(IndexedColors.BLUE.getIndex());

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);

        try (OutputStream fileOut = new FileOutputStream("apache-poi-fonts-name-height-color.xls")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Output Excel file:

Apache POI Fonts Name Height and Color

Example application to set Font Bold and different Underline styles

package simplesolution.dev;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FontBoldUnderlineExample {

    public static void main(String... args) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("fonts-example");

        // Create Cell 1
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("SimpleSolution.dev");

        Font font = workbook.createFont();
        font.setBold(true);
        font.setUnderline(Font.U_SINGLE);

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);

        // Create Cell 1
        row = sheet.createRow(2);
        cell = row.createCell(0);
        cell.setCellValue("SimpleSolution.dev");

        font = workbook.createFont();
        font.setBold(true);
        font.setUnderline(Font.U_DOUBLE);

        cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);

        // Create Cell 3
        row = sheet.createRow(4);
        cell = row.createCell(0);
        cell.setCellValue("SimpleSolution.dev");

        font = workbook.createFont();
        font.setBold(true);
        font.setUnderline(Font.U_SINGLE_ACCOUNTING);

        cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);

        // Create Cell 4
        row = sheet.createRow(6);
        cell = row.createCell(0);
        cell.setCellValue("SimpleSolution.dev");

        font = workbook.createFont();
        font.setBold(true);
        font.setUnderline(Font.U_DOUBLE_ACCOUNTING);

        cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);

        try (OutputStream fileOut = new FileOutputStream("apache-poi-fonts-bold-underline.xls")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Output Excel file:

Apache POI Fonts Bold Underline

Example application to set Font Italic and Strikeout

package simplesolution.dev;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FontItalicStrikeoutExample {

    public static void main(String... args) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("fonts-example");
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("SimpleSolution.dev");

        Font font = workbook.createFont();
        font.setItalic(true);
        font.setStrikeout(true);

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);

        try (OutputStream fileOut = new FileOutputStream("apache-poi-fonts-italic-strikeout.xls")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Output Excel file:

Apache POI Fonts Italic Strikeout

Example application to set Font Type Offset Super and Sub

package simplesolution.dev;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FontTypeOffsetExample {

    public static void main(String... args) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("fonts-example");

        // Create Cell 1
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("SimpleSolution.dev");

        Font font = workbook.createFont();
        font.setTypeOffset(Font.SS_SUPER);

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);

        // Create Cell 1
        row = sheet.createRow(2);
        cell = row.createCell(0);
        cell.setCellValue("SimpleSolution.dev");

        font = workbook.createFont();
        font.setTypeOffset(Font.SS_SUB);

        cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);

        try (OutputStream fileOut = new FileOutputStream("apache-poi-fonts-type-offset.xls")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Output Excel file:

Apache POI Fonts Type Offset Super and Sub

The source code in this article can be found at: github.com/simplesolutiondev/ApachePOIExcelCellFonts

or download at:

Download Source Code

Happy Coding 😊

Apache POI Excel Cell Background Foreground and Font Color

Apache POI Excel Cell Border and Border Color

Apache POI Vertical and Horizontal Excel Cell Alignment

Apache POI Merging Excel Cells

Tags: