简体   繁体   English

打开XML 2.0 Word文档横向合并单元格

[英]Open XML 2.0 Word Document Merge Cell Horizontally

I have a task where I need to merge more than 2 cells, with below code I am able to merge only 2 cells in the table header under word document.我有一个任务,我需要合并超过 2 个单元格,使用下面的代码,我只能合并 word 文档下表 header 中的 2 个单元格。

var tc = new TableCell();
Text header = new Text("");
if (j == 0)
{
    header = new Text("Header1");
    tc.Append(new TableCellProperties(
        new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "1620" },
        new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center }));
}
else if (j == 1)
{
    header = new Text("");
    tc.Append(new TableCellProperties(
        new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "0" }));
}
else if (j == 2)
{
    header = new Text("");
    tc.Append(new TableCellProperties(
       new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "0" }));
}
else if (j == 3)
{
    header = new Text("");
    tc.Append(new TableCellProperties(
       new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "0" }));
}
else if (j == 4)
{
    header = new Text("Header2");
    tc.Append(new TableCellProperties(
        new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "1076" },
        new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center }));
}
else if (j == 5)
{
    header = new Text("Header3");
    tc.Append(new TableCellProperties(
        new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "1004" },
        new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center }));
}

Run runHeaderRun = new Run(); 
runHeaderRun.Append(runHeader);
runHeaderRun.Append(header); 
paraHeader.Append(runHeaderRun);
tc.Append(paraHeader);

if (j == 0 || j == 2)
{
    tc.TableCellProperties = new TableCellProperties();
    tc.TableCellProperties.HorizontalMerge = new HorizontalMerge { Val = MergedCellValues.Restart };
}
else if (j == 1 || j == 3)
{
    tc.TableCellProperties = new TableCellProperties();
    tc.TableCellProperties.HorizontalMerge = new HorizontalMerge { Val = MergedCellValues.Continue };
}
headerRow.Append(tc);

table.Append(headerRow);

I get a result like this:我得到这样的结果:
样本

But I need it like this:但我需要这样:
样本

Although the code you posted does not compile, it looks like the problem is here:虽然您发布的代码无法编译,但看起来问题出在此处:

if (j == 0 || j == 2)
{
    tc.TableCellProperties = new TableCellProperties();
    tc.TableCellProperties.HorizontalMerge = new HorizontalMerge { Val = MergedCellValues.Restart };
}
else if (j == 1 || j == 3)
{
    tc.TableCellProperties = new TableCellProperties();
    tc.TableCellProperties.HorizontalMerge = new HorizontalMerge { Val = MergedCellValues.Continue };
}

I'm assuming that j is a loop index for the columns.我假设j是列的循环索引。 Your code says to start a new horizontal merge on column 0 and continue it to column 1, then restart the merge on column 2 and continue it to column 3. This is exactly what we see in the output image you provided.您的代码说要在第 0 列开始新的水平合并并继续到第 1 列,然后在第 2 列重新开始合并并继续到第 3 列。这正是我们在您提供的 output 图像中看到的内容。 If you want all four cells to be merged, then the first cell should have MergedCellValues.Restart and the other three should have MergedCellValues.Continue .如果要合并所有四个单元格,则第一个单元格应具有MergedCellValues.Restart ,其他三个应具有MergedCellValues.Continue

Also I notice that you are creating a brand new TableCellProperties to set the HorizontalMerge in, rather than adding it to the TableCellProperties you created earlier in your code for those same cells.我还注意到您正在创建一个全新的TableCellProperties来设置HorizontalMerge ntalMerge,而不是将其添加到您之前在代码中为这些相同单元格创建的TableCellProperties So that means that the properties you set earlier, like the TableCellVerticalAlignment , for example, will be lost for those cells.这意味着您之前设置的属性(例如TableCellVerticalAlignment )对于这些单元格将丢失。

I think if you change the above section of code to the following, it will fix both problems:我认为如果您将上述代码部分更改为以下内容,它将解决这两个问题:

if (j < 4)
{
    var merge = j == 0 ? MergedCellValues.Restart : MergedCellValues.Continue;
    tc.TableCellProperties.HorizontalMerge = new HorizontalMerge { Val = merge };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM