'第一段代码:写入word模板,生成word报表:
Dim clbx1 As WinForm.CheckedListBox = e.Form.Controls("CheckedListBox1")
Dim clbx2 As WinForm.CheckedListBox = e.Form.Controls("CheckedListBox2")
For Each j As String In clbx2.CheckedIndices
Dim tm As String = ProjectPath & "模板文件\" & clbx2.Items(j) '指定模板文件
For Each i As Integer In clbx1.CheckedIndices
Dim fl As String = ProjectPath & "成品文件\" & clbx1.items(i) & clbx2.Items(j) '指定目标文件
'文件已经存在,是否覆盖重新填写
If FileSys.FileExists(fl) Then
If MessageBox.Show(fl & "文件已经存在,是否覆盖重新填写?","提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then
Continue For
End If
End If
Dim nm As String = CurrentTable.Name
Dim dr As DataRow =DataTables(nm).Find("名称 = '" & clbx1.items(i) & "'")
'
If dr IsNot Nothing
Dim wrt As New WordReport(Tables(nm),tm,fl) '定义一个WordReport
wrt.BuildOne(dr)
wrt.quit
Dim app As New MSWord.Application
try
Dim doc = app.Documents.Open(fl)
For Each k As object In doc.Paragraphs
If Len(Trim(k.Range.text)) = 1 Then k.Range.Delete
Next
doc.Content.Find.Execute(FindText:="^l", replacewith:="^p", Replace:=2)
doc.Content.Find.Execute(FindText:="^13", replacewith:="^p", Replace:=2)
app.Selection.WholeStory '全选
With app.Selection.ParagraphFormat '选定区域段落设置
.CharacterUnitFirstLineIndent = 0 '取消首行缩进
.FirstLineIndent = 0
.CharacterUnitLeftIndent = 2 '左缩进2字符
End With
app.Selection.HomeKey(unit:=6) '光标移至文首
doc.save
app.quit
catch ex As exception
msgbox(ex.message)
app.quit
End try
End If
Next
Next