我需要根据数据所在的字段(列)执行不同的操作。
For Each row As DocumentFormat.OpenXml.Spreadsheet.Row In
worksheet.Descendants(Of DocumentFormat.OpenXml.Spreadsheet.Row)
For Each cell As DocumentFormat.OpenXml.Spreadsheet.Cell In row
Select Case True
Case cell.CellReference.Value Like "C*"
'if this cell is in column C
Case cell.CellReference.Value Like "A*"
'if this cell is in column A
Case Else
End Select
Next
Next
只要给定电子表格中的字段不超过 26 个,此方法就非常有效。
如何确保我的 Like "A*"
不会对 AA、AB 等列使用react?
请记住,OpenXML SDK 始终返回 .cellreference.value 的完整单元格引用,而不仅仅是列。 我需要指定,我并不是试图批量丢弃任何大于 26 的列,但我试图确保指定特定的列。受审查的列最终可能是 AA 或 AB,具体取决于创建特定源表的公司。我希望有一个属性,或者除此之外,其他人如何学会引用 openxml 中的特定列。
最佳答案
在您尝试匹配的列字母后面添加一个#
将告诉匹配器该字母后面必须有一个数字。如果列实际上是 AA
,这将阻止您检查 A
匹配。
Select Case True
Case cell.CellReference.Value Like "C#*"
'if this cell is in column C
Case cell.CellReference.Value Like "A#*"
'if this cell is in column A
Case cell.CellReference.Value Like "AA#*"
'if this cell is in column AA
Case Else
End Select
来自documentation ,#
将匹配“任何单个数字 (0–9)”。
关于.net - 如何指定正确的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40243604/