【发布时间】:2019-05-07 22:37:35
【问题描述】:
我有一个带有 下拉列表 的 Excel 文档,我正在尝试获取列表的值并 选择 一个(或至少能够选择一个由索引,因为它们不会改变)。
我尝试在 ICell 和 XSSFCell 上使用 SetCellValue 设置值,但它不能正常工作,它只是输入值和我需要选择它,因为 Excel 文档的其他部分会相应更改。
有没有办法使用 NPOI 或任何其他 C# Excel 库来做到这一点?
【问题讨论】:
我有一个带有 下拉列表 的 Excel 文档,我正在尝试获取列表的值并 选择 一个(或至少能够选择一个由索引,因为它们不会改变)。
我尝试在 ICell 和 XSSFCell 上使用 SetCellValue 设置值,但它不能正常工作,它只是输入值和我需要选择它,因为 Excel 文档的其他部分会相应更改。
有没有办法使用 NPOI 或任何其他 C# Excel 库来做到这一点?
【问题讨论】:
如果您使用的是 NPOI。您可以尝试其中一种方法。您还可以使用相同的方法 setCellFormula、SetAsActiveCell、setErrorValue、setCellType 等。
//Approach 1
var row = sheet.CreateRow(0);
row.Cells[targetColumn].SetCellValue("whatertypevalue");
//Approach 2
var namedRow = wb.GetSheetAt(sheetIndex).CreateRow(rowindex);
namedRow.CreateCell(columnIndex).SetCellValue("whatertypevalue");
//Approach 3
var namedRow1 = wb.GetSheetAt(0).GetRow(rowindex);
namedRow1.Cells[targetColumn].SetCellValue("whatertypevalue");
【讨论】:
使用它来浏览下拉列表。如果您使用的是下拉对象。我们正在使用shapes.item
您需要从您的 excel 中识别形状对象名称。
var control = xlWorksheet.Shapes.Item("Drop Down 22").ControlFormat;
control.ListIndex = 5; \\This allows you to change the drop down to 5th element
Excel.Range xlRangeloc= xlWorksheetH.get_Range("D5");
xlRangeloc.Value = "OptionOne";\\If the drop down is a combo box bound to a excel cell value
【讨论】: