Create dropdown list in Excel

  • NPOI
  • Spire.XLS
  • Download Sample Code

using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NPOI
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create workbook
            IWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = (XSSFSheet)workbook.CreateSheet("sheet");

            //Create dropdown list
            IDataValidationHelper validationHelper = new XSSFDataValidationHelper(sheet);
            CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
            IDataValidationConstraint constraint = validationHelper.CreateExplicitListConstraint(new String[] { "One", "Two", "Three", "Four" });
            IDataValidation dataValidation = validationHelper.CreateValidation(constraint, addressList);
            dataValidation.SuppressDropDownArrow = true;
            sheet.AddValidationData(dataValidation);

            //Save the file
            FileStream file = File.Create("ExcelDropdownList.xlsx");
            workbook.Write(file);
            file.Close();

            //Launch the file
            System.Diagnostics.Process.Start("ExcelDropdownList.xlsx");
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Xls;

namespace Spire.XLS
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create workbook
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];

            //Create dropdown list
            Validation validation = workbook.Worksheets[0].Range["A1"].DataValidation;
            validation.Values = new string[] { "One", "Two", "Three", "Four" };         

            //Save and Launch
            workbook.SaveToFile("ExcelDropdownList.xlsx", ExcelVersion.Version2013);
            System.Diagnostics.Process.Start("ExcelDropdownList.xlsx");
        }
    }
}