Paramètres de remplissage

Couleurs et motifs de fond

Microsoft Excel peut définir les couleurs de premier plan (contour) et d’arrière-plan (remplissage) des cellules et des motifs d’arrière-plan.

Aspose.Cells prend également en charge ces fonctionnalités de manière flexible. Dans cette rubrique, nous apprenons à utiliser ces fonctionnalités en utilisant Aspose.Cells.

Définition des couleurs et des motifs d’arrière-plan

Aspose.Cells fournit une classe,Cahier qui représente un fichier Excel Microsoft. LeCahier classe contient unFeuilles de travail collection qui permet d’accéder à chaque feuille de calcul dans le fichier Excel. Une feuille de calcul est représentée par leFeuille de travail classe. LeFeuille de travail la classe offre uneCells le recueil. Chaque élément de laCells collection représente un objet de laCellclasse.

LeCell a laObtenirStyle etDéfinirStyle méthodes utilisées pour obtenir et définir la mise en forme d’une cellule. LeStylefournit des propriétés pour définir les couleurs de premier plan et d’arrière-plan des cellules. Aspose.Cells fournit unType d’arrière-planénumération qui contient un ensemble de types prédéfinis de motifs d’arrière-plan qui sont donnés ci-dessous.

Motifs de fond Description
DiagonaleHachure Représente le motif hachuré en diagonale
Bande diagonale Représente le motif à rayures diagonales
Gris6 Représente un motif gris de 6,25 %
Gris12 Représente un motif gris de 12,5 %
Gris25 Représente 25 % de motif gris
Gris50 Représente un motif gris à 50 %
Gris75 Représente un motif gris à 75 %
Bande horizontale Représente le motif de rayures horizontales
Aucun Ne représente aucun arrière-plan
Bande diagonale inversée Représente le motif à rayures diagonales inversées
Solide Représente un motif solide
ÉpaisDiagonalCrosshatch Représente un motif hachuré diagonal épais
MinceDiagonalCrosshatch Représente un motif de hachures diagonales minces
Fine bande diagonale Représente un motif à fines rayures diagonales
ThinHorizontalCrosshatch Représente un motif hachuré horizontal fin
Fines rayures horizontales Représente un motif de fines rayures horizontales
ThinReverseDiagonalStripe Représente un motif à fines rayures diagonales inversées
ThinVerticalStripe Représente un motif de fines rayures verticales
Bande verticale Représente le motif de rayures verticales

Dans l’exemple ci-dessous, la couleur de premier plan de la cellule A1 est définie, mais A2 est configuré pour avoir à la fois des couleurs de premier plan et d’arrière-plan avec un motif d’arrière-plan à rayures verticales.

// 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 Workbook object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Define a Style and get the A1 cell style
Style style = worksheet.Cells["A1"].GetStyle();
// Setting the foreground color to yellow
style.ForegroundColor = Color.Yellow;
// Setting the background pattern to vertical stripe
style.Pattern = BackgroundType.VerticalStripe;
// Apply the style to A1 cell
worksheet.Cells["A1"].SetStyle(style);
// Get the A2 cell style
style = worksheet.Cells["A2"].GetStyle();
// Setting the foreground color to blue
style.ForegroundColor = Color.Blue;
// Setting the background color to yellow
style.BackgroundColor = Color.Yellow;
// Setting the background pattern to vertical stripe
style.Pattern = BackgroundType.VerticalStripe;
// Apply the style to A2 cell
worksheet.Cells["A2"].SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);

Important à savoir

Application d’effets de remplissage dégradé

Pour appliquer les effets de remplissage dégradé souhaités à la cellule, utilisez leStyle objetsSetTwoColorGradientméthode en conséquence.

// 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);
// Instantiate a new Workbook
Workbook workbook = new Workbook();
// Get the first worksheet (default) in the workbook
Worksheet worksheet = workbook.Worksheets[0];
// Input a value into B3 cell
worksheet.Cells[2, 1].PutValue("test");
// Get the Style of the cell
Style style = worksheet.Cells["B3"].GetStyle();
// Set Gradient pattern on
style.IsGradient = true;
// Specify two color gradient fill effects
style.SetTwoColorGradient(Color.FromArgb(255, 255, 255), Color.FromArgb(79, 129, 189), GradientStyleType.Horizontal, 1);
// Set the color of the text in the cell
style.Font.Color = Color.Red;
// Specify horizontal and vertical alignment settings
style.HorizontalAlignment = TextAlignmentType.Center;
style.VerticalAlignment = TextAlignmentType.Center;
// Apply the style to the cell
worksheet.Cells["B3"].SetStyle(style);
// Set the third row height in pixels
worksheet.Cells.SetRowHeightPixel(2, 53);
// Merge the range of cells (B3:C3)
worksheet.Cells.Merge(2, 1, 1, 2);
// Save the Excel file
workbook.Save(dataDir + "output.xlsx");

Couleurs et Palette

Une palette est le nombre de couleurs disponibles pour la création d’une image. L’utilisation d’une palette standardisée dans une présentation permet à l’utilisateur de créer un look cohérent. Chaque fichier Excel Microsoft (97-2003) possède une palette de 56 couleurs pouvant être appliquées aux cellules, polices, quadrillages, objets graphiques, remplissages et lignes d’un graphique.

Avec Aspose.Cells, il est possible non seulement d’utiliser les couleurs existantes de la palette, mais également des couleurs personnalisées. Avant d’utiliser une couleur personnalisée, ajoutez-la d’abord à la palette.

Cette rubrique explique comment ajouter des couleurs personnalisées à la palette.

Ajout de couleurs personnalisées à la palette

Aspose.Cells prend en charge la palette de 56 couleurs d’Excel Microsoft. Pour utiliser une couleur personnalisée qui n’est pas définie dans la palette, ajoutez la couleur à la palette.

Aspose.Cells fournit une classe,Cahier , qui représente un fichier Excel Microsoft. LeCahier la classe offre uneChangerPalette méthode qui prend les paramètres suivants pour ajouter une couleur personnalisée pour modifier la palette :

  • Couleur personnalisée, la couleur personnalisée à ajouter.
  • Index, l’index de la couleur dans la palette que la couleur personnalisée remplacera. Doit être compris entre 0 et 55.

L’exemple ci-dessous ajoute une couleur personnalisée (Orchidée) à la palette avant de l’appliquer sur une police.

// Instantiating an Workbook object
Workbook workbook = new Workbook();
//Checks if a color is in the palette for the spreadsheet.
Console.WriteLine(workbook.IsColorInPalette(Color.Orchid));
// Adding Orchid color to the palette at 55th index
workbook.ChangePalette(Color.Orchid, 55);
Console.WriteLine(workbook.IsColorInPalette(Color.Orchid));
// 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
Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Defining new Style object
Style styleObject = workbook.CreateStyle();
// Setting the Orchid (custom) color to the font
styleObject.Font.Color = workbook.Colors[55];
// Applying the style to the cell
cell.SetStyle(styleObject);
// Saving the Excel file
workbook.Save("out.xlsx");