Appy Multiple Fonts in One Cell in Java

2020-06-19 07:01:51 Written by  support iceblue
Rate this item
(0 votes)

This article demonstrates how to apply multiple font styles in a single Excel cell using Spire.XLS for Java.

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("MultiFonts.xlsx", ExcelVersion.Version2016);
    }
}

Appy Multiple Fonts in One Cell in Java

Additional Info

  • tutorial_title:
Last modified on Wednesday, 01 September 2021 02:41