e.Form.Controls("Label1").Text = "正在导入EXCEL表数据到【网上采集专业数据】表中,请稍后..."
Application.DoEvents()
Dim timestart,timeend As Date
timestart=Date.now
Dim dt As DataTable = DataTables("网上采集专业数据")
Dim Result As DialogResult
Result = MessageBox.Show("确定要导入吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Result = DialogResult.Yes Then
Dim dlg As New OpenFileDialog
dlg.Filter = "Excel文件|*.xls;*.xlsx"
dlg.MultiSelect = True '允许选择多个文件
If dlg.ShowDialog =DialogResult.OK Then
Dim stt As Date = Date.Now '开始计时
systemready = False
For Each fl As String In dlg.FileNames
Dim App As New MSExcel.Application
try
Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open(fl)
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()
Dim nms As New Dictionary(Of String, Integer)
Dim dic As new Dictionary(Of Integer,DataRow)
Dim ls As new List(Of Integer)
For c As Integer = 1 To Rg.Columns.count
If dt.DataCols.Contains(ary(1,c).replace(" ", "")) AndAlso ary(1,c) <> "序号" Then '法院列不导入
nms.Add(ary(1,c).replace(" ", ""), c)
End If
Next
Dim flag As Boolean = True
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.sqlFind("院校名称 = '" & sfzhm & "'")
If dr IsNot Nothing Then
Result = MessageBox.Show(FileSys.GetName(fl).split(".")(0) & vbcrlf &"有重复院校名称,是否覆盖导入?" & vbcrlf & "是---覆盖导入(将更新原有院校名称数据)"& vbcrlf &"否---新增导入(仅增加新院校名称数据)", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Result = DialogResult.Yes Then
flag = True
Else
flag = False
End If
Exit For
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.sqlFind("院校名称 = '" & sfzhm & "'")
If dr Is Nothing Then
dic.Add(n,Nothing)
ElseIf flag = True
dic.Add(n,dr)
End If
Next
For Each l As Integer In dic.Keys
Dim ndr As DataRow
If dic(l) Is Nothing Then
ndr = dt.AddNew
Else
ndr = dic(l)
End If
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.save
dt.ResumeRedraw()
Dim n1,n2 As Integer
For Each l As Integer In dic.Keys
Dim ndr As DataRow
If dic(l) Is Nothing Then
'ndr = dt.AddNew
n1+= 1
Else
ndr = dic(l)
n2+= 1
End If
Next
msgbox("新增" & n1 & " " & "更新旧数据" & n2)
app.quit
systemready = True
catch ex As exception
msgbox(ex.message)
app.quit
End try
Next
End If
End If
'DataTables("网上采集专业数据").Save()
e.Form.Controls("Label1").Text = "EXCEL表数据导入完成!"
timeend=Date.now
e.Form.Controls("Label1").text="导入EXCEL表数据到【网上采集专业数据】表完成!耗时" & (timeend-timestart).TotalSeconds & "秒"
Messagebox.show("从EXCEL表中导入到【网上采集专业数据】表中共导入"&(Tables("网上采集专业数据").rows.count) & "行","提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
MainTable = Tables("网上采集专业数据")