2

我正在尝试将混合字典键转储到 excel 工作表范围,但它没有按预期执行。这是我的代码。

    Dim ExcelApplication As Excel.Application = New Excel.Application
    Dim ExcelWorkbook As Excel.Workbook = ExcelApplication.Workbooks.Add
    Dim SheetName As String = "Area Information"
    Dim ExcelAreaBalanceWorksheet As Excel.Worksheet = CType(ExcelWorkbook.Sheets(1), Excel.Worksheet)
    Dim BAGenByKVDictionary As New HybridDictionary

   'code to populate hybriddictionary

    Dim Rng As Excel.Range = ExcelAreaBalanceWorksheet.Range("a1", _
                ExcelAreaBalanceWorksheet.Cells(BAGenByKVDictionary.Count, 1))
    Rng.Value = ExcelApplication.WorksheetFunction.Transpose(BAGenByKVDictionary.Keys.ToString)

当我尝试将密钥传递给 Excel 时,它会传递System.Collections.Hashtable+ValueCollection所有单元格而不是传递密钥。

4

1 回答 1

0

这是因为HybridDictionary上的 ToString()不会覆盖定义的基本Object实现,部分定义为:

ToString 方法的默认实现返回 Object 类型的完全限定名...

为了将键作为字符串数组检索,有很多方法,但我总是更喜欢最直接的方法,即循环:

    Dim sbKeys As New System.Text.StringBuilder(1000)

    For Each oKey As Object In BAGenByKVDictionary.Keys
        ' Produce a comma=separated list
        If sbKeys.Length <> 0 Then
            sbKeys.Append(",")
        End If
        sbKeys.Append(oKey.ToString())
    Next

    Rng.Value = ExcelApplication.WorksheetFunction.Transpose(sbKeys.ToString())
于 2013-01-19T01:15:02.997 回答