Foxtable(狐表)用户栏目专家坐堂 → [求助]导入筛选的execl文件


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

主题:[求助]导入筛选的execl文件

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


加好友 发短信
等级:五尾狐 帖子:1140 积分:11255 威望:0 精华:0 注册:2017/9/26 16:30:00
[求助]导入筛选的execl文件  发帖心情 Post By:2020/3/17 19:29:00 [只看该作者]

老师好!
Execl文件中的有多个不同的[复核人],如果只导入复核人为当前用户(_UserName) 的行,应该怎样修改下面的代码?
谢谢!

图片点击可在新窗口打开查看此主题相关图片如下:截屏图片.jpg
图片点击可在新窗口打开查看

’导入以[报告号]为标志列的行
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 App As New MSExcel.Application
        try
            Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open(dlg.FileName)
            Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1)
            wb.saved = True
            Dim Rg As MSExcel.Range = Ws.UsedRange
            Dim ary = rg.value
            
            dt.StopRedraw()
            'systemready = False
            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(" ", "")) 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 bgh As String = ary(n,nms("报告号"))
                If bgh = "" Then Continue For
                Dim dr As DataRow = dt.Find("报告号 = '" & bgh & "'")
                If dr IsNot Nothing Then
                    Result = MessageBox.Show("有重复报告号,是否覆盖导入?"& 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 bgh As String = ary(n,nms("报告号"))
                If bgh = "" Then Continue For
                Dim dr As DataRow = dt.Find("报告号 = '" & bgh & "'")
                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
                        'msgbox(m)
                        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.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
    End If
End If


 回到顶部
帅哥,在线噢!
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:107793 积分:548316 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2020/3/17 20:50:00 [只看该作者]

……
Dim flag As Boolean = True
For n As Integer = 2 To Rg.Rows.count
    If ary(n,nms("复核人")) = _UserName
        Dim bgh As String = ary(n,nms("报告号"))
        If bgh = "" Then Continue For
        Dim dr As DataRow = dt.Find("报告号 = '" & bgh & "'")
        If dr IsNot Nothing Then
            Result = MessageBox.Show("有重复报告号,是否覆盖导入?"& vbcrlf &"是---覆盖导入(将更新原有报告号数据)"& vbcrlf &"否---新增导入(仅增加新报告号数据)", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If Result = DialogResult.Yes Then
                flag = True
            Else
                flag = False
            End If
            Exit For
        End If
    End If
Next
For n As Integer = 2 To Rg.Rows.count
    If ary(n,nms("复核人")) = _UserName
        Dim bgh As String = ary(n,nms("报告号"))
        If bgh = "" Then Continue For
        Dim dr As DataRow = dt.Find("报告号 = '" & bgh & "'")
        If dr Is Nothing Then
            dic.Add(n,Nothing)
        ElseIf flag = True
            dic.Add(n,dr)
        End If
    End If
Next
……

 回到顶部
帅哥哟,离线,有人找我吗?
天一生水
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1140 积分:11255 威望:0 精华:0 注册:2017/9/26 16:30:00
  发帖心情 Post By:2020/3/17 21:01:00 [只看该作者]

谢谢蓝老师!

 回到顶部
帅哥哟,离线,有人找我吗?
天一生水
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1140 积分:11255 威望:0 精华:0 注册:2017/9/26 16:30:00
  发帖心情 Post By:2020/12/14 8:42:00 [只看该作者]

蓝老师好!
一楼的代码,假如有一列[报送单位]我不希望导入,怎样排除它?

 回到顶部
帅哥,在线噢!
有点蓝
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:107793 积分:548316 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2020/12/14 8:56:00 [只看该作者]

那个代码是处理列名的,然后判断如果是这个列名就不添加到字典里

 回到顶部
帅哥哟,离线,有人找我吗?
天一生水
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1140 积分:11255 威望:0 精华:0 注册:2017/9/26 16:30:00
  发帖心情 Post By:2020/12/14 11:49:00 [只看该作者]

谢谢

 回到顶部