下边框_C#/VB.NET 设置Word表格边框

本文详细介绍了如何使用C#和VB.NET编程语言,结合Spire.Doc库来设置Word文档中表格的边框。代码示例分别展示了如何设置整个表格的边框,以及对表格中特定单元格的边框进行定制,包括边框类型、线条样式、线条宽度和颜色等属性。通过这些方法,开发者可以灵活地为Word文档的表格添加各种风格的边框效果。
摘要由CSDN通过智能技术生成

概述

文本讲述通过C#和http://VB.NET程序代码给Word中的表格设置边框的方法,可分为给Table表格设置边框、给表格中的指定Cell设置边框,设置边框时,可设置边框颜色、边框类型、边框线条样式、边框线条粗细等等。

工具导入

编辑代码前,先下载需要的Word类库工具,本文中使用的是Spire的免费版库Free Spire.Doc for .NET。下载后,需要解压安装。在VS程序中将安装路径下Bin文件下的Spire.Doc.dll文件添加引用到“解决方案资源管理器”,如下添加引用结果:

885b294abc51912205dc88dd8d4dad7d.png

代码示例

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");
        }
    }
}

表格边框设置结果:

72623e48954b7e0d234436f5b0c9977c.png

[http://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 SetCellBorder_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;

            //获取单元格,设置上、下边框
            TableCell cell1 = table[0, 0];
            cell1.CellFormat.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.Single;
            cell1.CellFormat.Borders.Top.LineWidth = 2.0F;
            cell1.CellFormat.Borders.Top.Color = Color.Red;
            cell1.CellFormat.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.DashDotStroker;
            cell1.CellFormat.Borders.Bottom.LineWidth = 2.0F;
            cell1.CellFormat.Borders.Bottom.Color = Color.Pink;

            //获取单元格,设置左、右边框
            TableCell cell2 = table[2, 2];
            cell2.CellFormat.Borders.Left.BorderType = Spire.Doc.Documents.BorderStyle.Hairline;
            cell2.CellFormat.Borders.Left.LineWidth = 2.0F;
            cell2.CellFormat.Borders.Left.Color = Color.Yellow;
            cell2.CellFormat.Borders.Right.BorderType = Spire.Doc.Documents.BorderStyle.Double;
            cell2.CellFormat.Borders.Right.LineWidth = 2.0F;
            cell2.CellFormat.Borders.Right.Color = Color.HotPink;

            //保存文档
            doc.SaveToFile("SetCellBorder.docx",FileFormat.Docx2013);
            System.Diagnostics.Process.Start("SetCellBorder.docx");
        }
    }
}

单元格边框设置结果:

26098fbd8c0dc1d187698dc4311266cd.png

[http://VB.NET]

Imports Spire.Doc
Imports System.Drawing

Namespace SetCellBorder_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)

			'获取单元格,设置上、下边框
			Dim cell1 As TableCell = table(0, 0)
			cell1.CellFormat.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.[Single]
			cell1.CellFormat.Borders.Top.LineWidth = 2F
			cell1.CellFormat.Borders.Top.Color = Color.Red
			cell1.CellFormat.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.DashDotStroker
			cell1.CellFormat.Borders.Bottom.LineWidth = 2F
			cell1.CellFormat.Borders.Bottom.Color = Color.Pink

			'获取单元格,设置左、右边框
			Dim cell2 As TableCell = table(2, 2)
			cell2.CellFormat.Borders.Left.BorderType = Spire.Doc.Documents.BorderStyle.Hairline
			cell2.CellFormat.Borders.Left.LineWidth = 2F
			cell2.CellFormat.Borders.Left.Color = Color.Yellow
			cell2.CellFormat.Borders.Right.BorderType = Spire.Doc.Documents.BorderStyle.[Double]
			cell2.CellFormat.Borders.Right.LineWidth = 2F
			cell2.CellFormat.Borders.Right.Color = Color.HotPink

			'保存文档
			doc.SaveToFile("SetCellBorder.docx", FileFormat.Docx2013)
			System.Diagnostics.Process.Start("SetCellBorder.docx")
		End Sub
	End Class
End Namespace

(完)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值