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

时间:2018-11-12 09:37:58

标签: c# openxml wordprocessingml

创建TableCellProperties并删除TableCellBorders后,单词document变得不可读,我得到:

  

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自动恢复过程之后,该文件就可以了。谁会引起这样的问题?

1 个答案:

答案 0 :(得分:0)

问题解决了!

每个表单元格应包含/以Paragraph对象结尾,因此解决方案是:

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

好像我在this question中遇到了同样的问题,但是没有错误。

相关问题