我正在尝试根据用户在 ComboBox 元素上所做的选择来选择 Excel 工作表,但这是我唯一的解决方案,而且很丑:
using (var package = new ExcelPackage(existingFile))
{
ExcelWorkbook workbook = package.Workbook;
if (workbook != null)
{
if (workbook.Worksheets.Count > 0)
{
ExcelWorksheet currentWorkSheet;
if (blYear.Text == "2010")
{
currentWorkSheet = workbook.Worksheets.First();
}
else if (blYear.Text == "2011")
{
currentWorkSheet = workbook.Worksheets[2];
}
else if (blYear.Text == "2012")
{
currentWorkSheet = workbook.Worksheets[3];
}
else if (blYear.Text == "2013")
{
currentWorkSheet = workbook.Worksheets[4];
}
else
{
currentWorkSheet = workbook.Worksheets.First();
}
}
}
}
可以从 ComboBox 中获取选定的 Item 索引并改进我的代码吗?否则,每次 Excel 获得新工作表时,我都需要触摸代码,而我不想要这个。有什么帮助吗?建议?解决方案?