Gestione delle Impostazioni del Font
L’aspetto del testo può essere controllato modificando le impostazioni del carattere. Queste impostazioni del carattere possono includere il nome, lo stile, la dimensione, il colore e altri effetti dei font come mostrato di seguito nella figura:
Impostazioni del carattere in Microsoft Excel
Proprio come Microsoft Excel, anche Aspose.Cells supporta la configurazione delle impostazioni del carattere delle celle.
Configurazione delle Impostazioni del Carattere
Aspose.Cells fornisce una classe, Workbook che rappresenta un file di Microsoft Excel. La classe Workbook contiene una WorksheetCollection che consente di accedere a ciascun foglio di lavoro in un file di Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una raccolta Cells. Ogni elemento nella raccolta Cells rappresenta un oggetto della classe Cell.
Aspose.Cells fornisce il metodo setStyle della classe Cell utilizzato per impostare la formattazione di una cella. Inoltre, l’oggetto della classe Style fornisce proprietà per configurare le impostazioni del carattere.
Questo articolo mostra come:
- Applicare un font specifico al testo.
- Impostare il testo in grassetto.
- Impostare la dimensione del font.
- Impostare il colore del font.
- Sottolineare il testo.
- Barrare il testo.
- Impostare il testo come pedice.
- Impostare il testo come apice.
Impostazione del nome del carattere
Applicare un font specifico al testo nelle celle utilizzando la proprietà setName dell’oggetto Font.
// 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"); |
Impostare lo stile del carattere su Grassetto
Imposta il testo in grassetto impostando la proprietà setBold dell’oggetto Font a 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); |
Impostazione della dimensione del carattere
Imposta la dimensione del carattere utilizzando la proprietà setSize dell’oggetto Font.
// 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"); |
Impostazione del tipo sottolineato del carattere
Sottolinea il testo utilizzando la proprietà setUnderline dell’oggetto Font. Aspose.Cells offre vari tipi predefiniti di sottolineatura del carattere nella enumerazione FontUnderlineType.
Tipi di sottolineatura del carattere | Descrizione | ||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
NONE | Nessun sottolineatura | ||||||||||||||||||||||||||||||||||||||||||||||||||
SINGLE | Un singolo sottolineatura | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOUBLE | Doppio sottolineatura | ||||||||||||||||||||||||||||||||||||||||||||||||||
ACCOUNTING | Un singolo sottolineatura contabile | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOUBLE_ACCOUNTING | Doppio sottolineatura contabile | ||||||||||||||||||||||||||||||||||||||||||||||||||
DASH | Sottolineatura tratteggiata | ||||||||||||||||||||||||||||||||||||||||||||||||||
DASH_DOT_DOT_HEAVY | Sottolineatura spessa a trattino-punto-punto | ||||||||||||||||||||||||||||||||||||||||||||||||||
DASH_DOT_HEAVY | Sottolineatura spessa a tratino-punto | ||||||||||||||||||||||||||||||||||||||||||||||||||
DASHED_HEAVY | Sottolineatura spessa tratteggiata | ||||||||||||||||||||||||||||||||||||||||||||||||||
DASH_LONG | Sottolineatura lunga trattata | ||||||||||||||||||||||||||||||||||||||||||||||||||
DASH_LONG_HEAVY | Sottolineatura spessa lunga trattata | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOT_DASH | Sottolineatura trattino-punto | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOT_DOT_DASH | Trattino-Punto-Punto Sottolineato | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOTTED | Sottolineato a Puntini | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOTTED_HEAVY | Sottolineato Spesso a Puntini | ||||||||||||||||||||||||||||||||||||||||||||||||||
HEAVY | Sottolineatura spessa | ||||||||||||||||||||||||||||||||||||||||||||||||||
WAVE | Ondulato sottolineatura | ||||||||||||||||||||||||||||||||||||||||||||||||||
WAVY_DOUBLE | Doppia ondulato sottolineatura | ||||||||||||||||||||||||||||||||||||||||||||||||||
WAVY_HEAVY | Ondulato pesante sottolineatura | ||||||||||||||||||||||||||||||||||||||||||||||||||
WORDS | Sottolinea solo caratteri non spazio | ||||||||||||||||||||||||||||||||||||||||||||||||||
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
|
Impostazione del colore del carattere
Imposta il colore del font con la proprietà Font’s setColor. Seleziona un colore qualsiasi dall’enumerazione Color e assegna il colore selezionato alla proprietà Font’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"); |
Impostazione dell’effetto barrato sul testo
Barrare il testo con la proprietà Font’s 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); |
Impostazione del pedice
Rendi il testo in apice utilizzando la proprietà Font’s 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); |
Impostazione del pedice
Applica il pedice al testo con la proprietà Font’s 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); |