Dim dlg As new OpenFileDialog
dlg.Filter = "Excel文件|*.xls"
Dim fl As String
If dlg.ShowDialog = DialogResult.OK Then
fl = dlg.FileName
Vars("rz")=True
Dim dic As Dictionary(Of String ,String)
'Dim cmd As new SQLCommand
'cmd.CommandText = "select * From {khda}"
'Dim dt As DataTable = cmd.ExecuteReader(True)
Dim book As new XLS.Book(fl)
Dim sheet As XLS.Sheet = book.Sheets("sheet1")
If sheet(0,1).Value<>"品名" Then
msgbox("第一列品名不匹配")
Return
End If
If sheet(0,2).Value<>"封装" Then
msgbox("第二列封装不匹配")
Return
End If
DataTables("销售").StopRedraw()
Dim bz As Integer=0
Dim d As Double
Dim nms() As String = {"品名","封装","数量","单价","金额","仓库","区域","电话","备注"}
For n As Integer = 1 To Sheet.Rows.Count -1
If Sheet(n,1).Value=""
Exit For
End If
Dim filter As String
Dim filter1 As String
If Sheet(n,2).Value Is Nothing Then
filter = "品名 ='" & Sheet(n,1).Value.Replace("*", "[*]").Replace("'", "''").Replace("%", "[%]") & "' and 封装 Is null and 单价=" & Sheet(n,4).Value & " And (仓库<>'现货' or 仓库 is null)"
filter1 = "品名 ='" & Sheet(n,1).Value.Replace("*", "[*]").Replace("'", "''").Replace("%", "[%]") & "' and 封装 Is null and (仓库<>'现货' or 仓库 is null)"
Else
filter = "品名 ='" & Sheet(n,1).Value.Replace("*", "[*]").Replace("'", "''").Replace("%", "[%]") & "' and 封装='" & Sheet(n,2).Value &"' And 单价=" & Sheet(n,4).Value & " And (仓库<>'现货' or 仓库 is null)"
filter1 = "品名 ='" & Sheet(n,1).Value.Replace("*", "[*]").Replace("'", "''").Replace("%", "[%]") &"' And 封装='" & Sheet(n,2).Value &"' And (仓库<>'现货' or 仓库 is null)"
End If
'For Each pc As DataRow In DataTables("销售").Select("品名='" & Sheet(n,1).Value.Replace("*", "[*]").Replace("'", "''").Replace("%", "[%]") &"' And 封装='" & Sheet(n,2).Value &"' and (仓库<>'现货' or 仓库 is null)")
Dim pc As DataRow = DataTables("销售").find(filter)
'If (pc("单价")-Sheet(n,4).Value)>0 Or pc Is Nothing
If pc Is Nothing
Dim dr As DataRow = DataTables("销售").AddNew()
For m As Integer = 0 To nms.Length-1
Select Case m
Case 0,1
dr(nms(m)) = Sheet(n,m+1).Value.trim().replace(chr(10), "").replace(chr(13), "")
Case 2,3,4
If Sheet(n,m+1).Value="" Or Sheet(n,m+1).Value.trim="" Then
Else
If Double.TryParse(Sheet(n,m+1).Value, d)
dr(nms(m)) = Sheet(n,m+1).Value
Else
msgbox("行"& n & "列" & m+1 & "显示为" &Sheet(n,m+1).Value & "不是数值,记住后,请更改")
End If
End If
Case Else
dr(nms(m)) = Sheet(n,m+1).Value
End Select
Next
dr.save
Sheet(n,13).Value ="导入成功"
book.Save(fl)
Dim zd As Integer
Dim dj As Long=0
Do While DataTables("销售").Select(filter1).Count>3
For Each cf As DataRow In DataTables("销售").Select(filter1)
If dj<cf("单价")
dj=cf("单价")
zd=cf("_identify")
End If
Next
DataTables("销售").DeleteFor("_identify='" & zd &"'")
Loop
Else
Sheet(n,13).Value ="重复未导入!!"
End If
Next
DataTables("销售").Save
'DataTables("销售").DeleteFor("品名 is null")
'DataTables("销售").Save
Tables("销售").ResumeRedraw()
msgbox("导入完成!!")
Tables("销售").Sort="仓库"
DataTables("销售").LoadFilter=""
DataTables("销售").load
Vars("rz")=False
Else
Return
End If