我有两列,其中第1列包含问题,第2列包含答案类型代码。有两种类型的答案即。多项选择和是否。我需要用数据验证替换这些答案类型代码,即下拉列表。例如,如果第2列包含1,那么它应该被包含预定义选项的Drop Down替换,如果它包含2然后是另一个使用Yes-No选项下拉的话。 以下是生成我的excel的代码,尝试应用数据验证,它工作正常,只需看到代码块,将其添加到B列:
private void GenerateExcel(DataTable dt, string finalFileNameWithPath)
{
if (File.Exists(finalFileNameWithPath))
File.Delete(finalFileNameWithPath);
FileInfo newFile = new FileInfo(finalFileNameWithPath);
using (var package = new ExcelPackage(newFile))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("AnswerSheet");
worksheet.Cells["A1"].LoadFromDataTable(dt, true); //--dt is Datatable containing two columns i.e. Questions and Answer Type Code.
//--Following code place drop down with 6 options in column B
var val = worksheet.DataValidations.AddListValidation("B:B");
val.ShowErrorMessage = true;
val.ErrorTitle = "An invalid feedback was entered";
val.Error = "Please choose feedback from drop down only.";
val.Formula.Values.Add("Strongly Agree");
val.Formula.Values.Add("Agree");
val.Formula.Values.Add("Neutral");
val.Formula.Values.Add("Disagree");
val.Formula.Values.Add("Strongly Disagree");
val.Formula.Values.Add("NA");
//-- Following code save excel
package.Save();
package.Dispose();
}
}
任何人都可以帮助我吗?