以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  [求助] 用这个代码导入excel会打开两次,怎么修改呢  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=119477)

--  作者:hrlong
--  发布时间:2018/5/25 15:18:00
--  [求助] 用这个代码导入excel会打开两次,怎么修改呢
以下代码,分别把一个excel表格导入到2个表
我想要实现的是打开要导入的文件后自动的导入后台两个表
但是 代码执行时,会显示两次打开excel文件的对话框
第一次选择excel时,要找文件在哪里
第二次跳出时,已经选好了,直接点打开就可以了
请问怎么把第二次避免掉呢
=======================================================
Dim dlg As New OpenFileDialog \'定义一个新的OpenFileDialog
dlg.Filter = "Excel文件|*.xls;*.xlsx"
If dlg.ShowDialog = DialogResult.Ok Then \'如果用户单击了确定按钮
    Dim mg As New Merger
    mg.SourcePath = dlg.FileName
    mg.Format = "excel" \'指定格式
    mg.SourceTableName = "sheet0$"
    mg.DataTableName = "订单临时表"
mg.FieldMaps.Add("收件人电话/手机","收件人电话")
mg.FieldMaps.Add("收件人姓名*","收件人姓名")
    mg.Merge()
End If
DataTables("订单临时表").DataCols("系统单号").RaiseDataColChanged()
Dim dt As DataTable = DataTables("订单临时表汇总")
    If dlg.ShowDialog =DialogResult.OK Then
        Dim Book As New XLS.Book(dlg.FileName)
        Dim Sheet As XLS.Sheet = Book.Sheets("sheet0")
        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  dt.DataCols.Contains(sheet(0,c).Text.replace(" ", "")) Then
                nms.Add(sheet(0,c).Text.replace(" ", ""), c)
            End If
        Next
        Dim ls_sfzhm As new List(Of String)
        For n As Integer = 1 To Sheet.Rows.Count -1
            Dim sfzhm As String = sheet(n,nms("系统单号")).Text
            If sfzhm = "" Then Continue For
            Dim dr As DataRow = dt.Find("系统单号 = \'" & sfzhm & "\'")
            If dr Is Nothing Then
                If ls_sfzhm.Contains(sfzhm) = False Then
                    ls.add(n)
                    ls_sfzhm.Add(sfzhm)
                End If
            Else
                dic.Add(dr, n)
            End If
        Next
        For Each key As DataRow In dic.Keys
            For Each m As String In nms.keys
                If dt.DataCols(m).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(m) = False
                    Else
                        key(m) = True
                    End If
                Else If dt.DataCols(m).Expression > "" Then
                    \'表达式列
                Else If dt.DataCols(m).IsNumeric Then
                    key(m) = val(Sheet(dic(key),nms(m)).Value)
                Else If dt.DataCols(m).IsDate Then
                    Dim d As Date
                    If Date.TryParse(Sheet(dic(key),nms(m)).Value, d)
                        key(m) = d
                    End If
                Else
                    key(m) = Sheet(dic(key),nms(m)).Value
                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
                If dt.DataCols(m).IsBoolean Then
                    If Sheet(l,nms(m)).Text = "" OrElse Sheet(l,nms(m)).Text = "False" OrElse Sheet(l,nms(m)).Value = 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(Sheet(l,nms(m)).Value)
                Else If dt.DataCols(m).IsDate Then
                    Dim d As Date
                    If Date.TryParse(Sheet(l,nms(m)).Value, d)
                        ndr (m) = d
                    End If
                Else
                    ndr (m) = Sheet(l,nms(m)).Value
                End If
            Next
        Next
        dt.ResumeRedraw()
        \'systemready = True
    End If

Syscmd.Project.Save()
[此贴子已经被作者于2018/5/25 15:18:44编辑过]

--  作者:有点甜
--  发布时间:2018/5/25 15:48:00
--  

Dim dlg As New OpenFileDialog \'定义一个新的OpenFileDialog
dlg.Filter = "Excel文件|*.xls;*.xlsx"
If dlg.ShowDialog = DialogResult.Ok Then \'如果用户单击了确定按钮
    Dim mg As New Merger
    mg.SourcePath = dlg.FileName
    mg.Format = "excel" \'指定格式
    mg.SourceTableName = "sheet0$"
    mg.DataTableName = "订单临时表"
    mg.FieldMaps.Add("收件人电话/手机","收件人电话")
    mg.FieldMaps.Add("收件人姓名*","收件人姓名")
    mg.Merge()
    DataTables("订单临时表").DataCols("系统单号").RaiseDataColChanged()
    Dim dt As DataTable = DataTables("订单临时表汇总")
    Dim Book As New XLS.Book(dlg.FileName)
    Dim Sheet As XLS.Sheet = Book.Sheets("sheet0")
    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  dt.DataCols.Contains(sheet(0,c).Text.replace(" ", "")) Then
            nms.Add(sheet(0,c).Text.replace(" ", ""), c)
        End If
    Next
    Dim ls_sfzhm As new List(Of String)
    For n As Integer = 1 To Sheet.Rows.Count -1
        Dim sfzhm As String = sheet(n,nms("系统单号")).Text
        If sfzhm = "" Then Continue For
        Dim dr As DataRow = dt.Find("系统单号 = \'" & sfzhm & "\'")
        If dr Is Nothing Then
            If ls_sfzhm.Contains(sfzhm) = False Then
                ls.add(n)
                ls_sfzhm.Add(sfzhm)
            End If
        Else
            dic.Add(dr, n)
        End If
    Next
    For Each key As DataRow In dic.Keys
        For Each m As String In nms.keys
            If dt.DataCols(m).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(m) = False
                Else
                    key(m) = True
                End If
            Else If dt.DataCols(m).Expression > "" Then
                \'表达式列
            Else If dt.DataCols(m).IsNumeric Then
                key(m) = val(Sheet(dic(key),nms(m)).Value)
            Else If dt.DataCols(m).IsDate Then
                Dim d As Date
                If Date.TryParse(Sheet(dic(key),nms(m)).Value, d)
                    key(m) = d
                End If
            Else
                key(m) = Sheet(dic(key),nms(m)).Value
            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
            If dt.DataCols(m).IsBoolean Then
                If Sheet(l,nms(m)).Text = "" OrElse Sheet(l,nms(m)).Text = "False" OrElse Sheet(l,nms(m)).Value = 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(Sheet(l,nms(m)).Value)
            Else If dt.DataCols(m).IsDate Then
                Dim d As Date
                If Date.TryParse(Sheet(l,nms(m)).Value, d)
                    ndr (m) = d
                End If
            Else
                ndr (m) = Sheet(l,nms(m)).Value
            End If
        Next
    Next
    dt.ResumeRedraw()
    \'systemready = True
End If


Syscmd.Project.Save()