添加、修改和移除 Word 表格边框能够增强数据的可读性、美观性和组织结构。添加边框可以使表格内容更加清晰,区分不同的单元格,便于读者快速识别信息。修改边框样式(如线条粗细、颜色或样式)可以强调关键数据,引导视觉流向,或者符合特定的文档风格和设计需求。移除边框则在某些情况下减少视觉杂乱,使得内容更加紧凑、简约,特别适用于不需要严格划分或希望弱化结构显示的数据展示。本文将介绍如何使用 Spire.Doc for .NET 在 C# 项目中添加、修改和移除 Word 表格边框。
安装 Spire.Doc for .NET
首先,您需要将 Spire.Doc for.NET 包含的 DLL 文件作为引用添加到您的 .NET项目中。DLL 文件可以从此链接下载,也可以通过 NuGet 安装。
PM> Install-Package Spire.DocC# 添加 Word 表格边框
对整个 Word 表格的所有单元格设置边框,需遍历每个单元格,设置它的可视化的边框属性。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.LoadFromFile() 方法加载一个文档。
- 使用 Document .Sections[0] 获取文档的第一个节
- 使用 Section.Tables[0] 获取节中的第一个表格
- 使用 for 循环遍历表格中的所有单元格。
- 设置 TableCell.CellFormat.Borders.BorderType 为 BorderStyle.Single,即设置单元格边框为单线样式。
- 设置 TableCell.CellFormat.Borders.LineWidth 为 1.5,即定义边框的宽度为 1.5pt。
- 设置 TableCell.CellFormat.Borders.Color 为 Color.Black,即设置边框颜色为黑色。
- 使用 Document.SaveToFile() 方法保存到 Word 文档。
- C#
using Spire.Doc;
namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建一个新的Document对象
            Document doc = new Document();
            // 从文件加载文档
            doc.LoadFromFile("表格示例1.docx");
            // 获取文档的第一个节
            Section section = doc.Sections[0];
            // 获取该节中第一个表格
            Table table = (Table)section.Tables[0];
            // 声明TableRow和TableCell变量,以便在循环中使用
            TableRow tableRow;
            TableCell tableCell;
            // 遍历表格的所有行
            for (int i = 0; i < table.Rows.Count; i++)
            {
                // 获取当前行
                tableRow = table.Rows[i];
                // 遍历当前行的所有单元格
                for (int j = 0; j < tableRow.Cells.Count; j++)
                {
                    // 获取当前单元格
                    tableCell = tableRow.Cells[j];
                    // 设置当前单元格的边框样式为单线
                    tableCell.CellFormat.Borders.BorderType = Spire.Doc.Documents.BorderStyle.Single;
                   // 设置边框的宽度
                    tableCell.CellFormat.Borders.LineWidth = 1.5;
                   // 设置边框的颜色 
                    tableCell.CellFormat.Borders.Color = Color.Black;
                }
            }
            // 将修改后的文档保存为新的文件
            doc.SaveToFile("添加单元格边框.docx", FileFormat.Docx2016);
            // 关闭文档,释放资源
            doc.Close();
        }
    }
}
C# 修改 Word 表格边框
Spire.Doc 提供了一系列的边框属性,比如边框样式 TableCell.CellFormat.Borders.BorderType,边框宽度 TableCell.CellFormat.Borders.LineWidth,边框颜色 TableCell.CellFormat.Borders.Color 等。可以根据需要自定义这些属性来实现想要的效果。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.LadFromFile() 方法加载一个文档。
- 使用 Document .Sections[0] 获取文档的第一个节。
- 使用 Section.Tables[0] 获取节中的第一个表格。
- 使用 for 循环遍历表格中的需要更改边框样式的单元格。
- 更改右边框的颜色 TableCell.CellFormat.Borders.Bottom.Color 为 Color.PaleVioletRed。
- 更改右边框的样式 TableCell.CellFormat.Borders.Bottom.BorderType 为 BorderStyle.DotDash。
- 更改右边框的宽度 TableCell.CellFormat.Borders.Bottom.LineWidth 为 2。
- 使用 Document.SaveToFile() 方法保存到 Word 文档。
- C#
using Spire.Doc;
using System.Drawing;
namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建一个新的Document对象
            Document doc = new Document();
            // 从文件加载文档
            doc.LoadFromFile("表格示例2.docx");
            // 获取文档的第一个节
            Section section = doc.Sections[0];
            // 获取该节中第一个表格
            Table table = (Table)section.Tables[0];
            // 声明TableRow,以便在循环中使用
            TableRow tableRow;
            // 遍历表格的所有行
            for (int i = 1; i < table.Rows.Count - 1; i++)
            {
                tableRow = table.Rows[i];
                // 设置当前单元格的右边框颜色
                tableRow.Cells[1].CellFormat.Borders.Bottom.Color = Color.PaleVioletRed;
                // 设置当前单元格的右边框样式为点线
                tableRow.Cells[1].CellFormat.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.DotDash;
                // 设置右边框的宽度
                tableRow.Cells[1].CellFormat.Borders.Bottom.LineWidth = 2;
            }
            // 将修改后的文档保存为新的文件
            doc.SaveToFile("修改单元格边框.docx", FileFormat.Docx2016);
            // 关闭文档,释放资源
            doc.Close();
        }
    }
}
C# 移除 Word 表格边框
在 Word 文档处理过程中,不仅能够对整个表格应用边框样式,还能细化到对单个单元格进行边框自定义。若要彻底清除表格中的所有边框,建议采取两步走策略:首先,对表格本身进行边框去除设置;其次,逐个访问表格内的单元格并取消其边框样式。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.LoadFromFile() 方法加载一个文档。
- 使用 Section.Body 获取文档一个节的正文部分。
- 使用 Body.Tables[0] 获取正文部分的第一个表格。
- 使用 for 循环遍历表格中的所有单元格。
- 使用 Table.TableFormat.Borders.BorderType =BorderStyle.None 对表格进行边框去除设置。
- 使用 TableCell.CellFormat.Borders.BorderType = BorderStyle.None 对单元格进行边框去除设置。
- 使用 Document.SaveToFile() 方法保存到文档。
- C#
using Spire.Doc;
using System.Drawing;
namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建一个新的Document对象
            Document doc = new Document();
            // 从文件加载文档
            doc.LoadFromFile("表格示例2.docx");
            // 获取文档的第一个节
            Section section = doc.Sections[0];
            // 获取该节中第一个表格
            Table table = (Table)section.Tables[0];
            // 移除表格设置的边框
            table.TableFormat.Borders.BorderType = Spire.Doc.Documents.BorderStyle.None;
            // 声明TableRow,以便在循环中使用
            TableRow tableRow;
            // 遍历表格的所有行
            for (int i = 0; i < table.Rows.Count; i++)
            {
                tableRow = table.Rows[i];
                for (int j = 0; j < tableRow.Cells.Count; j++)
                {
                    // 移除单元格设置的所有边框
                    tableRow.Cells[j].CellFormat.Borders.BorderType = Spire.Doc.Documents.BorderStyle.None;
                }
            }
            // 将修改后的文档保存为新的文件
            doc.SaveToFile("移除单元格边框.docx", FileFormat.Docx2016);
            // 关闭文档,释放资源
            doc.Close();
        }
    }
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 



 
					



