如何以编程方式在Excel工作表中添加下拉菜单?

发布于 2024-08-09 02:49:31 字数 115 浏览 3 评论 0原文

我正在使用 ExcelPackage dll 从 .net 应用程序创建 Excel。

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

我还想对单元格值进行一些格式更改。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半岛未凉 2024-08-16 02:49:31
    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();
        }
    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();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文