以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  [求助]如何优化excel表中取值代码  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=171373)

--  作者:332595
--  发布时间:2021/8/27 17:46:00
--  [求助]如何优化excel表中取值代码
300个excel文件读入数据特别慢,基本上是1秒1条,CPU占用率高达48%,麻烦大家给点优化建议。


以下内容为程序代码:

1 \'打开excel文件
2 \'循环前50行,30列各单元格是否包含 工作总量 关键字
3 \'如果包含则增加一行,然后循环查找数组中的值与excel文件各单位比对,找到后取出该傎对应的数值
4
5
6 For Each file As String In filesys.GetFiles("E:\\360极速浏览器下载\\工作量")
7 If file.EndsWith(".xls") Then
8 Dim filename = file.SubString(file.Length-14,10)
9 Dim Book As New XLS.Book(file)
10 Dim Sheet As XLS.Sheet = Book.Sheets(0)
11 For i As Integer = 20 To 50 \'行
12 For k As Integer = 1 To 30 \'列
13 Dim jbbg As String = sheet(i,k).Value \'查找 工作总量 关键字
14 If jbbg.Contains("工作总量") Then \'找到关键字后开始循环取出数组中的各项数据
15 Dim dr As Row =Tables("表A").AddNew
16 Dim cs() As String = {"值班日期","工作总量","访问量","责任人","工作平台"}
17 For m As Integer = 0 To cs.Length - 1
18 \'MessageBox.Show("这是从数组中取的第" & m & "次循环值是:" & cs(m))
19 For n As Integer = 20 To 50 \'从行开始循环
20 For o As Integer = 0 To 40 \'从列开始循环
21 Dim gjj As String = sheet(n,o).value \'查找关键字
22 If gjj.StartsWith(cs(m)) Then
23 If m =0 Or m=6 Or m=7 Or m=9 Or m=10 Or m=12 Or m=13 Then
24 dr(cs(m)) = sheet(n,o+1).Text
25 ElseIf m =1 Or m=8 Or m=11 Or m=14 Or m=15 Or m=16 Or m=17 Or m=18 Then
26 dr(cs(m)) = sheet(n,o+2).Text
27 Else
28 dr(cs(m)) = sheet(n,o+3).Text
29 End If
30 Exit For
31 Else \'因为部分表中没有表头部分的日期,无法取出日期则需要将文件名日期引用进来
32 dr(cs(0)) = filename
33 End If
34 Next
35 Next
36 Next
37 Exit For
38 End If
39 Next
40 Next
41 End If
42 Next
43 MessageBox.Show("数据导入成功.","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)

--  作者:332595
--  发布时间:2021/8/27 17:48:00
--  
\'打开excel文件
\'循环前50行,30列各单元格是否包含  工作总量  关键字
\'如果包含则增加一行,然后循环查找数组中的值与excel文件各单位比对,找到后取出该傎对应的数值


For Each file As String In filesys.GetFiles("E:\\360极速浏览器下载\\工作量")
    If file.EndsWith(".xls") Then
        Dim filename = file.SubString(file.Length-14,10)
        Dim Book As New XLS.Book(file)
        Dim Sheet As XLS.Sheet = Book.Sheets(0)
        For i As Integer = 20 To 50 \'行
            For k As Integer = 1 To 30 \'列
                Dim jbbg As String = sheet(i,k).Value \'查找  工作总量  关键字
                If jbbg.Contains("工作总量") Then \'找到关键字后开始循环取出数组中的各项数据                   
                    Dim dr As Row =Tables("表A").AddNew
                    Dim cs() As String = {"值班日期","工作总量","访问量","责任人","工作平台"}
                    For m As Integer = 0 To cs.Length - 1
                        \'MessageBox.Show("这是从数组中取的第" & m & "次循环值是:" & cs(m))
                        For n As Integer = 20 To 50 \'从行开始循环
                            For o As Integer = 0 To 40 \'从列开始循环
                                Dim gjj As String = sheet(n,o).value \'查找关键字
                                If  gjj.StartsWith(cs(m)) Then                                    
                                    If m =0 Or m=6 Or m=7 Or m=9 Or m=10 Or m=12 Or m=13 Then
                                        dr(cs(m)) = sheet(n,o+1).Text                                       
                                    ElseIf m =1 Or m=8 Or m=11 Or m=14 Or m=15 Or m=16 Or m=17 Or m=18 Then
                                        dr(cs(m)) = sheet(n,o+2).Text
                                    Else                                        
                                        dr(cs(m)) = sheet(n,o+3).Text
                                    End If
                                    Exit For
                                Else \'因为部分表中没有表头部分的日期,无法取出日期则需要将文件名日期引用进来
                                    dr(cs(0)) = filename
                                End If
                            Next
                        Next
                    Next
                    Exit For
                End If
            Next
        Next
    End If
Next
MessageBox.Show("数据导入成功.","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)

--  作者:有点蓝
--  发布时间:2021/8/28 9:06:00
--  
上传2个execl文件看看
--  作者:332595
--  发布时间:2021/8/30 10:17:00
--  
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:2021-01-03.xls

 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:2021-01-10.xls
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:2021-07-25.xls



[此贴子已经被作者于2021/8/30 10:17:27编辑过]

--  作者:有点蓝
--  发布时间:2021/8/30 10:42:00
--  
如果是格式不固定,这些数据可能出现在任意单元格,也只能这样了

For Each file As String In filesys.GetFiles("E:\\360极速浏览器下载\\工作量")
    If file.EndsWith(".xls") Then
        Dim filename = file.SubString(file.Length-14,10)
        Dim Book As New XLS.Book(file)
        Dim Sheet As XLS.Sheet = Book.Sheets(0)
Dim bb As Boolean
        For i As Integer = 20 To 50 \'行
            For k As Integer = 1 To 30 \'列
                Dim jbbg As String = sheet(i,k).Value \'查找  工作总量  关键字
                If jbbg.Contains("工作总量") Then \'找到关键字后开始循环取出数组中的各项数据                   
                    Dim dr As Row =Tables("表A").AddNew
                    Dim cs() As String = {"值班日期","工作总量","访问量","责任人","工作平台"}
                    For m As Integer = 0 To cs.Length - 1
                        \'MessageBox.Show("这是从数组中取的第" & m & "次循环值是:" & cs(m))
                        For n As Integer = 20 To 50 \'从行开始循环
                            For o As Integer = 0 To 40 \'从列开始循环
                                Dim gjj As String = sheet(n,o).value \'查找关键字
                                If  gjj.StartsWith(cs(m)) Then                                    
                                    If m =0 Or m=6 Or m=7 Or m=9 Or m=10 Or m=12 Or m=13 Then
                                        dr(cs(m)) = sheet(n,o+1).Text                                       
                                    ElseIf m =1 Or m=8 Or m=11 Or m=14 Or m=15 Or m=16 Or m=17 Or m=18 Then
                                        dr(cs(m)) = sheet(n,o+2).Text
                                    Else                                        
                                        dr(cs(m)) = sheet(n,o+3).Text
                                    End If
                                    Exit For
                                Else \'因为部分表中没有表头部分的日期,无法取出日期则需要将文件名日期引用进来
                                    dr(cs(0)) = filename
                                End If
                            Next
                        Next
                    Next
bb = True
                    Exit For
                End If
            Next
If  bb = True Then Exit For
        Next
    End If
Next
MessageBox.Show("数据导入成功.","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)

--  作者:332595
--  发布时间:2021/8/30 11:16:00
--  
测试之后发现只能导入第一个文件呢。

感觉速度快起来了。

--  作者:有点蓝
--  发布时间:2021/8/30 11:34:00
--  
Dim bb As Boolean
改为
Dim bb As Boolean =  false