使用 DocX 库在 C# 中创建和格式化 Word 表格无需安装 Microsoft Office,且操作轻量、跨平台。以下是详细的实现步骤、示例代码及关键格式化技巧:
一、准备工作:安装 DocX 库
通过 NuGet 包管理器安装 DocX(由 Xceed 开发):
- 方式 1:NuGet 控制台执行
bash运行
Install-Package DocX - 方式 2:Visual Studio 中右键项目 → 管理 NuGet 程序包 → 搜索 “DocX” 并安装。
二、核心功能示例:创建并格式化表格
以下示例涵盖表格创建、内容填充、样式美化、布局调整等核心需求:
csharp
运行
using DocX;
using System;
using System.Drawing;class DocXTableDemo
{static void Main(){// 1. 创建或打开Word文档string filePath = @"C:\Temp\EmployeeTable.docx";using (DocX document = DocX.Create(filePath)) // 新建文档(若打开现有文档用DocX.Load(filePath)){// 添加标题document.InsertParagraph("员工信息表").FontSize(18).Bold().Alignment = Alignment.center;document.InsertParagraph("\n"); // 换行// 2. 创建表格(4行5列)Table table = document.AddTable(4, 5);// 3. 基础样式设置table.Design = TableDesign.MediumGridAccent2; // 内置表格样式(可选:LightGrid、DarkList等)table.Alignment = Alignment.center; // 表格整体居中// 4. 填充内容(表头+数据)// 表头table.Rows[0].Cells[0].Paragraphs.First().Append("序号").Bold().FontSize(11);table.Rows[0].Cells[1].Paragraphs.First().Append("姓名").Bold().FontSize(11);table.Rows[0].Cells[2].Paragraphs.First().Append("部门").Bold().FontSize(11);table.Rows[0].Cells[3].Paragraphs.First().Append("年龄").Bold().FontSize(11);table.Rows[0].Cells[4].Paragraphs.First().Append("入职日期").Bold().FontSize(11);// 数据行table.Rows[1].Cells[0].Paragraphs.First().Append("1");table.Rows[1].Cells[1].Paragraphs.First().Append("张三");table.Rows[1].Cells[2].Paragraphs.First().Append("研发部");table.Rows[1].Cells[3].Paragraphs.First().Append("28");table.Rows[1].Cells[4].Paragraphs.First().Append("2023-01-15");table.Rows[2].Cells[0].Paragraphs.First().Append("2");table.Rows[2].Cells[1].Paragraphs.First().Append("李四");table.Rows[2].Cells[2].Paragraphs.First().Append("市场部");table.Rows[2].Cells[3].Paragraphs.First().Append("32");table.Rows[2].Cells[4].Paragraphs.First().Append("2022-05-20");table.Rows[3].Cells[0].Paragraphs.First().Append("3");table.Rows[3].Cells[1].Paragraphs.First().Append("王五");table.Rows[3].Cells[2].Paragraphs.First().Append("人事部");table.Rows[3].Cells[3].Paragraphs.First().Append("25");table.Rows[3].Cells[4].Paragraphs.First().Append("2024-03-10");// 5. 高级格式化// (1)表头样式:背景色、文字居中foreach (TableCell cell in table.Rows[0].Cells){cell.FillColor = Color.FromArgb(200, 220, 255); // 自定义背景色cell.Paragraphs.First().Alignment = Alignment.center; // 文字居中}// (2)调整列宽(单位:磅)table.SetColumnWidth(0, 40); // 序号列table.SetColumnWidth(1, 80); // 姓名列table.SetColumnWidth(2, 100); // 部门列table.SetColumnWidth(3, 50); // 年龄列table.SetColumnWidth(4, 100); // 入职日期列// (3)设置单元格内边距table.CellSpacing = 5; // 单元格间距foreach (TableRow row in table.Rows){foreach (TableCell cell in row.Cells){cell.MarginTop = 5;cell.MarginBottom = 5;cell.MarginLeft = 5;cell.MarginRight = 5;}}// (4)合并单元格(示例:合并最后一行的“备注”列)// 先新增一行,再合并单元格table.InsertRow(table.Rows[3]); // 复制第3行作为新行table.Rows[4].Cells[0].Paragraphs.First().Append("4");table.Rows[4].MergeCells(1, 4); // 合并第4行的第1列到第4列(索引从0开始)table.Rows[4].Cells[1].Paragraphs.First().Append("(实习生)").Italic().Color(Color.Gray);// 6. 将表格插入文档document.InsertTable(table);// 7. 保存文档document.Save();Console.WriteLine($"表格已生成:{filePath}");}}
}
三、关键格式化技巧
-
表格样式预设
TableDesign枚举提供多种内置样式,如:TableDesign.LightGrid:浅色网格TableDesign.DarkList:深色列表TableDesign.MediumGridAccent3:中等网格 + 强调色 3
-
单元格操作
- 合并单元格:
table.Rows[i].MergeCells(startCol, endCol) - 拆分单元格:
table.Rows[i].Cells[j].SplitCells(rowCount, colCount) - 设置边框:
table.SetBorder(BorderType.Bottom, new Border(3, BorderStyle.Tcbs_double, Color.Black))
- 合并单元格:
-
文字格式
- 字体样式:
.Font("微软雅黑")、.Color(Color.Red)、.Italic() - 段落对齐:
.Alignment = Alignment.right(右对齐)
- 字体样式:
-
行 / 列动态调整
- 插入行:
table.InsertRow(index) - 删除列:
table.RemoveColumn(index) - 调整行高:
table.Rows[i].Height = 20
- 插入行:
四、注意事项
- 路径权限:确保生成文档的路径有写入权限(如
C:\Temp需先创建)。 - 特殊字符:若内容含换行符,可用
.AppendLine()替代.Append()。 - 兼容性:DocX 生成的是
.docx格式,不支持.doc(旧版 Word)。
通过以上方法,可快速实现 Word 表格的创建与精细化格式化,满足报表、文档生成等常见需求。