Skip to content
Related Articles

Related Articles

How to Apply Fonts to the Contents of a Cell Using Java?

Improve Article
Save Article
  • Last Updated : 20 Nov, 2020
Improve Article
Save Article

In this article, we will learn how to apply the custom font and various styles associated with it using Java and Apache POI (a Java API which is very useful to handle the Microsoft Documents).


Writing a file using POI is very simple and involve following steps:

  1. Create a workbook.
  2. Create a spreadsheet in workbook.
  3. Create a font and apply styling to it in the spreadsheet.
  4. Create a cell and apply value to it.
  5. Add cells in spreadsheet.
  6. Repeat step 3 to 5 to write more data



// import Statements
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class FontsInExcel {
    public static void fontFile()
          // Created a workbook
        XSSFWorkbook myWorkbook = new XSSFWorkbook();
        // Created a spreadsheet
        XSSFSheet newSpreadsheet
            = myWorkbook.createSheet("Book");
        XSSFRow row = newSpreadsheet.createRow(1);
        // Created a new font
        XSSFFont font = myWorkbook.createFont();
        // Setting Font Properties
        // Set created font into style
        XSSFCellStyle cellStyle
            = myWorkbook.createCellStyle();
        // Create a cell with a custom value and set style
        // to it.
        XSSFCell myCell = row.createCell(6);
        myCell.setCellValue("New Font");
        // Opening and changing the File
        FileOutputStream file = new FileOutputStream(
            new File("C:/Book.xlsx"));
    public static void main(String[] args) throws Exception


Apply fonts to the content of a cell

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!