以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  导入更新,但有新数据不会自动追加。如何修改代码  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=141716)

--  作者:weibu
--  发布时间:2019/10/9 15:22:00
--  导入更新,但有新数据不会自动追加。如何修改代码
老师好,下面代码,如果商品ID有值,导入的EXCL数据会自动更新狐表软件的数据。但如果导入新数据,如何实现狐表数据新增?
代码更新数据能实现,但有新数据无法新增,请问代码如何修改?商品ID列是表达式列=_indentify

Dim dt As DataTable = DataTables("rs_productinfo")
Dim nus() As String = {"proclass", "probrand", "probar","procode","proname","momunit","sonunit","unitno","sourcetype","prospecs","timesafe","prostate","checkbar","proid"} \'要导出的列名 
Dim caps() As String = {"分类","品牌","箱码","商品编码","商品名称","母码单位","子码单位","溯源因子","溯源类型","规格","保质期","商品状态","瓶码","商品ID"}  \'对应excl的列标题 
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"
    dlg.MultiSelect = True \'允许选择多个文件
    If dlg.ShowDialog =DialogResult.OK Then
        Dim Book As New XLS.Book(dlg.FileName)
        Dim Sheet As XLS.Sheet = Book.Sheets("Sheet1")
        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 = 0 To sheet.Cols.Count - 1
            If sheet(0,c).Text.replace(" ", "") > "" Then
                \'If dt.DataCols.Contains(sheet(0,c).Text.replace(" ", "")) Then
                nms.Add(sheet(0,c).Text.replace(" ", ""), c)
                \'End If
            End If
        Next
        For n As Integer = 1 To Sheet.Rows.Count -1
            Dim sfzhm As String = sheet(n,nms("商品ID")).Text
            If sfzhm = "" Then Continue For
            Dim dr As DataRow = dt.Find("proid = \'" & 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
                Dim idx As Integer = array.Indexof(caps, m)
                If idx >= 0 Then
                    Dim cname As String = nus(idx)
                    If dt.DataCols(cname).IsBoolean Then
                        If Sheet(dic(key),nms(m)).Text = "" OrElse Sheet(dic(key),nms(m)).Text = "False" OrElse Sheet(dic(key),nms(m)).Value = 0 Then
                            key(cname) = False
                        Else
                            key(cname) = True
                        End If
                    Else If dt.DataCols(cname).Expression > "" Then
                        \'表达式列
                    Else If dt.DataCols(cname).IsNumeric Then
                        key(cname) = val(Sheet(dic(key),nms(m)).Value)
                    Else If dt.DataCols(cname).IsDate Then
                        Dim d As Date
                        If Date.TryParse(Sheet(dic(key),nms(m)).Value, d)
                            key(cname) = d
                        End If
                        
                    Else
                        key(cname) = Sheet(dic(key),nms(m)).Value
                    End If
                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
                Dim idx As Integer = array.Indexof(caps, m)
                If idx >= 0 Then
                    Dim cname As String = nus(idx)
                    If dt.DataCols(cname).IsBoolean Then
                        If Sheet(l,nms(m)).Text = "" OrElse Sheet(l,nms(m)).Text = "False" OrElse Sheet(l,nms(m)).Value = 0 Then
                            ndr (cname) = False
                        Else
                            ndr (cname) = True
                        End If
                    Else If dt.DataCols(cname).Expression > "" Then
                        \'表达式列
                    Else If dt.DataCols(cname).IsNumeric Then
                        ndr (m) = val(Sheet(l,nms(m)).Value)
                    Else If dt.DataCols(cname).IsDate Then
                        Dim d As Date
                        If Date.TryParse(Sheet(l,nms(m)).Value, d)
                            ndr (cname) = d
                        End If
                    Else
                        ndr (cname) = Sheet(l,nms(m)).Value
                    End If
                End If
            Next
        Next
        dt.ResumeRedraw()
        MessageBox.Show("导入成功")

        \'systemready = True
    End If
End If

--  作者:狐狸爸爸
--  发布时间:2019/10/9 15:53:00
--  
用Excel的数据更新foxtable现有数据的代码,比简单地将将Excel数据添加到狐表,前者不是更复杂吗?
你能写出以上复杂的代码,做一些变通调整不至于不会啊。

更新和新增数据,帮助都有例子:

http://www.foxtable.com/webhelp/topics/2334.htm


--  作者:weibu
--  发布时间:2019/10/9 22:26:00
--  

图片点击可在新窗口打开查看此主题相关图片如下:微信截图_20191009222149.png
图片点击可在新窗口打开查看
老师我导入的excl里如果有“商品ID”的值是狐表数据没有的,提示如上图错误。
                         如果“商品ID”的值都是狐表数据里面的,则相同“商品ID”更新
                         如果“商品ID”为空,这不执行导入操作。
1楼的代码如何修改,能够达到,“商品ID”的值为空或狐表没有此数据,狐表自动增加行,并导入新的数据。

--  作者:狐狸爸爸
--  发布时间:2019/10/9 22:37:00
--  
帮助:


其中有个例子,如果存在相同编号的订单,则覆盖原来的订单,否则新增一个订单,代码为:

Dim Book As New XLS.Book("c:\\test\\订单.xls")
Dim Sheet As XLS.Sheet = Book.Sheets(0)
Tables("订单").StopRedraw()
Dim nms() As String = {"编号","产品","客户","雇员","单价","折扣","数量","日期"}
For n As Integer = 1 To Sheet.Rows.Count -1
    Dim bh As String = sheet(n,0).Text
    Dim dr As DataRow = DataTables("订单").Find("编号 = \'" & bh & "\'")
    If dr Is Nothing Then \'如果不存在同编号的订单
        dr =  DataTables("订单").AddNew()
    End If
    For m As Integer = 0 To nms.Length - 1
        dr(nms(m)) = Sheet(n,m).Value
    Next
Next
Tables("订单").ResumeRedraw()

如果要改为:如果编号为空,不执行操作,如果存在相同编号的订单,则覆盖原来的订单,否则新增一个订单,代码稍作调整即可:

Dim Book As New XLS.Book("c:\\test\\订单.xls")
Dim Sheet As XLS.Sheet = Book.Sheets(0)
Tables("订单").StopRedraw()
Dim nms() As String = {"编号","产品","客户","雇员","单价","折扣","数量","日期"}
For n As Integer = 1 To Sheet.Rows.Count -1
    Dim bh As String = sheet(n,0).Text
     if bn = "" Then 
       Continue for
     End if
    Dim dr As DataRow = DataTables("订单").Find("编号 = \'" & bh & "\'")
    If dr Is Nothing Then \'如果不存在同编号的订单
        dr =  DataTables("订单").AddNew()
    End If
    For m As Integer = 0 To nms.Length - 1
        dr(nms(m)) = Sheet(n,m).Value
    Next
Next
Tables("订单").ResumeRedraw()

代码并不复杂,自己体会消化。


[此贴子已经被作者于2019/10/9 22:37:41编辑过]