谢谢田老师!
我想在导入前先判断有没有重复案号,如果有,就弹出提示对话框,选择覆盖或新增;如果没有,直接新增导入。需要怎样修改代码?
Dim dt As DataTable = DataTables("数据表")
Dim Result As DialogResult
Result = MessageBox.Show("请选择:"& vbcrlf &"是---覆盖导入"& vbcrlf &"否---新增导入", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Result = DialogResult.Yes Then
’--------------以下为覆盖导入
Dim dlg As New OpenFileDialog
dlg.Filter = "Excel文件|*.xls"
dlg.MultiSelect = True '允许选择多个文件
If dlg.ShowDialog =DialogResult.OK Then
Dim App As New MSExcel.Application
try
Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open(dlg.FileName)
Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1)
wb.saved = True
Dim Rg As MSExcel.Range = Ws.UsedRange
Dim ary = rg.value
dt.ResumeRedraw()
dt.StopRedraw()
'systemready = False
Dim nms As New Dictionary(Of String, Integer)
Dim dic As new Dictionary(Of DataRow, Integer)
Dim ls As new List(Of Integer)
For c As Integer = 1 To Rg.Columns.count
If dt.DataCols.Contains(ary(1,c).replace(" ", "")) Then
nms.Add(ary(1,c).replace(" ", ""), c)
End If
Next
For n As Integer = 2 To Rg.Rows.count
Dim sfzhm As String = ary(n,nms("案号"))
If sfzhm = "" Then Continue For
Dim dr As DataRow = dt.Find("案号 = '" & sfzhm & "'")
If dr Is Nothing Then
ls.add(n)
Else
dic.Add(dr, n)
End If
Next
For Each key As DataRow In dic.Keys
For Each m As String In nms.keys
If dt.DataCols(m).IsBoolean Then
If ary(dic(key),nms(m)) = "" OrElse ary(dic(key),nms(m))= "False" OrElse ary(dic(key),nms(m)) = 0 Then
key(m) = False
Else
key(m) = True
End If
Else If dt.DataCols(m).Expression > "" Then
'表达式列
Else If dt.DataCols(m).IsNumeric Then
key(m) = val(ary(dic(key),nms(m)))
Else If dt.DataCols(m).IsDate Then
Dim d As Date
If Date.TryParse(ary(dic(key),nms(m)), d)
key(m) = d
End If
Else
key(m) = ary(dic(key),nms(m))
End If
Next
Next
For Each l As Integer In ls
Dim ndr As DataRow = dt.AddNew
For Each m As String In nms.keys
If dt.DataCols(m).IsBoolean Then
If ary(l,nms(m))= "" OrElse ary(l,nms(m))= "False" OrElse ary(l,nms(m))= 0 Then
ndr (m) = False
Else
ndr (m) = True
End If
Else If dt.DataCols(m).Expression > "" Then
'表达式列
Else If dt.DataCols(m).IsNumeric Then
ndr (m) = val(ary(l,nms(m)))
Else If dt.DataCols(m).IsDate Then
Dim d As Date
If Date.TryParse(ary(l,nms(m)), d)
ndr (m) = d
End If
Else
ndr (m) = ary(l,nms(m))
End If
Next
Next
dt.ResumeRedraw()
msgbox("新增" & ls.count & " " & "更新旧数据" & dic.count)
'msgbox("新增了" & ls.count)
'msgbox("覆盖了" & dic.count)
app.quit
'systemready = True
catch ex As exception
msgbox(ex.message)
app.quit
End try
End If
End If
’------------------------------------以下为新增导入
Else
Dim dlg As New OpenFileDialog
dlg.Filter = "Excel文件|*.xls"
dlg.MultiSelect = True '允许选择多个文件
If dlg.ShowDialog =DialogResult.OK Then
Dim App As New MSExcel.Application
try
Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open(dlg.FileName)
Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1)
wb.saved = True
Dim Rg As MSExcel.Range = Ws.UsedRange
Dim ary = rg.value
dt.ResumeRedraw()
dt.StopRedraw()
'systemready = False
Dim nms As New Dictionary(Of String, Integer)
Dim dic As new Dictionary(Of DataRow, Integer)
Dim ls As new List(Of Integer)
For c As Integer = 1 To Rg.Columns.count
If dt.DataCols.Contains(ary(1,c).replace(" ", "")) Then
nms.Add(ary(1,c).replace(" ", ""), c)
End If
Next
For n As Integer = 2 To Rg.Rows.count
ls.add(n)
Next
For Each l As Integer In ls
Dim ndr As DataRow = dt.AddNew
For Each m As String In nms.keys
If dt.DataCols(m).IsBoolean Then
If ary(l,nms(m))= "" OrElse ary(l,nms(m))= "False" OrElse ary(l,nms(m))= 0 Then
ndr (m) = False
Else
ndr (m) = True
End If
Else If dt.DataCols(m).Expression > "" Then
'表达式列
Else If dt.DataCols(m).IsNumeric Then
ndr (m) = val(ary(l,nms(m)))
Else If dt.DataCols(m).IsDate Then
Dim d As Date
If Date.TryParse(ary(l,nms(m)), d)
ndr (m) = d
End If
Else
ndr (m) = ary(l,nms(m))
End If
Next
Next
dt.ResumeRedraw()
msgbox("新增" & ls.count)
app.quit
'systemready = True
catch ex As exception
msgbox(ex.message)
app.quit
End try
End If
End If
[此贴子已经被作者于2018/1/16 16:21:48编辑过]