Monday, 17 February 2020 09:24

Apply Fonts in Excel in Java

Written by  support iceblue
Rate this item
(0 votes)

This article demonstrates how to apply font styles to Excel cells using Spire.XLS for Java.

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
        Workbook workbook = new Workbook();

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

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

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

        //Set font color
        sheet.getCellRange("B3").setText("Font color: Blue");
        sheet.getCellRange("B3").getCellStyle().getExcelFont().setColor(Color.blue);

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

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

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

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

Apply Fonts in Excel in Java

Additional Info

  • tutorial_title: