以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  代码优化  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=177298)

--  作者:wangglby
--  发布时间:2022/5/16 22:54:00
--  代码优化


           以下代码执行要10多秒,还有能优化的地方吗( 其中表A有900行,SHEET有2万多行)

 

             Dim Book As New XLS.Book(路径1)   
                        Dim Sheet As XLS.Sheet = Book.Sheets(0)
                        For Each dr3 As DataRow In DataTables("表A").Select("编码 is not  null  ")                          
                            For n As Integer = 0 To Sheet.Rows.Count -1
                                If sheet(n,1).VALUE = dr3("编码")   Then
                                    dr3("单价")= sheet(n,2).VALUE
                                    Exit For
                                End If
                            Next
                        Next


--  作者:有点蓝
--  发布时间:2022/5/16 23:17:00
--  
试试

Dim Book As New XLS.Book(路径1) 
Dim Sheet As XLS.Sheet = Book.Sheets(0)
Dim dict As New Dictionary(Of String, Double)
For n As Integer = 0 To Sheet.Rows.Count - 1
    dict.Add(sheet(n, 1).VALUE, sheet(n, 2).VALUE)
Next

For Each dr3 As DataRow In DataTables("表A").Select("编码 is not  null  ") 
    If dict.ContainsKey( dr3("编码")) Then
        dr3("单价") = dict( dr3("编码"))
    End If
Next