添加表格
添加简单表格的代码可以参考:
Dim
wdc
As
New
WordCreator()
Dim
tb
As
New
Word.Objects.RtfTable(4, 5)
'定义一个四行五列的表格
wdc.Add(tb)
'将表格添加到文档中
For
r
As
Integer
= 0
To
3
'遍历行
For
c
As
Integer
= 0
To
4
'遍历列
Dim
str
As
String
= CExp("{0}行{1}列",
r, c)
'要显示的文本
Dim
rtr
As
New
Word.Objects.RtfString(str)
'定义一个RtfString用于显示内文本
tb.Rows(r).Cells(c).Content.Add(rtr)
'将RtfString添加到单元格内容中
tb.Rows(r).Cells(c).SetRectBorder(Word.RtfBorderStyle.Single, color.Red,
1)
'设置单元格边框
Next
Next
Dim
fl
As
String
=
"c:\temp\test.docx"
wdc.Save(fl)
Process.Start(fl)
或:
Dim
wdc
As
New
WordCreator()
Dim
tb
As
New
Word.Objects.RtfTable(4, 5)
'定义一个四行五列的表格
wdc.Add(tb)
'将表格添加到文档中
For
r
As
Integer
= 0
To
3
'遍历行
For
c
As
Integer
= 0
To
4
'遍历列
Dim
str
As
String
= CExp("{0}行{1}列",
r, c)
'要显示的文本
Dim
rpg
As
New
Word.Objects.RtfParagraph()
'定义一个RtfParagraph
rpg.Add(New
Word.Objects.RtfString( str))
'将显示内容添加到段落中
tb.Rows(r).Cells(c).Content.Add(rpg)
'将RtfParagraph添加到单元格内容中
tb.Rows(r).Cells(c).SetRectBorder(Word.RtfBorderStyle.Single, color.Red,
1)
'设置单元格边框
Next
Next
Dim
fl
As
String
=
"c:\temp\test.docx"
wdc.Save(fl)
Process.Start(fl)
两段代码生成的文档是一样的:
单元格是一个容器,可以在其中添加任何对象,包括RtfString、RtfParagraph或图片等等,一般来说,简单的文本直接用RtfString就行,如果需要复杂的文本格式,则用RtfParagraph。