|
| 1 | +--- |
| 2 | +title: Adding a DropDown/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 /ComboBox in Excel Using RadSpreadProcessing |
| 6 | +slug: adding-combobox-to-excel-file-radspreadprocessing |
| 7 | +tags: spread, processing, combobox, excel, listdata, validation, document, dropdown, comma, delimiter |
| 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 | +Learn how to add a ComboBox with predefined options to an Excel file programmatically using [RadSpreadProcessing]({%slug radspreadprocessing-overview%}). Additionally, the ComboBox should support special characters like commas (or another delimiter) within the options. |
| 21 | + |
| 22 | +## Solution |
| 23 | + |
| 24 | +To add a DropDown/ComboBox to an Excel file and handle special characters like commas within options, use the [ListDataValidationRule]({%slug radspreadprocessing-features-data-validation%}) with a cell range as the source for the validation. Consider the following sample options: |
| 25 | + |
| 26 | +* Option 1, Inc. |
| 27 | +* Option 2, Inc. |
| 28 | +* Option 3, Inc. |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +Below is the sample code: |
| 33 | + |
| 34 | +```csharp |
| 35 | +// Create a workbook |
| 36 | +Workbook workbook = new Workbook(); |
| 37 | +Worksheet worksheet = workbook.Worksheets.Add(); |
| 38 | + |
| 39 | +// Define the ComboBox options in a cell range |
| 40 | +worksheet.Cells[0, 0].SetValue("ABC, Inc."); |
| 41 | +worksheet.Cells[0, 1].SetValue("123, Inc."); |
| 42 | +worksheet.Cells[0, 2].SetValue("KEF, Inc."); |
| 43 | +worksheet.Cells[0, 3].SetValue("HMS, Inc."); |
| 44 | + |
| 45 | +// Specify the cell where the ComboBox will be applied (e.g., A2) |
| 46 | +CellIndex cellIndex = new CellIndex(1, 0); // A2 |
| 47 | +
|
| 48 | +// Set up the validation rule with the cell range |
| 49 | +ListDataValidationRuleContext context = new ListDataValidationRuleContext(worksheet, cellIndex); |
| 50 | +context.InputMessageTitle = "Restricted input"; |
| 51 | +context.InputMessageContent = "Please select an option from the dropdown."; |
| 52 | +context.ErrorStyle = ErrorStyle.Stop; |
| 53 | +context.ErrorAlertTitle = "Invalid Input"; |
| 54 | +context.ErrorAlertContent = "The entered value is not valid. Allowed values are listed in the dropdown."; |
| 55 | +context.Argument1 = "=A1:D1"; // Cell range containing the options |
| 56 | +ListDataValidationRule rule = new ListDataValidationRule(context); |
| 57 | + |
| 58 | +// Apply the validation rule to the specified cell |
| 59 | +worksheet.Cells[cellIndex].SetDataValidationRule(rule); |
| 60 | + |
| 61 | +// Save the workbook to a file |
| 62 | +string outputFilePath = "ComboBoxExample.xlsx"; |
| 63 | +File.Delete(outputFilePath); |
| 64 | +using (FileStream stream = new FileStream(outputFilePath, FileMode.Create)) |
| 65 | +{ |
| 66 | + XlsxFormatProvider formatProvider = new XlsxFormatProvider(); |
| 67 | + formatProvider.Export(workbook, stream, TimeSpan.FromSeconds(60)); |
| 68 | +} |
| 69 | + |
| 70 | +// Open the generated file |
| 71 | +Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true }); |
| 72 | +``` |
| 73 | + |
| 74 | +### Key Points |
| 75 | +- Use a cell range to define options if the values include commas or other special characters that are not allowed out-of-the-box. |
| 76 | +- The `Argument1` property supports referencing a range of cells (e.g., `=A1:D1`) as dropdown options. |
| 77 | + |
| 78 | +## See Also |
| 79 | + |
| 80 | +- [RadSpreadProcessing Overview]({%slug radspreadprocessing-overview%}) |
| 81 | +- [List Rule Data Validation]({%slug radspreadprocessing-features-data-validation%}#list-rule) |
| 82 | +- [Setting the Culture]({%slug radspreadprocessing-features-setting-the-culture%}) |
0 commit comments