以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- IF的执行效率问题 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=89654) |
|
-- 作者:wh420 -- 发布时间:2016/8/26 11:12:00 -- IF的执行效率问题 代码如下:标红部分的代码执行效率非常低,麻烦老师给看一下如何优化? For k As Integer = 1 To wb.workSheets.Count \'//多个sheet的处理 Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(k) Dim Rg As MSExcel.Range = Ws.UsedRange Dim ary = rg.value If ary IsNot Nothing Then \'2016-8-22 判断EXCEL不能为空 lbl8.text = "行:" & rg.Rows.Count & "," & "列:" & rg.Columns.Count For i As Integer = 1 To rg.Rows.Count For j As Integer = 1 To rg.Columns.Count If ary(i,j) <> Nothing Then If Cbox1.Checked = True AndAlso Cbox2.Checked=True AndAlso System.Text.RegularExpressions.Regex.Match((ary(i,j)),"[\\u4e00-\\u9fa5]+").Tostring()>"" =True Then Dim dr As DataRow = DataTables("翻译内容").AddNew dr("第一列") = ary(i,j) lbl5.text = ary(i,j) ElseIf CBox1.Checked = True AndAlso System.Text.RegularExpressions.Regex.Match((ary(i,j)),"[\\u4e00-\\u9fa5]+").Tostring()>"" =True AndAlso Cbox2.Checked=False AndAlso rg.Columns(j).width <> 0 AndAlso rg.Rows(i).height <> 0 Then Dim dr As DataRow = DataTables("表A").AddNew dr("第一列") = ary(i,j) lbl5.text = ary(i,j) ElseIf CBox2.Checked = True AndAlso Cbox1.Checked=False Then Dim dr As DataRow = DataTables("表A").AddNew dr("第一列") = ary(i,j) lbl5.text = ary(i,j) ElseIf cbox1.checked=False AndAlso cbox2.checked=False AndAlso rg.Columns(j).width <> 0 AndAlso rg.Rows(i).height <> 0 Then Dim dr As DataRow = DataTables("表A").AddNew dr("第一列") = ary(i,j) lbl5.text = ary(i,j) End If End If Next Next End If Next Wb.Close() |
|
-- 作者:Hyphen -- 发布时间:2016/8/26 11:50:00 -- 只能这样 For k As Integer = 1 To wb.workSheets.Count \'//多个sheet的处理 Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(k) Dim Rg As MSExcel.Range = Ws.UsedRange Dim ary = rg.value If ary IsNot Nothing Then \'2016-8-22 判断EXCEL不能为空 lbl8.text = "行:" & rg.Rows.Count & "," & "列:" & rg.Columns.Count For i As Integer = 1 To rg.Rows.Count For j As Integer = 1 To rg.Columns.Count If ary(i,j) <> Nothing Then If Cbox1.Checked Then If System.Text.RegularExpressions.Regex.Match((ary(i,j)),"[\\u4e00-\\u9fa5]+").Tostring()>"" Then If Cbox2.Checked Dim dr As DataRow = DataTables("翻译内容").AddNew dr("第一列") = ary(i,j) ElseIf rg.Columns(j).width <> 0 AndAlso rg.Rows(i).height <> 0 Then Dim dr As DataRow = DataTables("表A").AddNew dr("第一列") = ary(i,j) End If End If Else If CBox2.Checked Then Dim dr As DataRow = DataTables("表A").AddNew dr("第一列") = ary(i,j) ElseIf rg.Columns(j).width <> 0 AndAlso rg.Rows(i).height <> 0 Then Dim dr As DataRow = DataTables("表A").AddNew dr("第一列") = ary(i,j) End If End If lbl5.text = ary(i,j) End If Next Next End If Next Wb.Close() |
|
-- 作者:wh420 -- 发布时间:2016/8/26 11:50:00 -- 老师们,帮忙看看呗。 |
|
-- 作者:wh420 -- 发布时间:2016/8/26 12:10:00 -- 老师,按您的方法改了一下效率还是一样,没有提升。还有其他办法吗? |
|
-- 作者:jspta -- 发布时间:2016/8/26 12:38:00 -- lbl5.text = ary(i,j) 这句话影响了把,反复设置这个干吗?去掉试试
|
|
-- 作者:wh420 -- 发布时间:2016/8/26 12:56:00 -- 代码的最终目的是要把EXCEL各单元格里的数据抽取到狐表中,
|
|
-- 作者:jspta -- 发布时间:2016/8/26 13:39:00 -- 这两句判断?简单去掉是不是会变快 rg.Columns(j).width <> 0 AndAlso rg.Rows(i).height另外如果行数非常多 Dim dr As DataRow = DataTables("表A").AddNew 这句话也严重影响速度, 需要先Tables(表A).stopRedraw ,完成后在.ResumeRedraw
|
|
-- 作者:大红袍 -- 发布时间:2016/8/26 13:58:00 -- 看7楼吧,一般是这句代码影响的,先去掉。有必要每次都判断吗?
rg.Columns(j).width <> 0 AndAlso rg.Rows(i).height <> 0
再有就是,你有赋值的操作,顺便看看datacolchanged事件的代码,可能有耗时代码 |