以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  从Excel导入不重复的数据  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=40056)

--  作者:东坡一剑
--  发布时间:2013/9/8 0:22:00
--  从Excel导入不重复的数据

想从一个Excel表中提取不重复的数据添加到foxtable的外部表中,代码如下:

Dim dlg As new OpenFileDialog
dlg.Filter = "Excel文件|*.xls"
Dim fl As String

If dlg.ShowDialog = DialogResult.OK Then
    fl = dlg.FileName
End If
MessageBox.Show(1)
Dim dic As Dictionary(Of String ,String)
Dim cmd As new SQLCommand
cmd.C
cmd.CommandText = "select * From {颜色编码}"
Dim dt As DataTable = cmd.ExecuteReader()
MessageBox.Show(2)
Dim book As new XLS.Book(fl)
Dim sheet As XLS.Sheet = book.Sheets("sheet1")
MessageBox.Show(2.1)
For i As Integer = 1 To sheet.Rows.Count - 1
    Dim dr As DataRow = dt.Find("色码 =\'" & sheet(i,1).Value & "\' And 品牌 =\'" & sheet(i,0).Value & "\'")
MessageBox.Show(2.2)
    If dr Is Nothing  AndAlso dic.ContainsKey( sheet(i,1).Value) = False Then
MessageBox.Show(2.3)
    dic.Add(sheet(i,1).Value,sheet(i,0).Value & "," & sheet(i,2).Value)
    End If
Next
MessageBox.Show(3)
For Each key As String  In dic.keys
    Dim dr As DataRow = DataTables("颜色编码").AddNew()
    dr("色码") = key
    dr("品牌") = dic(key).Substring(0, dic(key).Indexof(","))
    dr("颜色") = dic(key).Substring(dic(key).Indexof(",") + 1)
      
Next

 

测试时,MessageBox.Show(2.2)可以正常运行,错误提示为:

Next
此主题相关图片如下:2013-09-08 00 19 08.png
按此在新窗口浏览图片

 

找不到问题所在,请教大家支支招!

 

[此贴子已经被作者于2013-9-8 0:27:05编辑过]

--  作者:lsy
--  发布时间:2013/9/8 7:26:00
--  

例子没有,单从代码中找,恐怕也是找不到问题所在


--  作者:有点酸
--  发布时间:2013/9/8 9:51:00
--  

Dim dic As Dictionary(Of String ,String)

 

改为:

 

Dim dic As New Dictionary(Of String ,String)

 

分析过程:

 

你已经调试分析,说明MessageBox.Show(2.2)之后的一样代码有问题:

 

   If dr Is Nothing  AndAlso dic.ContainsKey( sheet(i,1).Value) = False Then

这行代码AndAlso前有两部分,前面一部分是判断,不可能有问题,后面使用dic,可能有问题,回头检查前面定义dic的这个语句,发现少了个New


--  作者:东坡一剑
--  发布时间:2013/9/8 10:31:00
--  

感谢“有点酸”,正是你说的问题。

佩服你的分析能力!

[此贴子已经被作者于2013-9-8 11:53:52编辑过]