下面代码是我从论坛搜素,通用判断不重复导入,注意: 准备导入表的字段应该包含在接收表中
Dim dlg As New OpenFileDialog '定义一个新的OpenFileDialog
dlg.Filter= "Excel文件|*.xls|CSV文件|*.csv" '设置筛选器
If dlg.ShowDialog = DialogResult.Ok Then '如果用户单击了确定按钮
Dim Book As New XLS.Book(dlg.FileName)
Dim Sheet As XLS.Sheet = Book.Sheets(0)
For i As Integer = 0 To Sheet.Cols.Count -1
If CurrentTable.Cols.Contains( Sheet(0,i).Value) = False Then
MessageBox.Show("目标文件不含【" & Sheet(0,i).Value & "】列,请修改与目标文件一致!", "导入错误提示")
Return
End If
Next
Dim Filter1 As String
For r As Integer = 1 To Sheet.Rows.Count - 1
Filter1 = ""
For c As Integer = 0 To Sheet.Cols.Count - 1
If c > 0 Then
Filter1 = Filter1 & " And "
End If
'If CurrentTable.DataTable.DataCols(sheet(0,c).Value).Isnumeric Then
'If sheet(r,c).Value = "" Then
' Filter1 = Filter1 & sheet(0,c).Value & " is null"
'Else
'Filter1 = Filter1 & sheet(0,c).Value & " = " & sheet(r,c).Value
'End If
'Else
If sheet(r,c).Value.Trim = "" Then
Filter1 = Filter1 & sheet(0,c).Value & " is null "
Else
Filter1 = Filter1 & sheet(0,c).Value & " = '" & sheet(r,c).Value.Trim() & "'"
End If
' End If
Next
If CurrentTable.DataTable.Find(Filter1) Is Nothing Then
Dim dr As Row = CurrentTable.AddNew()
For c As Integer = 0 To Sheet.Cols.Count - 1
dr(Sheet(0,c).Value) = Sheet(r,c).Value.Trim
Next
End If
Next
End If
[此贴子已经被作者于2012-11-1 21:51:22编辑过]