我有以下代码遍历Excel文件,这很好用
请注意,以下行确实有效(代码中的文件夹正在设置中)
Console.WriteLine("Value found in {1}: {0} ", Obj.Text, folder)
整个
For Each xlWorkSheet In xlWorkBook.Worksheets
For rCnt As Integer = 2 To xlWorkSheet.UsedRange.Rows.Count 'we start at 2 becuase we do not need the first header row
For cCnt As Integer = 1 To xlWorkSheet.UsedRange.Columns.Count
Dim Obj As Range = CType(xlWorkSheet.UsedRange.Cells(rCnt, cCnt), Range)
Console.WriteLine("Value found in {1}: {0} ", Obj.Text, folder)
Next
RaiseEvent ImportChanged()
Next
Next
但我想填写一个包含这些项目的字典,但是当我这样做时,它抱怨它“不是一个对象的实例”,即使我将它转换为Cstr()或使用.toString
任何人都知道为什么?
Dim EntryDetails As Dictionary(Of String, String)
For Each xlWorkSheet In xlWorkBook.Worksheets
For rCnt As Integer = 2 To xlWorkSheet.UsedRange.Rows.Count 'we start at 2 becuase we do not need the first header row
For cCnt As Integer = 1 To xlWorkSheet.UsedRange.Columns.Count
Dim Obj As Range = CType(xlWorkSheet.UsedRange.Cells(rCnt, cCnt), Range)
Console.WriteLine("Value found in {1}: {0} ", Obj.Text, folder)
EntryDetails.Add("demo", Obj.Text) ' THIS DOESNT WORK
EntryDetails.Add("demo", Obj.Text.ToString) ' THIS DOESNT WORK
EntryDetails.Add("demo", CStr(Obj.Text)) ' THIS DOESNT WORK
Next
RaiseEvent ImportChanged()
Next
Next
答案 0 :(得分:3)
您尚未创建EntryDetails
将New
关键字添加到第一行,以便调用构造函数:
Dim EntryDetails As New Dictionary(Of String, String)
For Each xlWorkSheet In xlWorkBook.Worksheets
// ...
Next