Foxtable(狐表)用户栏目专家坐堂 → 从excel中导入数据自动匹配字段


  共有3272人关注过本帖树形打印复制链接

主题:从excel中导入数据自动匹配字段

帅哥哟,离线,有人找我吗?
有点甜
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2019/6/3 8:49:00 [显示全部帖子]

Dim dlg As New OpenFileDialog '定义一个新的OpenFileDialog
dlg.Filter= "Excel文件|*.xls|Word文件|*.doc|Access文件|*.mdb" '设置筛选器
If dlg.ShowDialog = DialogResult.Ok Then '如果用户单击了确定按钮
    Dim nms() As String = {"登记日期","名称","规格型号","厂家","单位","数量","金额"}
    Dim nms2() As String = {"日期","组件名称","规格","生产企业","单位","数量","结算金额"}
    Dim dict As new Dictionary(of String,String)
    For m As Integer = 0 To nms.Length - 1
        dict.add(nms2(m),nms(m))
    Next
    Dim Book As New XLS.Book(dlg.FileName)
    Dim Sheet As XLS.Sheet = Book.Sheets(0)
    Tables("表A").StopRedraw()
    '注意下面的循环变量从1开始,而不是从0开始,因为Excel表的第一行是标题
    For n As Integer = 1 To Sheet.Rows.Count -1
        Dim r As Row = Tables("表A").AddNew()
        For m1 As Integer = 0 To Sheet.Cols.Count - 1
            If dict.ContainsKey(Sheet(0,m1).Value)
                r(dict(Sheet(0,m1).Value)) = Sheet(n,m1).Value
            End If
        Next
    Next
    Tables("表A").ResumeRedraw()
End If

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2019/6/3 10:17:00 [显示全部帖子]

Dim dlg As New OpenFileDialog '定义一个新的OpenFileDialog
dlg.Filter= "Excel文件|*.xls|Word文件|*.doc|Access文件|*.mdb" '设置筛选器
If dlg.ShowDialog = DialogResult.Ok Then '如果用户单击了确定按钮
    Dim nms() As String = {"登记日期","名称","规格型号","厂家","单位","数量","金额"}
    Dim nms2() As String = {"日期","组件名称","规格","生产企业","单位","数量","结算金额"}
    Dim dict As new Dictionary(of String,String)
    For m As Integer = 0 To nms.Length - 1
        dict.add(nms2(m),nms(m))
    Next
    Dim Book As New XLS.Book(dlg.FileName)
    Dim Sheet As XLS.Sheet = Book.Sheets(0)
    Tables("表A").StopRedraw()
    '注意下面的循环变量从1开始,而不是从0开始,因为Excel表的第一行是标题
    For n As Integer = 1 To Sheet.Rows.Count -1
        If  Sheet(n,0).text > "" Then
            Dim r As Row = Tables("表A").AddNew()
            For m1 As Integer = 0 To Sheet.Cols.Count - 1
                If dict.ContainsKey(Sheet(0,m1).Value)
                    r(dict(Sheet(0,m1).Value)) = Sheet(n,m1).Value
                End If
            Next
        End If
    Next
    Tables("表A").ResumeRedraw()
End If

 回到顶部