Simulating a Checkbox with Conditional Formatting in Telerik SpreadProcessing
Environment
| Product Version | Product | Author |
|---|---|---|
| 2025.3.806 | SpreadProcessing | Yoan Karamanov |
Description
This article describes how to use the SpreadProcessing 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.
This knowledge base article also answers the following questions:
- How to simulate checkbox behavior in Telerik SpreadProcessing?
- How to use conditional formatting for representing checkboxes?
- How to display different symbols based on cell values in Telerik SpreadProcessing?
Solution
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.
- Create a Workbook and add a worksheet.
- Define a DifferentialFormatting for the checked state with a format that displays "☑".
- Create an EqualToRule for the checked state, matching cells with a value of 1.
- Apply the conditional formatting rule to the cell.
- Define another DifferentialFormatting for the unchecked state with a format that displays "☐".
- Create an EqualToRule for the unchecked state, matching cells with a value of 0.
- Apply the second conditional formatting rule to the cell.
- Set the cell value to 1 or 0 to test the checkbox simulation.
- Export the workbook to an XLSX file.
Here’s an example implementation:
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets.Add();
// Define formatting for checked state
DifferentialFormatting checkedFormatting = new DifferentialFormatting();
checkedFormatting.CellValueFormat = new CellValueFormat("☑");
EqualToRule checkedRule = new EqualToRule("1", checkedFormatting);
ConditionalFormatting checkedConditionalFormatting = new ConditionalFormatting(checkedRule);
worksheet.Cells[0, 0].AddConditionalFormatting(checkedConditionalFormatting);
// Define formatting for unchecked state
DifferentialFormatting uncheckedFormatting = new DifferentialFormatting();
uncheckedFormatting.CellValueFormat = new CellValueFormat("☐");
EqualToRule uncheckedRule = new EqualToRule("0", uncheckedFormatting);
ConditionalFormatting uncheckedConditionalFormatting = new ConditionalFormatting(uncheckedRule);
worksheet.Cells[0, 0].AddConditionalFormatting(uncheckedConditionalFormatting);
// Set initial value to simulate checkbox
worksheet.Cells[0, 0].SetValue(1);
// Export workbook to XLSX file
string xlsxOutputPath = "output.xlsx";
IWorkbookFormatProvider xlsxFormatProvider = new XlsxFormatProvider();
using (Stream output = new FileStream(xlsxOutputPath, FileMode.Create))
{
xlsxFormatProvider.Export(workbook, output, TimeSpan.FromSeconds(10));
}