以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  [求助]下面这段 代码如何优化?  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=89122)

--  作者:18523982317
--  发布时间:2016/8/16 8:33:00
--  [求助]下面这段 代码如何优化?
Dim dlg As New OpenFileDialog \'定义一个新的OpenFileDialog
dlg.Filter= "excle|*.xlsx" \'设置筛选器
Dim c As Integer = 0
If dlg.ShowDialog = DialogResult.Ok Then \'如果用户单击了确定按钮
    Dim Book As New XLS.Book(dlg.FileName)
    Dim Sheet As XLS.Sheet = Book.Sheets(0)
    Tables("工资总表导入").StopRedraw()
    
    For sr As Integer = 1 To sheet.Rows.Count -1
        Dim r As Row = Tables("工资总表导入").AddNew
        For sc As Integer = 0 To sheet.Cols.Count -1
            For Each dc As DataCol In DataTables("工资总表导入").DataCols
                If dc.Name = sheet(0,sc).Value Then
                    r(dc.name) = sheet(sr,sc).Value
                End If
            Next
        Next
        c = c +1
    Next
    Tables("工资总表导入").ResumeRedraw()
    MessageBox.Show("导入完成,共导入" & c & "条记录")
End If





求这段代码优化,数据导入时间过长

--  作者:Hyphen
--  发布时间:2016/8/16 9:02:00
--  

Dim dlg As New OpenFileDialog \'定义一个新的OpenFileDialog
dlg.Filter= "excle|*.xlsx" \'设置筛选器

If dlg.ShowDialog = DialogResult.Ok Then \'如果用户单击了确定按钮
    Dim Book As New XLS.Book(dlg.FileName)
    Dim Sheet As XLS.Sheet = Book.Sheets(0)
    Dim t As Table = Tables("工资总表导入")
    t.StopRedraw()
    Dim c As Integer = t.Rows.Count
    Dim dict As new Dictionary(of Integer,String)
    For sc As Integer = 0 To sheet.Cols.Count -1
        If t.Cols.Contains(sheet(0,sc).Value) Then
            dict.Add(sc,sheet(0,sc).Value)
        End If
    Next
    
    For sr As Integer = 1 To sheet.Rows.Count -1
        Dim r As Row = t.AddNew
        For Each i As Integer In dict.Keys
            r(dict(i)) = sheet(sr,i).Value
        Next
    Next
    t.ResumeRedraw()
    MessageBox.Show("导入完成,共导入" & t.Rows.Count - c & "条记录")
End If

--  作者:18523982317
--  发布时间:2016/8/16 9:23:00
--  
以下是引用Hyphen在2016/8/16 9:02:00的发言:

Dim dlg As New OpenFileDialog \'定义一个新的OpenFileDialog
dlg.Filter= "excle|*.xlsx" \'设置筛选器

If dlg.ShowDialog = DialogResult.Ok Then \'如果用户单击了确定按钮
    Dim Book As New XLS.Book(dlg.FileName)
    Dim Sheet As XLS.Sheet = Book.Sheets(0)
    Dim t As Table = Tables("工资总表导入")
    t.StopRedraw()
    Dim c As Integer = t.Rows.Count
    Dim dict As new Dictionary(of Integer,String)
    For sc As Integer = 0 To sheet.Cols.Count -1
        If t.Cols.Contains(sheet(0,sc).Value) Then
            dict.Add(sc,sheet(0,sc).Value)
        End If
    Next
    
    For sr As Integer = 1 To sheet.Rows.Count -1
        Dim r As Row = t.AddNew
        For Each i As Integer In dict.Keys
            r(dict(i)) = sheet(sr,i).Value
        Next
    Next
    t.ResumeRedraw()
    MessageBox.Show("导入完成,共导入" & t.Rows.Count - c & "条记录")
End If


Dictionary  居然还有这个代码。。。。
但是这段代码我复制上去了 报错


错误提示: 列“”不属于表 工资导入。  工资总表导入我改成了工资导入,表重新复制了下源表。。


--  作者:大红袍
--  发布时间:2016/8/16 15:10:00
--  
2楼代码应该没问题,上传实例说明。