处理字体设置
可以通过更改字体设置来控制文本的外观和感觉。这些字体设置可能包括字体的名称、样式、大小、颜色和其他效果,如下图所示:
Microsoft Excel 中的字体设置
与Microsoft Excel一样,Aspose.Cells也支持配置单元格的字体设置。
配置字体设置
Aspose.Cells提供了一个Workbook类,代表一个Microsoft Excel文件。Workbook类包含一个WorksheetCollection,允许访问Excel文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供了一个Cells集合。Cells集合中的每个项目都表示Cell类的对象。
Aspose.Cells提供了Cell类的setStyle方法,用于设置单元格的格式。Style类的对象还提供了配置字体设置的属性。
本文介绍如何:
设置字体名称
使用 Font 对象的 setName 属性在单元格中应用特定字体。
// 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"); |
将字体样式设置为粗体
通过将 Font 对象的 setBold 属性设置为 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); |
设置字体大小
// 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"); |
设置字体下划线类型
使用 Font 对象的 setUnderline 属性给文本添加下划线。Aspose.Cells 在 FontUnderlineType 枚举中提供各种预定义的字体下划线类型。
字体下划线类型 | 描述 | ||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
NONE | 无下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
SINGLE | 单下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOUBLE | 双下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
ACCOUNTING | 单会计下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOUBLE_ACCOUNTING | 双会计下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
DASH | 虚线下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
DASH_DOT_DOT_HEAVY | 粗虚线-点-点下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
DASH_DOT_HEAVY | 粗虚线-点下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
DASHED_HEAVY | 粗虚线下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
DASH_LONG | 长虚线下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
DASH_LONG_HEAVY | 粗长虚线下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOT_DASH | 点划线下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOT_DOT_DASH | 点划双下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOTTED | 点线下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOTTED_HEAVY | 粗点线下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
HEAVY | 粗下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
WAVE | 波浪线下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
WAVY_DOUBLE | 双波浪线下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
WAVY_HEAVY | 厚波浪线下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
WORDS | 仅对非空格字符下划线 | ||||||||||||||||||||||||||||||||||||||||||||||||||
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
|
设置字体颜色
使用Font对象的setColor属性设置字体颜色。从Color枚举中选择任何颜色,并将所选颜色分配给Font对象的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"); |
在文本上设置删除线效果
使用Font对象的setStrikeout属性划掉文本。
// 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); |
设置下标
通过使用Font对象的setSubscript属性使文本成为上标。
// 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); |
设置上标
使用Font对象的setSuperscript属性将上标应用于文本。
// 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); |