|
| 1 | +--- |
| 2 | +title: Simulating a Checkbox with Conditional Formatting in Telerik SpreadProcessing |
| 3 | +description: Learn how to simulate a checkbox using conditional formatting with Telerik SpreadProcessing. |
| 4 | +type: how-to |
| 5 | +page_title: Simulating a Checkbox Using Conditional Formatting with Telerik SpreadProcessing |
| 6 | +meta_title: Simulating a Checkbox Using Conditional Formatting with Telerik SpreadProcessing |
| 7 | +slug: spreadprocessing-simulating-checkbox-conditional-formatting |
| 8 | +tags: spread, processing, conditional, formatting, check, box, telerik, document, xlsx |
| 9 | +res_type: kb |
| 10 | +ticketid: 1667088 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +|Product Version|Product|Author| |
| 16 | +|----|----|----| |
| 17 | +|2025.3.806|[SpreadProcessing]({%slug radspreadprocessing-overview%})|[Yoan Karamanov](https://www.telerik.com/blogs/author/yoan-karamanov)| |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +This article describes how to use the [SpreadProcessing]({%slug radspreadprocessing-overview%}) library to simulate a checkbox by using conditional formatting rules. The goal is to display a checked symbol (☑) when the cell value is **1** and an unchecked symbol (☐) when the cell value is **0**. |
| 22 | + |
| 23 | +This knowledge base article also answers the following questions: |
| 24 | +- How to simulate checkbox behavior in Telerik SpreadProcessing? |
| 25 | +- How to use conditional formatting for representing checkboxes? |
| 26 | +- How to display different symbols based on cell values in Telerik SpreadProcessing? |
| 27 | + |
| 28 | +## Solution |
| 29 | + |
| 30 | +To simulate a checkbox using conditional formatting in Telerik SpreadProcessing, apply two separate conditional formatting rules: one for the `checked` state and one for the `unchecked` state. |
| 31 | + |
| 32 | +1. Create a [Workbook]({%slug radspreadprocessing-working-with-workbooks-what-is-workbook%}) and add a worksheet. |
| 33 | +2. Define a [DifferentialFormatting]({%slug radspreadprocessing-features-conditional-formatting%}) for the checked state with a format that displays "☑". |
| 34 | +3. Create an **EqualToRule** for the checked state, matching cells with a value of **1**. |
| 35 | +4. Apply the conditional formatting rule to the cell. |
| 36 | +5. Define another **DifferentialFormatting** for the unchecked state with a format that displays "☐". |
| 37 | +6. Create an **EqualToRule** for the unchecked state, matching cells with a value of **0**. |
| 38 | +7. Apply the second conditional formatting rule to the cell. |
| 39 | +8. Set the cell value to **1** or **0** to test the checkbox simulation. |
| 40 | +9. Export the workbook to an XLSX file. |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +Here’s an example implementation: |
| 45 | + |
| 46 | +```csharp |
| 47 | +Workbook workbook = new Workbook(); |
| 48 | +Worksheet worksheet = workbook.Worksheets.Add(); |
| 49 | + |
| 50 | +// Define formatting for checked state |
| 51 | +DifferentialFormatting checkedFormatting = new DifferentialFormatting(); |
| 52 | +checkedFormatting.CellValueFormat = new CellValueFormat("☑"); |
| 53 | +EqualToRule checkedRule = new EqualToRule("1", checkedFormatting); |
| 54 | +ConditionalFormatting checkedConditionalFormatting = new ConditionalFormatting(checkedRule); |
| 55 | +worksheet.Cells[0, 0].AddConditionalFormatting(checkedConditionalFormatting); |
| 56 | + |
| 57 | +// Define formatting for unchecked state |
| 58 | +DifferentialFormatting uncheckedFormatting = new DifferentialFormatting(); |
| 59 | +uncheckedFormatting.CellValueFormat = new CellValueFormat("☐"); |
| 60 | +EqualToRule uncheckedRule = new EqualToRule("0", uncheckedFormatting); |
| 61 | +ConditionalFormatting uncheckedConditionalFormatting = new ConditionalFormatting(uncheckedRule); |
| 62 | +worksheet.Cells[0, 0].AddConditionalFormatting(uncheckedConditionalFormatting); |
| 63 | + |
| 64 | +// Set initial value to simulate checkbox |
| 65 | +worksheet.Cells[0, 0].SetValue(1); |
| 66 | + |
| 67 | +// Export workbook to XLSX file |
| 68 | +string xlsxOutputPath = "output.xlsx"; |
| 69 | +IWorkbookFormatProvider xlsxFormatProvider = new XlsxFormatProvider(); |
| 70 | + |
| 71 | +using (Stream output = new FileStream(xlsxOutputPath, FileMode.Create)) |
| 72 | +{ |
| 73 | + xlsxFormatProvider.Export(workbook, output, TimeSpan.FromSeconds(10)); |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +## See Also |
| 78 | + |
| 79 | +* [Conditional Formatting]({%slug radspreadprocessing-features-conditional-formatting%}) |
| 80 | +* [Workbook]({%slug radspreadprocessing-working-with-workbooks-what-is-workbook%}) |
| 81 | +* [Using XlsxFormatProvider]({%slug radspreadprocessing-formats-and-conversion-xlsx-xlsxformatprovider%}) |
0 commit comments