Dealing with Font Settings
The look and feel of the text can be controlled by changing its font settings. These font settings may include the name, style, size, color and other effects of the fonts as shown below in the figure:
Font settings in Microsoft Excel
Just like Microsoft Excel, Aspose.Cells also supports configuring the font settings of the cells.
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:
- Apply a specific font to text.
- Set text to bold.
- Set the font size.
- Set the font color.
- Underline text.
- Strikeout text.
- Set text to subscript.
- Set text to superscript.
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
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); |