vb.net2005 利用创建好的WORD模板制作WORD报表

来源:互联网 发布:淘宝信用卡怎么分期 编辑:程序博客网 时间:2024/09/21 09:25

       Dim WordAPP As New Word.Application
        Dim WordDocument As Word.Document
        WordDocument = WordAPP.Documents.Open(Application.StartupPath & "/report.doc")
        WordDocument.Activate()

     '进行查找替换

        With WordAPP.Selection.Find
            .ClearFormatting()
            .Replacement.ClearFormatting()
            .Text = "C1"
            .Replacement.Text = "小样门诊室"
            .MatchCase = True
            .Execute(, , , , , , , , , , Word.WdReplace.wdReplaceAll)
        End With

        With WordAPP.Selection.Find
            .ClearFormatting()
            .Replacement.ClearFormatting()
            .Text = "C2"
            .Replacement.Text = "小样路"
            .MatchCase = True
            .Execute(, , , , , , , , , , Word.WdReplace.wdReplaceAll)
        End With

        With WordAPP.Selection.Find
            .ClearFormatting()
            .Replacement.ClearFormatting()
            .Text = "C3"
            .Replacement.Text = "小样"
            .MatchCase = True
            .Execute(, , , , , , , , , , Word.WdReplace.wdReplaceAll)
        End With

        With WordAPP.Selection.Find
            .ClearFormatting()
            .Replacement.ClearFormatting()
            .Text = "C4"
            .Replacement.Text = "1100101010"
            .MatchCase = True
            .Execute(, , , , , , , , , , Word.WdReplace.wdReplaceAll)
        End With

        With WordAPP.Selection.Find
            .ClearFormatting()
            .Replacement.ClearFormatting()
            .Text = "C5"
            .Replacement.Text = "药品项目^l检查项目"
            .MatchCase = True
            .Execute(, , , , , , , , , , Word.WdReplace.wdReplaceAll)
        End With
        With WordAPP.Selection.Find
            .ClearFormatting()
            .Replacement.ClearFormatting()
            .Text = "C6"
            .Replacement.Text = "小草^l手术费"
            .MatchCase = True
            .Execute(, , , , , , , , , , Word.WdReplace.wdReplaceAll)
        End With
        With WordAPP.Selection.Find
            .ClearFormatting()
            .Replacement.ClearFormatting()
            .Text = "C7"
            .Replacement.Text = "10^l5"
            .MatchCase = True
            .Execute(, , , , , , , , , , Word.WdReplace.wdReplaceAll)
        End With
        With WordAPP.Selection.Find
            .ClearFormatting()
            .Replacement.ClearFormatting()
            .Text = "C8"
            .Replacement.Text = "2^l1"
            .MatchCase = True
            .Execute(, , , , , , , , , , Word.WdReplace.wdReplaceAll)
        End With
        With WordAPP.Selection.Find
            .ClearFormatting()
            .Replacement.ClearFormatting()
            .Text = "C9"
            .Replacement.Text = "20^l5"
            .MatchCase = True
            .Execute(, , , , , , , , , , Word.WdReplace.wdReplaceAll)
        End With

        WordAPP.Visible = True

'^l为手动换行符 1为字母L 也以换个两个数字1 换成数字后如果后面跟数字可能会有点问题。所以建议使用字母

'^p为硬回车

图1为没有替换这前的模板内容

图2为替换了内容后的文档