Paramètres de la police

Configuration des paramètres de police

Aspose.Cells fournit une classe, Workbook qui représente un fichier Microsoft Excel. La classe Workbook contient une collection Worksheets 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 Cells. Chaque élément de la collection Cells représente un objet de la classe Cell.

Aspose.Cells fournit les méthodes GetStyle et SetStyle de la classe Cell utilisées pour obtenir et définir le style de mise en forme d’une cellule. La classe Style fournit des propriétés pour configurer les paramètres de police.

Définition du nom de la police

Les développeurs peuvent appliquer n’importe quelle police au texte à l’intérieur d’une cellule en utilisant la propriété Name de l’objet Style.Font.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["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 name to "Times New Roman"
style.Font.Name = "Times New Roman";
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);

Définition du style de police en gras

Les développeurs peuvent mettre en gras le texte en définissant la propriété IsBold de l’objet Style.Font sur true.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["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.Font.IsBold = true;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save("out.xlsx");

Définition de la taille de police

Définissez la taille de la police avec la propriété Size de l’objet Style.Font.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["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 size to 14
style.Font.Size = 14;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save("out.xlsx");

Définition de la couleur de police

Utilisez la propriété Color de l’objet Style.Font pour définir la couleur de la police. Sélectionnez une couleur dans l’énumération Color (partie du framework .NET) et attribuez-la à la propriété Color.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["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 color to blue
style.Font.Color = Color.Blue;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save("out.xlsx");

Définition du type de soulignement de la police

Utilisez la propriété Underline de l’objet Style.Font pour souligner le texte. Aspose.Cells propose divers types de soulignement de police prédéfinis dans l’énumération FontUnderlineType.

Types de soulignement de police Description
Accounting Un soulignement de comptabilité unique
Double Double soulignement
DoubleAccounting Double soulignement de comptabilité
None Pas de soulignement
Single Un seul soulignement
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["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 to be underlined
style.Font.Underline = FontUnderlineType.Single;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "out.xlsx");

Définition de l’effet Barré

Appliquer l’effet barré en définissant la propriété IsStrikeout de l’objet Style.Font à true.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting the strike out effect on the font
style.Font.IsStrikeout = true;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "out.xlsx");

Définir l’effet de bas indice

Appliquer le bas indice en définissant la propriété IsSubScript de l’objet Style.Font à true.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting subscript effect
style.Font.IsSubscript = true;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "out.xlsx");

Définition de l’effet exposant sur la police

Les développeurs peuvent appliquer l’effet exposant sur la police en définissant la propriété IsSuperscript de l’objet Style.Font à true.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting superscript effect
style.Font.IsSuperscript = true;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "out.xlsx");

Sujets avancés