Gestion des paramètres de police
L’apparence et le style du texte peuvent être contrôlés en modifiant ses paramètres de police. Ces paramètres de police peuvent inclure le nom, le style, la taille, la couleur et d’autres effets des polices comme indiqué ci-dessous dans la figure :
Paramètres de police dans Microsoft Excel
Tout comme Microsoft Excel, Aspose.Cells prend également en charge la configuration des paramètres de police des cellules.
Configuration des paramètres de police
Aspose.Cells fournit une classe, Workbook qui représente un fichier Microsoft Excel. La classe Workbook contient une WorksheetCollection qui permet d’accéder à chaque feuille de calcul dans un fichier Excel. Une feuille de calcul est représentée par la classe Worksheet. La classe Worksheet fournit une collection de Cells. Chaque élément de la collection Cells représente un objet de la classe Cell.
Aspose.Cells fournit la méthode setStyle de la classe Cell, utilisée pour définir la mise en forme d’une cellule. De plus, l’objet de la classe Style fournit des propriétés pour configurer les paramètres de la police.
Cet article montre comment :
- Appliquer une police spécifique au texte.
- Définir le texte en gras.
- Définir la taille de la police.
- Définir la couleur de la police.
- Souligner le texte.
- Barrer le texte.
- Définir le texte en indice.
- Définir le texte en exposant.
Définition du nom de la police
Appliquer une police spécifique au texte dans les cellules en utilisant la propriété setName de l’objet 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"); |
Définition du style de police en gras
Mettre le texte en gras en définissant la propriété setBold de l’objet Font sur 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); |
Définition de la taille de police
Définir la taille de la police en utilisant la propriété setSize de l’objet 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"); |
Définition du type de soulignement de la police
Souligner le texte avec la propriété setUnderline de l’objet Font. Aspose.Cells offre différents types de soulignement de police prédéfinis dans l’énumération FontUnderlineType.
Types de soulignement de police | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
NONE | Pas de soulignement | ||||||||||||||||||||||||||||||||||||||||||||||||||
SINGLE | Un seul soulignement | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOUBLE | Double souligné | ||||||||||||||||||||||||||||||||||||||||||||||||||
ACCOUNTING | Un seul souligné comptable | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOUBLE_ACCOUNTING | Double souligné comptable | ||||||||||||||||||||||||||||||||||||||||||||||||||
DASH | Souligné en pointillés | ||||||||||||||||||||||||||||||||||||||||||||||||||
DASH_DOT_DOT_HEAVY | Souligné épais trait-point-point | ||||||||||||||||||||||||||||||||||||||||||||||||||
DASH_DOT_HEAVY | Souligné épais trait-point | ||||||||||||||||||||||||||||||||||||||||||||||||||
DASHED_HEAVY | Souligné épais pointillé | ||||||||||||||||||||||||||||||||||||||||||||||||||
DASH_LONG | Souligné long pointillé | ||||||||||||||||||||||||||||||||||||||||||||||||||
DASH_LONG_HEAVY | Souligné épais long pointillé | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOT_DASH | Souligné trait-point | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOT_DOT_DASH | Souligné trait-point-point | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOTTED | Souligné en pointillés | ||||||||||||||||||||||||||||||||||||||||||||||||||
DOTTED_HEAVY | Souligné épais en pointillés | ||||||||||||||||||||||||||||||||||||||||||||||||||
HEAVY | Souligné épais | ||||||||||||||||||||||||||||||||||||||||||||||||||
WAVE | Souligné ondulé | ||||||||||||||||||||||||||||||||||||||||||||||||||
WAVY_DOUBLE | Double souligné ondulé | ||||||||||||||||||||||||||||||||||||||||||||||||||
WAVY_HEAVY | Souligné ondulé épais | ||||||||||||||||||||||||||||||||||||||||||||||||||
WORDS | Souligné uniquement pour les caractères non-espaces | ||||||||||||||||||||||||||||||||||||||||||||||||||
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
|
Définition de la couleur de police
Définir la couleur de la police avec l' Font objet’s setColor propriété. Sélectionnez une couleur quelconque dans l’énumération Color et attribuez la couleur sélectionnée à l' Font objet’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"); |
Paramétrage de l’effet de barré sur le texte
Barrer le texte avec l’objet Font et la propriété 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); |
Paramétrage de l’indice
Mettre le texte en exposant en utilisant l’objet Font et la propriété 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); |
Paramétrage de l’indice supérieur
Appliquer un exposant au texte avec l’objet Font et la propriété 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); |