Appliquer la mise en forme conditionnelle dans la feuille de calcul
Scénario d’utilisation possible
Aspose.Cells vous permet d’ajouter toutes sortes de formatage conditionnel, par exemple Formule, Au-dessus de la moyenne, Échelle de couleurs, Barre de données, Jeu d’icônes, Top10, etc. Il fournit leIFormatConditionclasse qui possède toutes les méthodes nécessaires pour appliquer la mise en forme conditionnelle de votre choix. Voici la liste de quelques-unes des méthodes Get.
Appliquer la mise en forme conditionnelle dans la feuille de calcul
L’exemple de code suivant montre comment ajouter une mise en forme conditionnelle de valeur Cell sur les cellules A1 et B2. Veuillez consulter lefichier excel de sortie généré par le code et la capture d’écran suivante qui explique l’effet du code sur lefichier excel de sortie. Si vous mettez une valeur supérieure à 100 dans les cellules A2 et B2, la couleur de remplissage rouge des cellules A1 et B2 disparaîtra.
Exemple de code
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of output excel file | |
StringPtr outputApplyConditionalFormattingInWorksheet = outPath->StringAppend(new String("outputApplyConditionalFormattingInWorksheet.xlsx")); | |
//Create an empty workbook | |
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook(); | |
//Access first worksheet | |
intrusive_ptr<IWorksheet> ws = wb->GetIWorksheets()->GetObjectByIndex(0); | |
//Adds an empty conditional formatting | |
int idx = ws->GetIConditionalFormattings()->Add(); | |
intrusive_ptr<IFormatConditionCollection> fcs = ws->GetIConditionalFormattings()->GetObjectByIndex(idx); | |
//Set the conditional format range | |
intrusive_ptr<ICellArea> ca = ICellArea::CreateICellArea(new String("A1"), new String("A1")); | |
fcs->AddArea(ca); | |
ca = ICellArea::CreateICellArea(new String("B2"), new String("B2")); | |
fcs->AddArea(ca); | |
//Add condition and set the background color | |
idx = fcs->AddCondition(FormatConditionType_CellValue, OperatorType_Between, new String("=A2"), new String("100")); | |
intrusive_ptr<IFormatCondition> fc = fcs->GetObjectByIndex(idx); | |
fc->GetIStyle()->SetBackgroundColor(Systems::Drawing::Color::GetRed()); | |
//User friendly message to test the output excel file. | |
StringPtr msgStr = new String("Red color in cells A1 and B2 is because of Conditional Formatting. Put 101 or any value >100 in cell A2 and B2, you will see Red background color will be gone."); | |
ws->GetICells()->GetObjectByIndex(new String("A10"))->PutValue(msgStr); | |
//Save the output excel file | |
wb->Save(outputApplyConditionalFormattingInWorksheet); |