Show / Hide Table of Contents

Conditional Formatting Cell Formatting

Output: ConditionalFormattingFormatting.xlsx

public void Run()
{
    var workbook = new Workbook();
    var worksheet = workbook.Worksheets[0];
    worksheet.Columns[0].WidthCharacters = 40;
    worksheet.Columns.SetWidthCharacters("B", "F", 5);

    worksheet["A1"].Value = "Font Bold";
    worksheet.GetRange("B1:F1").SetValue(new int[] { 1, 2, 3, 4, 5 });
    var ruleB1 = worksheet.ConditionalFormattings.AddCellValue("B1:F1",ConditionalFormattingOperator.Equal,1);
    ruleB1.Font = Font.Create().SetBold();

    worksheet["A2"].Value = "Font Double Underline";
    worksheet.GetRange("B2:F2").SetValue(new int[] { 1, 2, 3, 4, 5 });
    var ruleB2 = worksheet.ConditionalFormattings.AddCellValue("B2:F2", ConditionalFormattingOperator.Equal, 2);
    ruleB2.Font = Font.Create().WithUnderline(SpreadsheetUnderline.Double);

    worksheet["A3"].Value = "Border Thin Red";
    worksheet.GetRange("B3:F3").SetValue(new int[] { 1, 2, 3, 4, 5 });
    var ruleB3 = worksheet.ConditionalFormattings.AddCellValue("B3:F3", ConditionalFormattingOperator.Equal, 3);
    ruleB3.Borders = Borders.CreateBoxBorder(BorderStyle.Thin, SpreadsheetColor.Red);

    worksheet["A4"].Value = "Fill Red Light Trellis";
    worksheet.GetRange("B4:F4").SetValue(new int[] { 1, 2, 3, 4, 5 });
    var ruleB4 = worksheet.ConditionalFormattings.AddCellValue("B4:F4", ConditionalFormattingOperator.Equal, 4);
    ruleB4.Fill = CellFill.CreatePattern(SpreadsheetColor.Automatic, SpreadsheetColor.Red, PatternType.LightTrellis);

    worksheet["A5"].Value = "Number Format Percentage";
    worksheet.GetRange("B5:F5").SetValue(new int[] { 1, 2, 3, 4, 5 });
    var ruleB5 = worksheet.ConditionalFormattings.AddCellValue("B5:F5", ConditionalFormattingOperator.Equal, 5);
    ruleB5.NumberFormat = new NumberFormat("0%");

    workbook.SaveAs("ConditionalFormattingFormatting.xlsx");
}
Back to top Generated by DocFX