Search code examples
excelcombobox

add combobox to excel


I want to export some data as excel in my project. I prepared a structure for this. I want to have comboboxes in some columns in this structure.

I found the example below, but this example doesn't work for me. Normally he should add data one after the other, but he perceives them as a text and adds.

Example

It does not add the addition as a separate element. It adds "Item 1, Item 2, Item 3" into the combobox. How can I solve this problem.

Microsoft.Office.Interop.Excel version 15.0.0.0

I used Office 365.

var items = new List<string>() { "Item 1", "Item 2", "Item 3" };
        var formattedItems = string.Join(",", items.ToArray());

        var dropDownRange = sunWorksheet.Range["J2"].EntireColumn;
        dropDownRange.Validation.Delete();
        dropDownRange.Validation.Add(Excel.XlDVType.xlValidateList,
            Excel.XlDVAlertStyle.xlValidAlertInformation,
            Excel.XlFormatConditionOperator.xlBetween,
            formattedItems,
            Type.Missing);

        dropDownRange.Value = "Item 2";

Solution

  • Concatenating data with "," does not work. Instead of ";" Combining with fixed the problem.

    var items = new List<string>() { "Item 1", "Item 2", "Item 3" };
            var formattedItems = string.Join(";", items.ToArray());