边框与背景
Cell(单元格)有个BackFilling属性,用于设置单元格的背景颜色
Cell(单元格)有个SetRectBorder方法,用于设置单元格的边框,其语法为:
SetRectBorder(Style, Color, Width)
参数 | 说明 |
Style | 边框类型,RtfBorderStyle型枚举 |
Color | 边框颜色 |
Width | 边框宽度 |
示例一
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))
'将显示内容添加到段落中
Dim
cell
As
word.Objects.RtfCell = tb.Rows(r).Cells(c)
'定义一个变量引用单元格
cell.Content.Add(rpg)
'将RtfParagraph添加到单元格内容中
cell.SetRectBorder(Word.RtfBorderStyle.Single, color.DimGray, 1)
'设置默认边框
If
c = 2
Then
cell.SetRectBorder(Word.RtfBorderStyle.Single, color.Red, 1)
'设置第三列边框
End
If
If
r = 3
Then
cell.BackFilling = Color.LightCyan
'设置第四行背景
End
If
Next
Next
Dim
fl
As
String
=
"c:\temp\test.docx"
wdc.Save(fl)
Process.Start(fl)
得到的文档为:
如果要单独设置某个方向的边框,则不能使用SetRectBorder方法,需要使用 以下属性:
属性 | 说明 |
TopBorderStyle TopBorderColor TopBorderWidth |
用于设置上边框的类型、颜色和宽度 |
BottomBorderStyle BottomBorderColor BottomBorderWidth |
用于设置下边框的类型、颜色和宽度 |
LeftBorderStyle LeftBorderColor LeftBorderWidth |
用于设置左边框的类型、颜色和宽度 |
RightBorderStyle RightBorderColor RightBorderWidth |
用于设置右边框的类型、颜色和宽度 |
BackwardDiagonalStyle BackwardDiagonalColor BackwardDiagonalWidth |
用于设置反斜线的类型、颜色和宽度 |
ForwardDiagonalStyle ForwardDiagonalColor ForwardDiagonalWidth |
用于设置正斜线的类型、颜色和宽度 |
示例二
Dim
wdc
As
New
WordCreator()
Dim
tb
As
New
Word.Objects.RtfTable(4, 4)
'定义一个四行五列的表格
wdc.Add(tb)
'将表格添加到文档中
For
r
As
Integer
= 0
To
tb.Rows.Count - 1
'遍历行
For
c
As
Integer
= 0
To
tb.ColumnCount - 1
'遍历列
Dim
cell
As
word.Objects.RtfCell = tb.Rows(r).Cells(c)
'定义一个变量引用单元格
cell.SetRectBorder(Word.RtfBorderStyle.Single, color.DimGray, 1)
'设置默认边框
If
c = 1
Then
'设置第二列的右边框
cell.RightBorderColor = Color.Red
cell.RightBorderStyle = Word.RtfBorderStyle.Double
End
If
If
r = 1
Then
'设置第二行的底边框
cell.BottomBorderColor = Color.Red
cell.BottomBorderStyle = Word.RtfBorderStyle.Double
End
If
If
r = 0
AndAlso
c = 0
Then
'在第一个单元格显示一个斜线
cell.BackwardDiagonalColor = Color.Red
cell.BackwardDiagonalWidth = 1
End
If
Next
tb.Rows(r).Height = 40
'设置行高
Next
Dim
fl
As
String
=
"c:\temp\test.docx"
wdc.Save(fl)
Process.Start(fl)
生成的文档为: