Définir des formats conditionnels des fichiers Excel et ODS.

Introduction

La mise en forme conditionnelle est une fonctionnalité avancée de Microsoft Excel qui vous permet d’appliquer des formats à une cellule ou à une plage de cellules et de faire en sorte que la mise en forme change en fonction de la valeur de la cellule ou de la valeur d’une formule. Par exemple, une cellule peut apparaître en gras uniquement lorsque sa valeur est supérieure à 500. Lorsque la valeur de la cellule satisfait la condition, le format spécifié est appliqué à la cellule. Si la valeur de la cellule ne satisfait pas la condition de format, la mise en forme par défaut de la cellule est utilisée. Dans Microsoft Excel, sélectionnez Format, puis Mise en forme conditionnelle pour ouvrir la boîte de dialogue Mise en forme conditionnelle.

Aspose.Cells prend en charge l’application de la mise en forme conditionnelle aux cellules à l’exécution. Cet article explique comment procéder. Il explique également comment calculer la couleur utilisée par Excel pour la mise en forme conditionnelle de l’échelle de couleurs.

Application de la mise en forme conditionnelle

Aspose.Cells prend en charge la mise en forme conditionnelle de plusieurs manières :

  • Utilisation de la feuille de calcul du concepteur
  • Utilisation de la méthode de copie.
  • Création de la mise en forme conditionnelle à l’exécution.

Utilisation de la feuille de calcul du concepteur

Les développeurs peuvent créer une feuille de calcul du concepteur contenant une mise en forme conditionnelle dans Microsoft Excel, puis ouvrir cette feuille de calcul avec Aspose.Cells. Aspose.Cells charge et enregistre la feuille de calcul du concepteur en conservant tous les paramètres de mise en forme conditionnelle.

Utilisation de la méthode de copie

Aspose.Cells permet aux développeurs de copier les paramètres de mise en forme conditionnelle d’une cellule à une autre dans la feuille de calcul en appelant la méthode Range.Copy().

// 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);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "Book1.xlsx", FileMode.Open);
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Copying conditional format settings from cell "A1" to cell "B1"
//worksheet.CopyConditionalFormatting(0, 0, 0, 1);
int TotalRowCount = 0;
for (int i = 0; i < workbook.Worksheets.Count; i++)
{
Worksheet sourceSheet = workbook.Worksheets[i];
Range sourceRange = sourceSheet.Cells.MaxDisplayRange;
Range destRange = worksheet.Cells.CreateRange(sourceRange.FirstRow + TotalRowCount, sourceRange.FirstColumn,
sourceRange.RowCount, sourceRange.ColumnCount);
destRange.Copy(sourceRange);
TotalRowCount = sourceRange.RowCount + TotalRowCount;
}
// Saving the modified Excel file
workbook.Save(dataDir + "output.xls");
// Closing the file stream to free all resources
fstream.Close();

Application de la mise en forme conditionnelle à l’exécution

Aspose.Cells vous permet d’ajouter et de supprimer la mise en forme conditionnelle à l’exécution. Les exemples de code ci-dessous montrent comment définir la mise en forme conditionnelle :

  1. Instancier un classeur.
  2. Ajouter une mise en forme conditionnelle vide.
  3. Définir la plage à laquelle la mise en forme doit s’appliquer.
  4. Définir les conditions de formatage.
  5. Enregistrez le fichier.

Après cet exemple, voici un certain nombre d’autres exemples plus petits qui montrent comment appliquer les paramètres de la police, les paramètres des bordures et les motifs.

Microsoft Excel 2007 a ajouté une mise en forme conditionnelle plus avancée que Aspose.Cells prend également en charge. Les exemples ici illustrent comment utiliser la mise en forme simple, les exemples de Microsoft Excel 2007 montrent comment appliquer une mise en forme conditionnelle plus avancée.

// 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);
string filePath = dataDir + "Book1.xlsx";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
// Adds an empty conditional formatting
int index = sheet.ConditionalFormattings.Add();
FormatConditionCollection fcs = sheet.ConditionalFormattings[index];
// Sets the conditional format range.
CellArea ca = new CellArea();
ca.StartRow = 0;
ca.EndRow = 0;
ca.StartColumn = 0;
ca.EndColumn = 0;
fcs.AddArea(ca);
ca = new CellArea();
ca.StartRow = 1;
ca.EndRow = 1;
ca.StartColumn = 1;
ca.EndColumn = 1;
fcs.AddArea(ca);
// Adds condition.
int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "=A2", "100");
// Adds condition.
int conditionIndex2 = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100");
// Sets the background color.
FormatCondition fc = fcs[conditionIndex];
fc.Style.BackgroundColor = Color.Red;
// Saving the Excel file
workbook.Save(dataDir + "output.xls");

Définir la police

// 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 weight to bold
style.Font.IsBold = true;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);

Définir la bordure

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
public static void Run()
{
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
// Adds an empty conditional formatting
int index = sheet.ConditionalFormattings.Add();
FormatConditionCollection fcs = sheet.ConditionalFormattings[index];
// Sets the conditional format range.
CellArea ca = new CellArea();
ca.StartRow = 0;
ca.EndRow = 5;
ca.StartColumn = 0;
ca.EndColumn = 3;
fcs.AddArea(ca);
// Adds condition.
int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100");
// Sets the background color.
FormatCondition fc = fcs[conditionIndex];
fc.Style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Dashed;
fc.Style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Dashed;
fc.Style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Dashed;
fc.Style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Dashed;
fc.Style.Borders[BorderType.LeftBorder].Color = Color.FromArgb(0, 255, 255);
fc.Style.Borders[BorderType.RightBorder].Color = Color.FromArgb(0, 255, 255);
fc.Style.Borders[BorderType.TopBorder].Color = Color.FromArgb(0, 255, 255);
fc.Style.Borders[BorderType.BottomBorder].Color = Color.FromArgb(255, 255, 0);
workbook.Save(dataDir + "output.xlsx");
}

Définir le motif

// 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);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
// Adds an empty conditional formatting
int index = sheet.ConditionalFormattings.Add();
FormatConditionCollection fcs = sheet.ConditionalFormattings[index];
// Sets the conditional format range.
CellArea ca = new CellArea();
ca.StartRow = 0;
ca.EndRow = 5;
ca.StartColumn = 0;
ca.EndColumn = 3;
fcs.AddArea(ca);
// Adds condition.
int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100");
FormatCondition fc = fcs[conditionIndex];
fc.Style.Pattern = BackgroundType.ReverseDiagonalStripe;
fc.Style.ForegroundColor = Color.FromArgb(255, 255, 0);
fc.Style.BackgroundColor = Color.FromArgb(0, 255, 255);
workbook.Save(dataDir + "output.xlsx");

Sujets avancés