Excel& EPPlus .NET库:如何从另一个工作表创建动态列表?

时间:2012-02-28 16:16:20

标签: .net excel xlsx epplus

我想在创建Excel文件(xlsx)时使用.NET EPPlus库从数据所在的不同工作表中创建基于数据列表(未知计数)的动态列表。我怎么能做到这一点?

Manual workflow in Excel 2007:
1.) click on sheet2 (dropdowns worksheet)
2.) add some data from cell A2:A4
3.) click on sheet1 (data worksheet) > highlight cells in some column > Data (menu) > Data Validation > Data Validation > Allow: List, Source: ='sheet2'!$A$2:$A$4 > click 'OK'

1 个答案:

答案 0 :(得分:1)

希望这会有所帮助。有关数据验证的更详细示例,请从codeplex下载EPPlus示例应用程序,并查看示例11。

// add a validation and set values
        var validation = sheet.DataValidations.AddListValidation("A1");
        // Alternatively:
        // var validation = sheet.Cells["A1"].DataValidation.AddListDataValidation();
        validation.ShowErrorMessage = true;
        validation.ErrorStyle = ExcelDataValidationWarningStyle.warning;
        validation.ErrorTitle = "An invalid value was entered";
        validation.Error = "Select a value from the list";
        validation.Formula.ExcelFormula = "B2:B4";