要实现自行选定文件上传(每天收到Excel表格上传),列名不同用手工编码填充,并且以"报告编号"为关键词,重复的跳过,不重复的新增表结构
Dim Cols1() As String = {"报告编号","客户姓名","联系电话",“联系地址”}
Dim Cols2() As String = {"报告编号","姓名","电话",“地址”}
-----------------------------------------------------------------
以下是查帮助的方法,请教如何写,不胜感谢
如果希望出现一个打开文件对话框,导入用户指定的文件,可以参考下面的代码:
Dim dlg As New
OpenFileDialog
'定义一个新的OpenFileDialog
dlg.Filter= "Excel文件|*.xls"
'设置筛选器
If dlg.ShowDialog = DialogResult.Ok Then '如果用户单击了确定按钮
Dim ip As New
Importer
ip.SourcePath = dlg.FileName
'指定数据文件
ip.SourceTableName = "订单"
'指定要导入的表
ip.NewTableName ="订单"
'导入后的表名
ip.Import()
End If
合并数据
如果订单表中已经存在相同编号的订单,那么就跳过此订单,可以将代码改为:
Dim Book As New XLS.Book("c:\test\订单.xls")
Dim Sheet As XLS.Sheet = Book.Sheets(0)
Tables("订单").StopRedraw()
'注意以下数组中列名称的顺序,必须和Excel表中的列顺序一致
Dim nms() As String = {"编号","产品","客户","雇员","单价","折扣","数量","日期"}
'注意下面的循环变量从1开始,而不是从0开始,因为Excel表的第一行是标题
For n As Integer = 1 To Sheet.Rows.Count -1
Dim bh As String = sheet(n,0).Text
If DataTables("订单").Find("编号 = '" & bh & "'") Is Nothing Then '如果不存在同编号的订单
Dim r As Row = Tables("订单").AddNew()
For m As Integer = 0 To nms.Length - 1
r(nms(m)) = Sheet(n,m).Value
Next
End If
Next
Tables("订单").ResumeRedraw(
如果表A和表B的结构不同,或者只需填充部分列,就需要定义数据来源列和数据接收列,例如:
Dim Cols1() As String = {"来源列一","来源列二","来源列三"}
Dim Cols2() As String = {"接收列一","接收列二","接收列三"}
For Each dr1 As DataRow In DataTables("表A").Select("[金额] > 100")
Dim dr2 As DataRow = DataTables("表B").AddNew()
For i As Integer = 0 To Cols1.Length -1
dr2(Cols2(i)) = dr1(Cols1(i))
Next
Next
Dim dlg As New OpenFileDialog '定义一个新的OpenFileDialog
dlg.Filter = "Excel文件|*.xls" '设置筛选器
If dlg.ShowDialog = DialogResult.Ok Then '如果用户单击了确定按钮
Dim Cols1() As String = {"报告编号", "客户姓名", "联系电话", “联系地址”}
Dim Cols2() As String = {"报告编号", "姓名", "电话",“地址”}
Dim Book As New XLS.Book(dlg.FileName)
Dim Sheet As XLS.Sheet = Book.Sheets(0)
Tables("订单").StopRedraw()
'注意下面的循环变量从1开始,而不是从0开始,因为Excel表的第一行是标题
For n As Integer = 1 To Sheet.Rows.Count - 1
Dim bh As String = sheet(n, 0).Text
If DataTables("订单").Find("报告编号 = '" & bh & "'") Is Nothing Then '如果不存在同编号的订单
Dim r As Row = Tables("订单").AddNew()
For m As Integer = 0 To Cols1.Length - 1
r(Cols2(m)) = Sheet(n, m).Value
Next
End If
Next
Tables("订单").ResumeRedraw()
End If
蓝老师,可以上传了,能跳列吧,上传表有很多是无用的列,能不能指定列上传
改为指定列的位置,比如execl里
"报告编号"在第一列, "客户姓名"在第3列, "联系电话"在第5列, “联系地址”在第6列
If dlg.ShowDialog = DialogResult.Ok Then '如果用户单击了确定按钮
Dim Cols1() As integer = {0, 2, 4, 5} 'execl列索引从0开始
Dim Cols2() As String = {"报告编号", "姓名", "电话",“地址”} 'Foxtable表格列名
Dim Book As New XLS.Book(dlg.FileName)
Dim Sheet As XLS.Sheet = Book.Sheets(0)
Tables("订单").StopRedraw()
'注意下面的循环变量从1开始,而不是从0开始,因为Excel表的第一行是标题
For n As Integer = 1 To Sheet.Rows.Count - 1
Dim bh As String = sheet(n, 0).Text
If DataTables("订单").Find("报告编号 = '" & bh & "'") Is Nothing Then '如果不存在同编号的订单
Dim r As Row = Tables("订单").AddNew()
For m As Integer = 0 To Cols1.Length - 1
r(Cols2(m)) = Sheet(n, Cols1(m)).Value
Next
End If
Next
Tables("订单").ResumeRedraw()
End If