在C#中操作Word表格时,如何合并或拆分表格单元格?

在 C# 中操作 Word 表格的单元格合并与拆分,需根据使用的库(Microsoft.Office.Interop.WordDocX)调用对应的 API。以下分场景详细说明实现方法:

一、使用 Microsoft.Office.Interop.Word

1. 单元格合并

需先选中要合并的单元格范围,再调用Merge()方法。
示例 1:合并相邻单元格(同一行 / 列)
csharp
 
运行
 
using Microsoft.Office.Interop.Word;

// 假设已创建表格table(3行4列)
// 合并第1行第1列到第1行第4列(合并表头)
Range mergeRange = table.Range(
    StartRow: 1, StartColumn: 1, 
    EndRow: 1, EndColumn: 4
);
mergeRange.Cells.Merge();

// 合并第2行第2列到第3行第2列(纵向合并)
Range verticalMergeRange = table.Range(2, 2, 3, 2);
verticalMergeRange.Cells.Merge();
 
示例 2:合并任意区域单元格
csharp
 
运行
 
 
// 合并第2行第3列到第3行第4列(矩形区域)
Cell startCell = table.Cell(2, 3);
Cell endCell = table.Cell(3, 4);
Range mergeArea = wordApp.ActiveDocument.Range(startCell.Range.Start, endCell.Range.End);
mergeArea.Cells.Merge();
 

2. 单元格拆分

使用Cells.Split()方法,指定拆分后的行数和列数。
csharp
 
运行
// 拆分第1行的合并单元格(拆分为1行4列)
Cell mergedCell = table.Cell(1, 1);
mergedCell.Split(NumRows: 1, NumColumns: 4);

// 拆分纵向合并的单元格(拆分为2行1列)
Cell verticalMergedCell = table.Cell(2, 2);
verticalMergedCell.Split(2, 1);
 

3. 合并后内容处理

合并单元格后,内容默认保留第一个单元格的文本,需手动合并内容:
csharp
 
运行
Range mergeRange = table.Range(1, 1, 1, 4);
string combinedText = "合并后的表头文本";
mergeRange.Cells.Merge();
mergeRange.Text = combinedText; // 设置合并后的文本
mergeRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; // 居中
 

二、使用 DocX 库

1. 单元格合并

DocX 通过MergeCells()方法合并指定范围的单元格,支持行 / 列合并。
示例 1:合并表头(第 1 行所有列)
csharp
 
运行
using DocX;
using System.Drawing;

// 创建表格(4行4列)
Table table = document.AddTable(4, 4);
table.Design = TableDesign.LightGrid;

// 合并第1行第1列到第1行第4列
table.MergeCells(0, 0, 0, 3); // 行索引从0开始,列索引从0开始
table.Rows[0].Cells[0].Paragraphs.First().Append("合并的表头").Alignment = Alignment.center;
 
示例 2:合并矩形区域单元格
csharp
 
运行
// 合并第2行第2列到第3行第3列
table.MergeCells(1, 1, 2, 2);
table.Rows[1].Cells[1].Paragraphs.First().Append("合并区域").Alignment = Alignment.center;
 

2. 单元格拆分

DocX 通过SplitCell()方法拆分单元格,需指定拆分后的行列数。
csharp
 
运行
// 拆分第1行的合并单元格(拆分为1行4列)
table.Rows[0].Cells[0].SplitCell(1, 4);

// 拆分第2行第2列的合并单元格(拆分为2行2列)
table.Rows[1].Cells[1].SplitCell(2, 2);
 

三、关键注意事项

  1. 合并范围要求
    • 合并的单元格必须是连续的矩形区域(非矩形无法合并)。
    • Interop.Word 中,合并后的单元格以第一个单元格的索引标识(如合并第 1 行 1-4 列后,仅table.Cell(1,1)有效)。
  2. 拆分规则
    • 拆分后的单元格总数需与原合并范围一致(如合并了 2x2 的单元格,拆分时需指定 2 行 2 列)。
    • DocX 拆分后需重新填充单元格内容,Interop.Word 拆分后内容默认保留在第一个新单元格。
  3. 格式保留:合并 / 拆分后需重新设置单元格样式(如边框、对齐方式),避免格式错乱。

四、完整示例(Interop.Word)

csharp
 
运行
Application wordApp = new Application();
wordApp.Visible = true;
Document doc = wordApp.Documents.Add();

// 创建表格(4行4列)
Table table = doc.Tables.Add(doc.Range(), 4, 4);
table.Style = "Table Grid";

// 合并表头
Range headerRange = table.Range(1, 1, 1, 4);
headerRange.Cells.Merge();
headerRange.Text = "员工信息表";
headerRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
headerRange.Font.Bold = 1;

// 合并第2行第2列到第3行第2列
Range verticalRange = table.Range(2, 2, 3, 2);
verticalRange.Cells.Merge();
verticalRange.Text = "年龄";
verticalRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

// 拆分表头(还原为1行4列)
// table.Cell(1,1).Split(1,4);

doc.SaveAs2(@"C:\Temp\MergeTable.docx");
doc.Close();
wordApp.Quit();
 

五、完整示例(DocX)

csharp
 
运行
 
using (DocX document = DocX.Create(@"C:\Temp\DocXMergeTable.docx"))
{
    Table table = document.AddTable(4, 4);
    table.Design = TableDesign.LightGridAccent1;

    // 合并表头
    table.MergeCells(0, 0, 0, 3);
    table.Rows[0].Cells[0].Paragraphs.First().Append("员工信息表")
        .Alignment = Alignment.center;

    // 填充数据
    table.Rows[1].Cells[0].Paragraphs.First().Append("姓名");
    table.MergeCells(1, 1, 2, 1); // 合并第2行第2列到第3行第2列
    table.Rows[1].Cells[1].Paragraphs.First().Append("年龄");
    table.Rows[1].Cells[2].Paragraphs.First().Append("部门");
    table.Rows[1].Cells[3].Paragraphs.First().Append("日期");

    table.Rows[2].Cells[0].Paragraphs.First().Append("张三");
    table.Rows[2].Cells[2].Paragraphs.First().Append("研发部");
    table.Rows[2].Cells[3].Paragraphs.First().Append("2023-01-15");

    document.InsertTable(table);
    document.Save();
}
 

总结

  • Interop.Word:通过Range定位合并区域,调用Merge();拆分用Split(),需处理 COM 对象索引。
  • DocX:直接通过MergeCells(startRow, startCol, endRow, endCol)合并,拆分用SplitCell(),更简洁。
合并时确保单元格为连续矩形,拆分时匹配原合并范围的行列数,可避免格式异常。根据项目需求选择库,优先使用 DocX(轻量)或 Interop.Word(全功能)。
 
 
 
posted @ 2025-11-27 10:22  福寿螺888  阅读(39)  评论(0)    收藏  举报