Create Excel Dropdown List in C#, VB.NET

Excel dropdown list is one type of data validation. It enables users to list several options for cells and select one option as cell value. Solution in this guide introduces how to add Excel dropdown list for a specified cell in C# and VB.NET with Spire.XLS for .NET. And the following screenshot presents result after programming.

Excel Dropdown List

Spire.XLS for .NET provides XlsRange class which contains DataValidation property to represent data validation for specified cell range. Three properties bool IsListFormula, String[] Value and bool IsSuppressDropDownArrow should be set to create a dropdown list. The value for the first property must be a String array so that you can initialize a String[] instance with a series of Unicode characters at first. Set the second property as true to form a list which saves initialized String array and the third property as true to present dropdown list arrow on the right of cell to indicate that a dropdown list is applied for this cell. Download and install Spire.XLS for .NET and follow the code:

[C#]
using System;
using Spire.Xls;

namespace DropdownList
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();     
            Worksheet sheet = workbook.Worksheets[0];
            sheet.Range["A1"].Value = "e-iceblue";
            sheet.Range["A2"].Value = "Spire.XLS";
            sheet.Range["A3"].Value = "DropDownList";

            CellRange range = workbook.Worksheets[0].Range["A1:A3"];
            workbook.Worksheets[0].Range["C2"].DataValidation.DataRange = range;

            workbook.SaveToFile("ExcelDropdownList.xlsx", ExcelVersion.Version2010);
         }
    }
}
          
[VB.NET]
Imports System
Imports Spire.Xls
Module Module1

    Sub Main()
        Dim workbook As New Workbook()
        Dim sheet As Worksheet = workbook.Worksheets(0)
        sheet.Range("A1").Value = "e-iceblue"
        sheet.Range("A2").Value = "Spire.XLS"
        sheet.Range("A3").Value = "DropDownList"

        Dim range As CellRange = workbook.Worksheets(0).Range("A1:A3")
        workbook.Worksheets(0).Range("C2").DataValidation.DataRange = range

        workbook.SaveToFile("ExcelDropdownList.xlsx", ExcelVersion.Version2010)
    End Sub

End Module
          

Spire.XLS, specified designed for developers, enables to generate, write, edit and save Excel workbooks (Excel 97-2003, Excel 2007 and Excel 2010) in .NET, Silverlight and WPF with C# and VB.NET.