文本講述經過C#和VB.NET程序代碼給Word中的表格設置邊框的方法,可分爲給Table表格設置邊框、給表格中的指定Cell設置邊框,設置邊框時,可設置邊框顏色、邊框類型、邊框線條樣式、邊框線條粗細等等。html
編輯代碼前,先下載須要的Word類庫工具,本文中使用的是Spire的免費版庫Free Spire.Doc for .NET。下載後,須要解壓安裝。在VS程序中將安裝路徑下Bin文件下的Spire.Doc.dll文件添加引用到「解決方案資源管理器」,以下添加引用結果:ide
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]spa
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#]code
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"); } } }
單元格邊框設置結果:
[VB.NET]orm
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