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());  
}
  
复制代码

 

posted @   想念熊小米  阅读(3016)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示