Dealing with Font Settings

Configuring Font Settings

Aspose.Cells provides a class, Workbook that represents a Microsoft Excel file. The Workbook class contains a WorksheetCollection that allows access to each worksheet in an Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides a Cells collection. Each item in the Cells collection represents an object of the Cell class.

Aspose.Cells provides the Cell class' setStyle method, used to set a cell’s formatting. Also, the object of the Style class provides properties for configuring font settings.

This article shows how to:

Setting Font Name

Apply a specific font to text in cells using the Font object’s setName property.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(SettingFontName.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding some value to the "A1" cell
Cell cell = cells.get("A1");
cell.setValue("Hello Aspose!");
// Setting the font name to "Times New Roman"
Style style = cell.getStyle();
Font font = style.getFont();
font.setName("Times New Roman");
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "SettingFontName_out.xls");

Setting Font Style to Bold

Set text to bold by setting the Font object’s setBold property to true.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the output directory.
String outputDir = Utils.Get_OutputDirectory();
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.getWorksheets().add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.getWorksheets().get(i);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.getStyle();
// Setting the font weight to bold
style.getFont().setBold(true);
// Applying the style to the cell
cell.setStyle(style);
// Saving the Excel file
workbook.save(outputDir + "book1.out.xlsx", SaveFormat.XLSX);

Setting Font Size

Set the font size using the Font object’s setSize property.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(SetFontSize.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding some value to the "A1" cell
Cell cell = cells.get("A1");
cell.setValue("Hello Aspose!");
// Setting the font weight to bold
Style style = cell.getStyle();
Font font = style.getFont();
font.setSize(14);
cell.setStyle(style);
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "SetFontSize_out.xls");

Setting Font Underline Type

Underline text with the Font object’s setUnderline property. Aspose.Cells offers various pre-defined font underline types in the FontUnderlineType enumeration.

Font Underline Types Description
NONE No underline
SINGLE A single underline
DOUBLE Double underline
ACCOUNTING A single accounting underline
DOUBLE_ACCOUNTING Double accounting underline
DASH Dashed Underline
DASH_DOT_DOT_HEAVY Thick Dash-Dot-Dot Underline
DASH_DOT_HEAVY Thick Dash-Dot Underline
DASHED_HEAVY Thick Dashed Underline
DASH_LONG Long Dashed Underline
DASH_LONG_HEAVY Thick Long Dashed Underline
DOT_DASH Dash-Dot Underline
DOT_DOT_DASH Dash-Dot-Dot Underline
DOTTED Dotted Underline
DOTTED_HEAVY Thick Dotted Underline
HEAVY Thick Underline
WAVE Wave Underline
WAVY_DOUBLE Double Wave Underline
WAVY_HEAVY Heavy Wave Underline
WORDS Underline Non-Space Characters Only
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(SettingFontUnderlineType.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding some value to the "A1" cell
Cell cell = cells.get("A1");
cell.setValue("Hello Aspose!");
// Setting the font to be underlined
Style style = cell.getStyle();
Font font = style.getFont();
font.setUnderline(FontUnderlineType.SINGLE);
cell.setStyle(style);
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "SFUnderlineType_out.xls");

Setting Font Color

Set the font color with the Font object’s setColor property. Select any color from the Color enumeration and assign the selected color to the Font object’s setColor.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(SetFontColor.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding some value to the "A1" cell
Cell cell = cells.get("A1");
cell.setValue("Hello Aspose!");
// Setting the font color to blue
Style style = cell.getStyle();
Font font = style.getFont();
font.setColor(Color.getBlue());
cell.setStyle(style);
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "SetFontColor_out.xls");

Setting Strikeout Effect on Text

Strikeout text with the Font object’s setStrikeout property.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(SettingStrikeOutEffect.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding some value to the "A1" cell
Cell cell = cells.get("A1");
cell.setValue("Hello Aspose!");
// Setting the strike out effect on the font
Style style = cell.getStyle();
Font font = style.getFont();
font.setStrikeout(true);
cell.setStyle(style);

Setting Subscript

Make text superscript by using the Font object’s setSubscript property.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(SetSubscript.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding some value to the "A1" cell
Cell cell = cells.get("A1");
cell.setValue("Hello Aspose!");
// Setting subscript effect
Style style = cell.getStyle();
Font font = style.getFont();
font.setSubscript(true);
cell.setStyle(style);

Setting Superscript

Apply superscript to text with the Font object’s setSuperscript property.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(SetSubscript.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding some value to the "A1" cell
Cell cell = cells.get("A1");
cell.setValue("Hello Aspose!");
// Setting superscript effect
Style style = cell.getStyle();
Font font = style.getFont();
font.setSuperscript(true);
cell.setStyle(style);

Advance topics

  • Apply Superscript and Subscript Effects on Fonts
  • Get a List of Fonts used in a Spreadsheet or Workbook