Dim dlg As New OpenFileDialog '定义一个新的OpenFileDialog
dlg.Filter= "Excel文件|*.xls" '设置筛选器
If dlg.ShowDialog = DialogResult.Ok Then '如果用户单击了确定按钮
e.Sender.Text = "导入中……"
Dim Book As New XLS.Book(dlg.FileName)
Dim Sheet As XLS.Sheet = Book.Sheets(0)
'Tables("入库明细表").StopRedraw()
'注意以下数组中列名称的顺序,必须和Excel表中的列顺序一致
Dim nms() As String = {"窑号","窑洞","数量","单位","出窑人","担当","录入时间","验收时间","商品名"}
Application.DoEvents
'注意下面的循环变量从1开始,而不是从0开始,因为Excel表的第一行是标题
Dim arrTemp As object = Sheet.UsedRange.value
dim cmd as new sqlcommand
cmd.commandtext = "Select distinct 窑号,窑洞,数量,出窑人 ,验收时间 ,商品名 from 入库明细表"
dim dt as datatable = cmd.ExecuteReader
For n As Integer = 0 To arrTemp.Length -1
Dim bh As String = "窑号 ='" & arrTemp(n,0).Text & "' and 窑洞 = " & arrTemp(n,1).Text & " and 数量 = " & arrTemp(n,3).Text & " and 出窑人 = '" & arrTemp(n,5).Text & "' and 验收时间 = #" & arrTemp(n,6).Text & "# and 商品名 = '" & arrTemp(n,2).Text & "'"
'如果以下这段还需要很多时间,就先用一个数组保存,然后一起添加行addnew(XX)再循环
If dt.Find(bh) Is Nothing Then '如果不存在同编号的订单
Dim r As Row = Tables("入库明细表").AddNew() ' 最耗时的是这句
For m As Integer = 0 To nms.Length - 1
r(nms(m)) = arrTemp(n,m).Value
Next
End If
Next
e.Sender.Text = "导入"
Tables("入库明细表").ResumeRedraw()
End If
这段代码如何优化,现在是运行代码就要很长时间