Gestione delle impostazioni dei caratteri
L’aspetto del testo può essere controllato modificando le impostazioni del carattere. Queste impostazioni dei caratteri possono includere il nome, lo stile, la dimensione, il colore e altri effetti dei caratteri come mostrato di seguito nella figura:
Impostazioni dei caratteri in Microsoft Excel
Proprio come Microsoft Excel, Aspose.Cells supporta anche la configurazione delle impostazioni dei caratteri delle celle.
Configurazione delle impostazioni dei caratteri
Aspose.Cells offre un corso,Cartella di lavoro che rappresenta un file Excel Microsoft. IlCartella di lavoro la classe contiene unRaccolta di fogli di lavoro che consente l’accesso a ciascun foglio di lavoro in un file Excel. Un foglio di lavoro è rappresentato daFoglio di lavoroclasse. IlFoglio di lavoro la classe fornisce aCells collezione. Ogni elemento delCells collezione rappresenta un oggetto dellaCellclasse.
Aspose.Cells fornisce ilCell classe'setStyle, utilizzato per impostare la formattazione di una cella. Inoltre, l’oggetto delStileclass fornisce le proprietà per la configurazione delle impostazioni dei caratteri.
Questo articolo mostra come:
- Applicare un carattere specifico al testo.
- Imposta il testo in grassetto.
- Imposta la dimensione del carattere.
- Imposta il colore del carattere.
- Sottolineare il testo.
- Testo barrato.
- Imposta il testo in pedice.
- Imposta il testo in apice.
Impostazione del nome del carattere
Applicare un carattere specifico al testo nelle celle utilizzando ilFont dell’oggettoimposta nomeproprietà.
// 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"); |
Impostazione dello stile del carattere su grassetto
Impostare il testo in grassetto impostando ilFont dell’oggettosetBold proprietà aVERO.
// 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
Impostare la dimensione del carattere utilizzando ilFont dell’oggettosetSizeproprietà.
// 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 di sottolineatura del carattere
Sottolineare il testo con ilFont dell’oggettosetSottolineato proprietà. Aspose.Cells offre vari tipi di sottolineatura dei caratteri predefiniti nel fileFontUnderlineTypeenumerazione.
Tipi di caratteri sottolineati | Descrizione | ||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
NESSUNO | Nessuna sottolineatura | ||||||||||||||||||||||||||||||||||||||||||||||||||
SEPARARE | Una sola sottolineatura | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOPPIO | Doppia sottolineatura | ||||||||||||||||||||||||||||||||||||||||||||||||||
CONTABILITÀ | Un’unica sottolineatura contabile | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOPPIO_CONTO | Doppia sottolineatura contabile | ||||||||||||||||||||||||||||||||||||||||||||||||||
TRATTINO | Sottolineatura tratteggiata | ||||||||||||||||||||||||||||||||||||||||||||||||||
TRATTINO_PUNTO_PUNTO_PESANTE | Sottolineatura tratteggiata-punto-punto spessa | ||||||||||||||||||||||||||||||||||||||||||||||||||
TRATTINO_PUNTO_PESANTE | Spessa sottolineatura tratteggiata | ||||||||||||||||||||||||||||||||||||||||||||||||||
TRATTEGGIATO_PESANTE | Sottolineatura tratteggiata spessa | ||||||||||||||||||||||||||||||||||||||||||||||||||
DASH_LUNGO | Sottolineatura tratteggiata lunga | ||||||||||||||||||||||||||||||||||||||||||||||||||
TRATTINO_LUNGO_PESANTE | Spessa sottolineatura tratteggiata lunga | ||||||||||||||||||||||||||||||||||||||||||||||||||
PUNTO_LINEA | Tratto-punto sottolineato | ||||||||||||||||||||||||||||||||||||||||||||||||||
PUNTO_PUNTO_TRATTINO | Trattino-punto-punto sottolineato | ||||||||||||||||||||||||||||||||||||||||||||||||||
PUNTEGGIATO | Sottolineatura punteggiata | ||||||||||||||||||||||||||||||||||||||||||||||||||
PUNTEGGIATO_PESANTE | Sottolineatura punteggiata spessa | ||||||||||||||||||||||||||||||||||||||||||||||||||
PESANTE | Sottolineatura spessa | ||||||||||||||||||||||||||||||||||||||||||||||||||
ONDA | Onda sottolineata | ||||||||||||||||||||||||||||||||||||||||||||||||||
WAVY_DOUBLE | Sottolineatura a doppia onda | ||||||||||||||||||||||||||||||||||||||||||||||||||
ONDULATO_PESANTE | Sottolineatura dell’onda pesante | ||||||||||||||||||||||||||||||||||||||||||||||||||
PAROLE |
Sottolinea solo caratteri diversi dallo 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 carattere con ilFont dell’oggettosetColor proprietà. Seleziona qualsiasi colore daColore enumerazione e assegnare il colore selezionato al fileFont dell’oggettosetColor.
// 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 ilFont dell’oggettosetSbarramentoproprietà.
// 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 pedice
Rendi il testo in apice usando ilFont dell’oggettosetSubscriptproprietà.
// 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 apice
Applica l’apice al testo con ilFont dell’oggettosetApiceproprietà.
// 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); |