Skip to content

Commit 4a48da4

Browse files
author
KB Bot
committed
Added new kb article adding-combobox-to-excel-file-radspreadprocessing
1 parent 96efd9e commit 4a48da4

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
title: Adding a ComboBox to an Excel File Using RadSpreadProcessing
3+
description: Explains how to add a combo box with specific options to an Excel file using RadSpreadProcessing.
4+
type: how-to
5+
page_title: Adding a Dropdown in Excel Using RadSpreadProcessing
6+
slug: adding-combobox-to-excel-file-radspreadprocessing
7+
tags: radspreadprocessing, combobox, excel, listdata, datavalidation, document-processing
8+
res_type: kb
9+
ticketid: 1689410
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| ---- | ---- | ---- |
16+
| 2025.2.520| RadSpreadProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
17+
18+
## Description
19+
20+
I want to add a ComboBox with predefined options to an Excel file programmatically using RadSpreadProcessing. Additionally, I need the ComboBox to support special characters like commas within the options.
21+
22+
This knowledge base article also answers the following questions:
23+
- How can I add comboboxes with options to Excel files using RadSpreadProcessing?
24+
- How do I handle special characters in combobox options in an Excel file?
25+
- How can I use cell ranges for dropdown validation in RadSpreadProcessing?
26+
27+
## Solution
28+
29+
To add a ComboBox to an Excel file and handle special characters like commas within options, use the `ListDataValidationRule` with a cell range as the source for the validation. Below is the sample code:
30+
31+
```csharp
32+
// Create a workbook
33+
Workbook workbook = new Workbook();
34+
Worksheet worksheet = workbook.Worksheets.Add();
35+
36+
// Define the ComboBox options in a cell range
37+
worksheet.Cells[0, 0].SetValue("ABC, Inc.");
38+
worksheet.Cells[0, 1].SetValue("123, Inc.");
39+
worksheet.Cells[0, 2].SetValue("KEF, Inc.");
40+
worksheet.Cells[0, 3].SetValue("HMS, Inc.");
41+
42+
// Specify the cell where the ComboBox will be applied (e.g., A2)
43+
CellIndex cellIndex = new CellIndex(1, 0); // A2
44+
45+
// Set up the validation rule with the cell range
46+
ListDataValidationRuleContext context = new ListDataValidationRuleContext(worksheet, cellIndex);
47+
context.InputMessageTitle = "Restricted input";
48+
context.InputMessageContent = "Please select an option from the dropdown.";
49+
context.ErrorStyle = ErrorStyle.Stop;
50+
context.ErrorAlertTitle = "Invalid Input";
51+
context.ErrorAlertContent = "The entered value is not valid. Allowed values are listed in the dropdown.";
52+
context.Argument1 = "=A1:D1"; // Cell range containing the options
53+
ListDataValidationRule rule = new ListDataValidationRule(context);
54+
55+
// Apply the validation rule to the specified cell
56+
worksheet.Cells[cellIndex].SetDataValidationRule(rule);
57+
58+
// Save the workbook to a file
59+
string outputFilePath = "ComboBoxExample.xlsx";
60+
File.Delete(outputFilePath);
61+
using (FileStream stream = new FileStream(outputFilePath, FileMode.Create))
62+
{
63+
XlsxFormatProvider formatProvider = new XlsxFormatProvider();
64+
formatProvider.Export(workbook, stream, TimeSpan.FromSeconds(60));
65+
}
66+
67+
// Open the generated file
68+
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
69+
```
70+
71+
### Key Points
72+
- Use a cell range to define options if the values include commas or other special characters.
73+
- The `Argument1` property supports referencing a range of cells (e.g., `=A1:D1`) as dropdown options.
74+
75+
## See Also
76+
77+
- [RadSpreadProcessing Overview](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/overview)
78+
- [List Rule Data Validation](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/features/data-validation#list-rule)
79+
- [Setting the Culture](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/features/setting-the-culture)
80+
- [RadSpreadProcessing API Reference](https://docs.telerik.com/devtools/document-processing/api/telerik.windows.documents.spreadsheet.model)

0 commit comments

Comments
 (0)