2016-10-25 33 views
2

我需要做基於其字段(列)不同的事情的數據是。如何指定正確的列?

  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,等等?

記住的OpenXML的SDK始終返回.cellreference.value一個完整的單元格引用,而不是僅僅列。 而且我需要指定我不想批量拋出大於26的任何列,但我試圖確保指定了特定的列。被審查的專欄有可能最終成爲AA或AB,具體取決於創建具體來源表的公司。我希望有一個屬性,或者除了那個,其他人已經學會了如何引用openxml中的特定列。

+0

你可以從單元格中獲得列嗎? – ChrisF

+0

@ChrisF謝謝。據我所知,獲得列的唯一方法是使用'.cellreference.value',它返回一個字符串。如果存在另一種獲取引用的方式(可能採用不同的格式),它似乎沒有記錄在MSDN中。 – CWilson

+0

我沒有想到數據,只是獲取參考的列部分。 – ChrisF

回答

2

添加列字母(S)你想匹配會告訴匹配,必須有字母后的數字後#。如果該列實際爲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)」。

相關問題