How to add dropdown in excel sheet programmatically?

ぐ巨炮叔叔 提交于 2019-12-25 01:46:19

问题


I am using the ExcelPackage dll to creating the excel from the .net application.

I want to add drop down list in one of column of Excel sheet. How to add it?

Also I want to do some formatting changes to the cell values.


回答1:


Try spreadsheetgear ....




回答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();
        }


来源:https://stackoverflow.com/questions/1624095/how-to-add-dropdown-in-excel-sheet-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!