C#动态给EXCEL列添加下拉选项
Microsoft.Office.Interop.Excel.Application excel=new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbooks workbooks=excel.Workbooks; Microsoft.Office.Interop.Excel.Workbook workbook=Workbooks.Add (Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); Microsoft.Office.Interop.Excel.WorkSheet workSheet= (Microsoft.Office.Interop.Excel.Worksheet) workbook.Worksheets[1]; ((Microsoft.Office.Interop.Excel.Range Range_Col=(Microsoft.Office.Interop.Excel.Range) worksheet.Columns.get_Item(1,Type.Missing); //加载下拉列表内容 string Name=GetName(DataTable);//查出的DataTable Range_Col.Validation.Add(Microsoft.Office.Interop.Excel.XlDVType.xlValidateList,Microsoft.Office.Interop.Excel.XlDVAlertStyle.xlValidAlertStop, Microsoft.Office.Interop.Excel.XlFormatConditionOperator.xlBetween,Name,Type.Missing);//Name就是上面获取的列表 private string GetName(DataTable dt) { var list=dt.AsEnumerable().Select(t=>t.Field<string>("Name")).ToList(); return string.Join(",",list.ToArray()); }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步