删除单元格边框后无法读取的Word文档(OpenXml.Wordprocessing)

问题描述 投票:0回答:1

创建一个TableCellProperties并删除TableCellBorders后,word文档变得不可读,我得到:

Word在test.docx中找到了不可读的内容。您想恢复此文档的内容吗?如果您信任此文档的来源,请单击“是”。

我使用的代码:

TableCellProperties cellProp = new TableCellProperties(
    new TableCellBorders(
        new TopBorder()
        {
            Val =
                new EnumValue<BorderValues>(BorderValues.Nil),
        },
        new BottomBorder()
        {
            Val =
                new EnumValue<BorderValues>(BorderValues.Nil),
        },
        new LeftBorder()
        {
            Val =
                new EnumValue<BorderValues>(BorderValues.Nil),

        },
        new RightBorder()
        {
            Val = new EnumValue<BorderValues>(BorderValues.Nil),
        }
    )
);
TableCell tc = new TableCell();


tc.Append(cellProp);

TableRow trTest = new TableRow();
trTest.Append(new TableCell(tc.OuterXml));
trTest.Append(new TableCell(new Paragraph(new Run(new Text("B")))));
trTest.Append(new TableCell(new Paragraph(new Run(new Text("C")))));
trTest.Append(new TableCell(new Paragraph(new Run(new Text("D")))));
trTest.Append(new TableCell(new Paragraph(new Run(new Text("E")))));
trTest.Append(new TableCell(new Paragraph(new Run(new Text("F")))));
t.Append(trTest);

BorderValue设置为Nil,因为None似乎没有移除边界。在MS Word自动恢复过程之后,文件很好。 Whan会引起这样的问题吗?

c# openxml wordprocessingml
1个回答
0
投票

问题解决了!

每个表格单元格应该包含/ end与Paragraph对象,因此解决方案是:

tc.Append(cellProp);
tc.Append(new Paragraph());

看起来我有像this question一样的问题,但没有错误。

© www.soinside.com 2019 - 2024. All rights reserved.