C# 设置Word表格和单元格边框
TO BE 人气:0概述
文本通过C#和VB.NET程序代码给Word中的表格设置边框的方法,可分为给Table表格设置边框、给表格中的指定单元格Cell设置边框,设置边框时,可设置边框颜色、边框类型、边框线条样式、边框线条粗细等等。
程序代码
1. 设置Table边框
在程序中引入如下程序集文件:
【C#】
using Spire.Doc;
using System.Drawing;
namespace SetTableBorder_Doc
{
class Program
{
static void Main(string[] args)
{
//加载Word文档
Document doc = new Document();
doc.LoadFromFile("test.docx");
//获取Section
Section section = doc.Sections[0];
//获取第一个表格
Table table = section.Tables[0] as Table;
//设置上边框
table.TableFormat.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.DotDash;
table.TableFormat.Borders.Top.LineWidth = 2.0F;
table.TableFormat.Borders.Top.Color = Color.Red;
//设置右边框
table.TableFormat.Borders.Right.BorderType = Spire.Doc.Documents.BorderStyle.Double;
table.TableFormat.Borders.Right.LineWidth = 2.0F;
table.TableFormat.Borders.Right.Color = Color.Green;
//设置下边框
table.TableFormat.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.None;
//设置左边框
table.TableFormat.Borders.Left.BorderType = Spire.Doc.Documents.BorderStyle.Hairline;
table.TableFormat.Borders.Left.LineWidth = 2.0F;
table.TableFormat.Borders.Left.Color = Color.Blue;
//设置垂直边框
table.TableFormat.Borders.Vertical.BorderType = Spire.Doc.Documents.BorderStyle.Dot;
table.TableFormat.Borders.Vertical.LineWidth = 2.0F;
table.TableFormat.Borders.Vertical.Color = Color.Orange;
//设置水平边框
table.TableFormat.Borders.Horizontal.BorderType = Spire.Doc.Documents.BorderStyle.Wave;
table.TableFormat.Borders.Horizontal.LineWidth = 2.0F;
table.TableFormat.Borders.Horizontal.Color = Color.Purple;
//保存文档
doc.SaveToFile("SetTableBorder.docx",FileFormat.Docx2013);
System.Diagnostics.Process.Start("SetTableBorder.docx");
}
}
}
表格边框设置结果:
【VB.NET】
Imports Spire.Doc
Imports System.Drawing
Namespace SetTableBorder_Doc
Class Program
Private Shared Sub Main(args As String())
'加载Word文档
Dim doc As New Document()
doc.LoadFromFile("test.docx")
'获取Section
Dim section As Section = doc.Sections(0)
'获取第一个表格
Dim table As Table = TryCast(section.Tables(0), Table)
'设置上边框
table.TableFormat.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.DotDash
table.TableFormat.Borders.Top.LineWidth = 2F
table.TableFormat.Borders.Top.Color = Color.Red
'设置右边框
table.TableFormat.Borders.Right.BorderType = Spire.Doc.Documents.BorderStyle.[Double]
table.TableFormat.Borders.Right.LineWidth = 2F
table.TableFormat.Borders.Right.Color = Color.Green
'设置下边框
table.TableFormat.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.None
'设置左边框
table.TableFormat.Borders.Left.BorderType = Spire.Doc.Documents.BorderStyle.Hairline
table.TableFormat.Borders.Left.LineWidth = 2F
table.TableFormat.Borders.Left.Color = Color.Blue
'设置垂直边框
table.TableFormat.Borders.Vertical.BorderType = Spire.Doc.Documents.BorderStyle.Dot
table.TableFormat.Borders.Vertical.LineWidth = 2F
table.TableFormat.Borders.Vertical.Color = Color.Orange
'设置水平边框
table.TableFormat.Borders.Horizontal.BorderType = Spire.Doc.Documents.BorderStyle.Wave
table.TableFormat.Borders.Horizontal.LineWidth = 2F
table.TableFormat.Borders.Horizontal.Color = Color.Purple
'保存文档
doc.SaveToFile("SetTableBorder.docx", FileFormat.Docx2013)
System.Diagnostics.Process.Start("SetTableBorder.docx")
End Sub
End Class
End Namespace
2. 设置Cell边框
引入如下程序集文件:
【C#】
using Spire.Doc;
using System.Drawing;
namespace SetTableBorder_Doc
{
class Program
{
static void Main(string[] args)
{
//加载Word文档
Document doc = new Document();
doc.LoadFromFile("test.docx");
//获取Section
Section section = doc.Sections[0];
//获取第一个表格
Table table = section.Tables[0] as Table;
//设置上边框
table.TableFormat.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.DotDash;
table.TableFormat.Borders.Top.LineWidth = 2.0F;
table.TableFormat.Borders.Top.Color = Color.Red;
//设置右边框
table.TableFormat.Borders.Right.BorderType = Spire.Doc.Documents.BorderStyle.Double;
table.TableFormat.Borders.Right.LineWidth = 2.0F;
table.TableFormat.Borders.Right.Color = Color.Green;
//设置下边框
table.TableFormat.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.None;
//设置左边框
table.TableFormat.Borders.Left.BorderType = Spire.Doc.Documents.BorderStyle.Hairline;
table.TableFormat.Borders.Left.LineWidth = 2.0F;
table.TableFormat.Borders.Left.Color = Color.Blue;
//设置垂直边框
table.TableFormat.Borders.Vertical.BorderType = Spire.Doc.Documents.BorderStyle.Dot;
table.TableFormat.Borders.Vertical.LineWidth = 2.0F;
table.TableFormat.Borders.Vertical.Color = Color.Orange;
//设置水平边框
table.TableFormat.Borders.Horizontal.BorderType = Spire.Doc.Documents.BorderStyle.Wave;
table.TableFormat.Borders.Horizontal.LineWidth = 2.0F;
table.TableFormat.Borders.Horizontal.Color = Color.Purple;
//保存文档
doc.SaveToFile("SetTableBorder.docx",FileFormat.Docx2013);
System.Diagnostics.Process.Start("SetTableBorder.docx");
}
}
}
单元格边框设置结果:
【VB.NET】
Imports Spire.Doc
Imports System.Drawing
Namespace SetTableBorder_Doc
Class Program
Private Shared Sub Main(args As String())
'加载Word文档
Dim doc As New Document()
doc.LoadFromFile("test.docx")
'获取Section
Dim section As Section = doc.Sections(0)
'获取第一个表格
Dim table As Table = TryCast(section.Tables(0), Table)
'设置上边框
table.TableFormat.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.DotDash
table.TableFormat.Borders.Top.LineWidth = 2F
table.TableFormat.Borders.Top.Color = Color.Red
'设置右边框
table.TableFormat.Borders.Right.BorderType = Spire.Doc.Documents.BorderStyle.[Double]
table.TableFormat.Borders.Right.LineWidth = 2F
table.TableFormat.Borders.Right.Color = Color.Green
'设置下边框
table.TableFormat.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.None
'设置左边框
table.TableFormat.Borders.Left.BorderType = Spire.Doc.Documents.BorderStyle.Hairline
table.TableFormat.Borders.Left.LineWidth = 2F
table.TableFormat.Borders.Left.Color = Color.Blue
'设置垂直边框
table.TableFormat.Borders.Vertical.BorderType = Spire.Doc.Documents.BorderStyle.Dot
table.TableFormat.Borders.Vertical.LineWidth = 2F
table.TableFormat.Borders.Vertical.Color = Color.Orange
'设置水平边框
table.TableFormat.Borders.Horizontal.BorderType = Spire.Doc.Documents.BorderStyle.Wave
table.TableFormat.Borders.Horizontal.LineWidth = 2F
table.TableFormat.Borders.Horizontal.Color = Color.Purple
'保存文档
doc.SaveToFile("SetTableBorder.docx", FileFormat.Docx2013)
System.Diagnostics.Process.Start("SetTableBorder.docx")
End Sub
End Class
End Namespace
总结:以上是本次关于C#及VB.NET程序来设置Word表格和单元格边框的方法。代码中的文件路径为 C:\Users\Administrator\Documents\Visual Studio 2013\Projects\CreateTable_Doc\SetTableBorder_Doc\bin\Debug,文件路径也可自定义。
(如需转载,请注明出处!)
加载全部内容