码迷,mamicode.com
首页 > Windows程序 > 详细

C#动态给EXCEL列添加下拉选项

时间:2015-06-05 17:10:57      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:

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

 

C#动态给EXCEL列添加下拉选项

标签:

原文地址:http://www.cnblogs.com/softwaredeveloper/p/4554959.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!