Dim dlg As New OpenFileDialog '定义一个新的OpenFileDialog
dlg.Filter= "Excel文件|*.xls*" '设置筛选器
Dim tb As DataTable = DataTables("表A")
If dlg.ShowDialog = DialogResult.Ok Then '如果用户单击了确定按钮
tb.LoadFilter = ""
tb.Load
tb.ReplaceFor("状态",False)
Dim zdw As new List(of Integer)
Dim Book As New XLS.Book(dlg.FileName)
Dim Sheet As XLS.Sheet = Book.Sheets(0)
Dim zd As New Dictionary(Of Integer, String)
For n As Integer = 0 To sheet.Cols.Count - 1
If zd.ContainsValue(sheet(0,n).value.trim()) Then
MessageBox.Show("存在重复列" & sheet(0,n).value.trim())
Return
Else
zd.Add(n,sheet(0,n).value.trim())
End If
Next
Dim s As New List(Of Integer)
For Each k As Integer In zd.Keys '显示所有键及其对应的值
If tb.DataCols.Contains(zd(k)) = False Then
s.Add(k)
End If
Next
For Each q As Integer In s
zd.Remove(q)
Next
If zd.Count = 0 Then
MessageBox.Show("不存在与导入表相符的列!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
Return
End If
Dim r As DataRow
For n As Integer = 1 To Sheet.Rows.Count -1
r = tb.Find("编号 = '" & sheet(n,0).value & "'")
If r Is Nothing Then
zdw.Add(n)
Continue For
End If
For Each k As Integer In zd.Keys '显示所有键及其对应的值
If sheet(n,k).value = r(zd(k)) Then
Else
r(zd(k)) = sheet(n,k).value
End If
Next
r("状态") = True
Next
Dim tbq As Integer = tb.DataRows.Count
tb.AddNew(zdw.Count)
For Each n As Integer In zdw
r = tb.DataRows(tbq)
r("状态") = True
For Each k As Integer In zd.Keys '显示所有键及其对应的值
If sheet(n,k).value = r(zd(k)) Then
Else
r(zd(k)) = sheet(n,k).value
End If
Next
tbq += 1
Next
End If
'tb.Save