概述

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

工具导入

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

代码示例

1. 设置Table边框

[C#]

  1. using Spire.Doc;
  2. using System.Drawing;

  3. namespace SetTableBorder_Doc
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             //加载Word文档
  10.             Document doc = new Document();
  11.             doc.LoadFromFile("test.docx");

  12.             //获取Section
  13.             Section section = doc.Sections[0];

  14.             //获取第一个表格
  15.             Table table = section.Tables[0] as Table;

  16.             //设置上边框
  17.             table.TableFormat.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.DotDash;
  18.             table.TableFormat.Borders.Top.LineWidth = 2.0F;
  19.             table.TableFormat.Borders.Top.Color = Color.Red;
  20.             
  21.             //设置右边框
  22.             table.TableFormat.Borders.Right.BorderType = Spire.Doc.Documents.BorderStyle.Double;
  23.             table.TableFormat.Borders.Right.LineWidth = 2.0F;
  24.             table.TableFormat.Borders.Right.Color = Color.Green;

  25.             //设置下边框
  26.             table.TableFormat.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.None;

  27.             //设置左边框
  28.             table.TableFormat.Borders.Left.BorderType = Spire.Doc.Documents.BorderStyle.Hairline;
  29.             table.TableFormat.Borders.Left.LineWidth = 2.0F;
  30.             table.TableFormat.Borders.Left.Color = Color.Blue;

  31.             //设置垂直边框
  32.             table.TableFormat.Borders.Vertical.BorderType = Spire.Doc.Documents.BorderStyle.Dot;
  33.             table.TableFormat.Borders.Vertical.LineWidth = 2.0F;
  34.             table.TableFormat.Borders.Vertical.Color = Color.Orange;

  35.             //设置水平边框
  36.             table.TableFormat.Borders.Horizontal.BorderType = Spire.Doc.Documents.BorderStyle.Wave;
  37.             table.TableFormat.Borders.Horizontal.LineWidth = 2.0F;
  38.             table.TableFormat.Borders.Horizontal.Color = Color.Purple;

  39.             
  40.             //保存文档
  41.             doc.SaveToFile("SetTableBorder.docx",FileFormat.Docx2013);
  42.             System.Diagnostics.Process.Start("SetTableBorder.docx");
  43.         }
  44.     }
  45. }

表格边框设置结果:

C#/VB.NET 设置Word表格边框-LMLPHP

[VB.NET]

  1. Imports Spire.Doc
  2. Imports System.Drawing

  3. Namespace SetTableBorder_Doc
  4.     Class Program
  5.         Private Shared Sub Main(args As String())
  6.             '加载Word文档
  7.             Dim doc As New Document()
  8.             doc.LoadFromFile("test.docx")

  9.             '获取Section
  10.             Dim section As Section = doc.Sections(0)

  11.             '获取第一个表格
  12.             Dim table As Table = TryCast(section.Tables(0), Table)

  13.             '设置上边框
  14.             table.TableFormat.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.DotDash
  15.             table.TableFormat.Borders.Top.LineWidth = 2F
  16.             table.TableFormat.Borders.Top.Color = Color.Red

  17.             '设置右边框
  18.             table.TableFormat.Borders.Right.BorderType = Spire.Doc.Documents.BorderStyle.[Double]
  19.             table.TableFormat.Borders.Right.LineWidth = 2F
  20.             table.TableFormat.Borders.Right.Color = Color.Green

  21.             '设置下边框
  22.             table.TableFormat.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.None

  23.             '设置左边框
  24.             table.TableFormat.Borders.Left.BorderType = Spire.Doc.Documents.BorderStyle.Hairline
  25.             table.TableFormat.Borders.Left.LineWidth = 2F
  26.             table.TableFormat.Borders.Left.Color = Color.Blue

  27.             '设置垂直边框
  28.             table.TableFormat.Borders.Vertical.BorderType = Spire.Doc.Documents.BorderStyle.Dot
  29.             table.TableFormat.Borders.Vertical.LineWidth = 2F
  30.             table.TableFormat.Borders.Vertical.Color = Color.Orange

  31.             '设置水平边框
  32.             table.TableFormat.Borders.Horizontal.BorderType = Spire.Doc.Documents.BorderStyle.Wave
  33.             table.TableFormat.Borders.Horizontal.LineWidth = 2F
  34.             table.TableFormat.Borders.Horizontal.Color = Color.Purple


  35.             '保存文档
  36.             doc.SaveToFile("SetTableBorder.docx", FileFormat.Docx2013)
  37.             System.Diagnostics.Process.Start("SetTableBorder.docx")
  38.         End Sub
  39.     End Class
  40. End Namespace

2. 设置Cell边框

[C#]

  1. using Spire.Doc;
  2. using System.Drawing;

  3. namespace SetCellBorder_Doc
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             //加载Word文档
  10.             Document doc = new Document();
  11.             doc.LoadFromFile("test.docx");

  12.             //获取Section
  13.             Section section = doc.Sections[0];

  14.             //获取第一个表格
  15.             Table table = section.Tables[0] as Table;

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

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

  32.             //保存文档
  33.             doc.SaveToFile("SetCellBorder.docx",FileFormat.Docx2013);
  34.             System.Diagnostics.Process.Start("SetCellBorder.docx");
  35.         }
  36.     }
  37. }

单元格边框设置结果:

C#/VB.NET 设置Word表格边框-LMLPHP
[VB.NET]

  1. Imports Spire.Doc
  2. Imports System.Drawing

  3. Namespace SetCellBorder_Doc
  4.     Class Program
  5.         Private Shared Sub Main(args As String())
  6.             '加载Word文档
  7.             Dim doc As New Document()
  8.             doc.LoadFromFile("test.docx")

  9.             '获取Section
  10.             Dim section As Section = doc.Sections(0)

  11.             '获取第一个表格
  12.             Dim table As Table = TryCast(section.Tables(0), Table)

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

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

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


09-25 01:31