1、

 1 using System;
 2 using DocumentFormat.OpenXml;
 3 using DocumentFormat.OpenXml.Packaging;
 4 using DocumentFormat.OpenXml.Wordprocessing;
 5 
 6 namespace TableCellPropertiesEx
 7 {
 8     class Program
 9     {
10         // Insert a table into an existing word processing document.  
11         static void Main(string[] args)
12         {
13             string fileName = @"D:\12.docx";
14             using (WordprocessingDocument document
15                 = WordprocessingDocument.Open(fileName, true))
16             {
17                 // Create an empty table.  
18                 Table table = new Table();
19 
20                 // Create a TableProperties object and specify its border information.  
21                 TableProperties tblProp = new TableProperties(
22                     new TableBorders(new TopBorder()
23                     { Val = new EnumValue<BorderValues>(BorderValues.Sawtooth), Size = 24 },
24                         new BottomBorder()
25                         { Val = new EnumValue<BorderValues>(BorderValues.Sawtooth), Size = 24 },
26                         new LeftBorder()
27                         { Val = new EnumValue<BorderValues>(BorderValues.Sawtooth), Size = 24 },
28                         new RightBorder()
29                         { Val = new EnumValue<BorderValues>(BorderValues.Sawtooth), Size = 24 },
30                         new InsideHorizontalBorder()
31                         { Val = new EnumValue<BorderValues>(BorderValues.Sawtooth), Size = 24 },
32                         new InsideVerticalBorder()
33                         { Val = new EnumValue<BorderValues>(BorderValues.Sawtooth), Size = 24 })
34                 );
35 
36                 table.AppendChild<TableProperties>(tblProp);
37 
38                 for (int i = 0; i < 2; i++)
39                 {
40                     TableRow tableRow = new TableRow();
41                     for (int j = 0; j < 3; j++)
42                     {
43                         TableCell tableCell = new TableCell();
44                         TableCellProperties properties = new TableCellProperties();
45                         properties.Append(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" });
46                         tableCell.Append(new Paragraph(new Run(new Text($"i={i},j={j}"))));
47                         if (i == 0 && j == 0)
48                         {
49                             HorizontalMerge horizontalMerge = new HorizontalMerge();
50                             horizontalMerge.Val = MergedCellValues.Restart;
51                             VerticalMerge verticalMerge = new VerticalMerge();
52                             verticalMerge.Val = MergedCellValues.Restart;
53                             properties.Append(horizontalMerge);
54                             properties.Append(verticalMerge);
55 
56                         }
57                         else if(i ==0 && j > 0)
58                         {
59                             HorizontalMerge horizontalMerge = new HorizontalMerge();
60                             horizontalMerge.Val = MergedCellValues.Continue;
61                             properties.Append(horizontalMerge);
62                           
63                         }
64                         else if (i > 0 && j == 0)
65                         {
66                             VerticalMerge verticalMerge = new VerticalMerge();
67                             verticalMerge.Val = MergedCellValues.Continue;
68                             
69                             properties.Append(verticalMerge);
70                             HorizontalMerge horizontalMerge = new HorizontalMerge();
71                             horizontalMerge.Val = MergedCellValues.Restart;
72 
73                             properties.Append(horizontalMerge);
74                         }
75                         else if (i > 0 && j > 0)
76                         {
77                             HorizontalMerge horizontalMerge = new HorizontalMerge();
78                             horizontalMerge.Val = MergedCellValues.Continue;
79                             VerticalMerge verticalMerge = new VerticalMerge();
80                             verticalMerge.Val = MergedCellValues.Continue;
81                             properties.Append(horizontalMerge);
82                             properties.Append(verticalMerge);
83                         }
84                         tableCell.Append(properties);
85                         tableRow.Append(tableCell);
86                     }
87                     table.Append(tableRow);
88                 }                
89                 // Append the table to the document.  
90                 document.MainDocumentPart.Document.Body.Append(table);
91             }
92         }
93     }
94 }