需要做一个报表,报表的模板写了,其中报表的一个单元格内容为:本月总共参加会议的人数<人数>,现在我需要做的功能就是将<人数>替换。查了写资料写了如下代码Dim App As New MSExcel.Application
Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open(ProjectPath & "Attachments\测试.xlsx")
Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1)
Dim Rg As MSExcel.Range = Ws.UsedRange
For Each dr As DataRow In DataTables("表A").DataRows
Dim s As String
s ="<" & dr("项目信息") & ">"
rg.Replace(What:=s, Replacement:=dr("值"))
Next
app.visible = True
功能是实现了,但是却在弹出报表前报错了,这里是不是可以先判断下条件,该怎么进行修改
问题2,通过广论坛发现一个类似的报表替换例子:
Dim app As New MSWord.Application try Dim fileName = "d:\test.doc" Dim doc = app.Documents.Open(fileName) app.Selection.Find.Text = "[第一列]" app.Selection.Find.Replacement.ClearFormatting() app.Selection.Find.Replacement.Text = "123456789" app.Selection.Find.Execute(Replace:=MSWord.WdReplace.wdReplaceAll) app.visible = True catch ex As exception msgbox(ex.message) app.Quit finally End try 这段代码如何改成Excel的效果 |