如何在Excel工作表添加下拉编程?(How to add dropdown in excel sh

2019-10-28 19:27发布

我现在用的是ExcelPackage DLL从.NET应用程序创建的Excel。

我想添加在Excel工作表中的列的一个下拉列表。 如何添加呢?

此外,我希望做一些格式更改单元格值。

Answer 1:

尝试的SpreadsheetGear ....



Answer 2:

    protected void AddDropDownToExcel(string path)
        {
            //path gives the location where u have created excel file
            string fileName = path.Replace("\\", "\\\\");
// F is the column name where you want to place the dropdown
            string RowCount = "F" + gridrowcount;
            // Open Excel and get first worksheet.      
            var workbook = application.Workbooks.Open(fileName);
            var worksheet = workbook.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;     
            // Set range for dropdownlist       
            var rangeNewStatus = worksheet.get_Range("F2", RowCount);
            rangeNewStatus.ColumnWidth = 20;
            rangeNewStatus.Validation.Add(Microsoft.Office.Interop.Excel.XlDVType.xlValidateList, Microsoft.Office.Interop.Excel.XlDVAlertStyle.xlValidAlertStop,
            Microsoft.Office.Interop.Excel.XlFormatConditionOperator.xlBetween, "dropdownlistitem1, dropdownlistitem2");
            // Save.
            workbook.Save();
            workbook.Close(Microsoft.Office.Interop.Excel.XlSaveAction.xlSaveChanges, Type.Missing, Type.Missing);
            application.Quit();
        }


文章来源: How to add dropdown in excel sheet programmatically?