以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  word报告生成效率问题  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=104917)

--  作者:benwong2013
--  发布时间:2017/8/8 15:09:00
--  word报告生成效率问题
If e.Form.Controls("DateTimePicker3").value Is Nothing Then
    MessageBox.Show("完成日期:" & "不能为空,请检查")
    e.Cancel = True
Else
    If Tables("T_Report").current IsNot Nothing Then
        DataTables("T_Report").ReplaceFor("报告状态",True,"[报告编号] = \'" & Tables("T_Report").current("报告编号") & "\'")
    End If
    Tables("T_Report").Current.save()
    Dim bh As String = Tables("T_Report").Current("报告格式编号")
    Dim tm As String  = ProjectPath & "Attachments\\" & bh & ".doc" \'指定模板文件
    Dim fl As String = ProjectPath & "Reports\\" & Tables("T_Report").current("报告编号") & " " & Tables("T_Report").current("样品名称") & ".Doc" \'指定目标文件
    Dim wrt As New WordReport(Tables("T_Report"),tm,fl) \'定义一个WordReport
    Tables("T_Report.T_Item").Sort = "报告序号"
    For i As Integer = 0 To Tables("T_Report.T_Item").Rows.count - 1
        Dim r As Row = Tables("T_Report.T_Item").Rows(i)
        wrt.ReplaceOne("[报告序号" & i & "]", r("报告序号"))
    Next
    wrt.BuildOne(Tables("T_Report").Current)
    wrt.Quit
    Dim app As New MSWord.Application
    try
        Dim fileName = ProjectPath & "Reports\\" & Tables("T_Report").current("报告编号") & " " & Tables("T_Report").current("样品名称") & ".Doc"
        Dim doc As Object = app.Documents.Open(fileName)
        
        app.ActiveWindow.ActivePane.View.SeekView = MSWord.WdSeekView.wdSeekCurrentPageHeader
        If app.Selection.Find.Execute("CTTREPORT") Then \'查找到指定字符串
            app.Selection.Text =" " & Tables("T_Report").current("报告编号") & "" \'替换字符串
        End If
        app.ActiveWindow.ActivePane.View.SeekView =  MSWord.WdSeekView.wdSeekMainDocument
        
        app.Visible = True
    catch ex As exception
        msgbox(ex.message)
        app.Quit
    finally
        \'app.Quit
    End try
End If

我是通过以上的代码来生成报告模板的,但在点击按钮之后一般都是要等大约20s左右才能生成,请问有没有优化的空间?

--  作者:有点甜
--  发布时间:2017/8/8 15:16:00
--  

加入msgbox,看是哪一步耗时,耗时代码包括 ReplaceFor、save、Sort 等

 

其余的,就不能优化了。