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()); }
时间: 2024-10-07 21:03:16