垂直合并单元格后,OpenXml无法打开docx文件,

时间:2015-06-02 14:38:03

标签: openxml-sdk

string path = @"D:\newdoc.docx" ;

using (WordprocessingDocument doc = WordprocessingDocument.Create(path, WordprocessingDocumentType.Document)) 
{
    MainDocumentPart mainpart = doc.AddMainDocumentPart();
    mainpart.Document = new Document();
    Body body = mainpart.Document.AppendChild(new Body());

    Table t = new Table();
    TableProperties tpr = new TableProperties(new TableWidth(){Width="0",Type=TableWidthUnitValues.Auto},new TableLook(){Val="04A0"});
    t.Append(tpr);
    TableGrid tg = new TableGrid(new GridColumn() { Width = "4261" }, new GridColumn() { Width = "4261" });
    t.Append(tg);

    TableRow tr1 = new TableRow();
    TableCell r1tc1 = new TableCell();
    TableCell r1tc2 = new TableCell();
    tr1.Append(r1tc1);
    tr1.Append(r1tc2);

    TableRow tr2 = new TableRow();
    TableCell r2tc1 = new TableCell();
    TableCell r2tc2 = new TableCell();
    tr2.Append(r2tc1);
    tr2.Append(r2tc2);

    TableRow tr3 = new TableRow();
    TableCell r3tc1 = new TableCell();                   
    TableCellProperties r3tc1prp = new TableCellProperties();
     VerticalMerge vm = new VerticalMerge() { Val = MergedCellValues.Restart };
    r3tc1prp.Append(vm);              
    r3tc1.Append(r3tc1prp);

    TableCell r3tc2 = new TableCell(new TableCellProperties());
    tr3.Append(r3tc1);
    tr3.Append(r3tc2);

    TableRow tr4 = new TableRow();
    TableCell r4tc1 = new TableCell();
    r4tc1.TableCellProperties = new TableCellProperties(new TableCellWidth() { Width = "4261", Type = TableWidthUnitValues.Dxa });
    r4tc1.TableCellProperties.VerticalMerge = new VerticalMerge();


    TableCell r4tc2 = new TableCell();
    tr4.Append(r4tc1);
    tr4.Append(r4tc2);

    t.Append(tr1);
    t.Append(tr2);
    t.Append(tr3);
    t.Append(tr4);

    body.Append(t);
}

创建并保存文档后,我无法用Word2007打开它

  

错误:<p>元素必须在元素</tc>

之前

我无法在document.xml

中找到<p>元素

困扰我好几天,任何人都可以帮助thxs

1 个答案:

答案 0 :(得分:0)

您在文档中找不到<p>元素的事实实际上是问题所在。架构期望每ParagraphTableCell个元素;没有Paragraph文档无效。这导致了一个相当神秘的错误,你必须在<p>(关闭表格单元格元素)之前有一个</tc>(开放的段落元素)。

为您创建的每个Paragraph添加TableCell即可解决您的问题。最简单的方法是将new Paragraph传递给每个TableCell构造函数:

string path = @"D:\newdoc.docx";

using (WordprocessingDocument doc = WordprocessingDocument.Create(path, WordprocessingDocumentType.Document))
{
    MainDocumentPart mainpart = doc.AddMainDocumentPart();
    mainpart.Document = new Document();
    Body body = mainpart.Document.AppendChild(new Body());

    Table t = new Table();
    TableProperties tpr = new TableProperties(new TableWidth() { Width = "0", Type = TableWidthUnitValues.Auto }, new TableLook() { Val = "04A0" });
    t.Append(tpr);
    TableGrid tg = new TableGrid(new GridColumn() { Width = "4261" }, new GridColumn() { Width = "4261" });
    t.Append(tg);

    TableRow tr1 = new TableRow();
    TableCell r1tc1 = new TableCell(new Paragraph());
    TableCell r1tc2 = new TableCell(new Paragraph());
    tr1.Append(r1tc1);
    tr1.Append(r1tc2);

    TableRow tr2 = new TableRow();
    TableCell r2tc1 = new TableCell(new Paragraph());
    TableCell r2tc2 = new TableCell(new Paragraph());
    tr2.Append(r2tc1);
    tr2.Append(r2tc2);

    TableRow tr3 = new TableRow();
    TableCell r3tc1 = new TableCell(new Paragraph());
    TableCellProperties r3tc1prp = new TableCellProperties();
    VerticalMerge vm = new VerticalMerge() { Val = MergedCellValues.Restart };
    r3tc1prp.Append(vm);
    r3tc1.Append(r3tc1prp);

    TableCell r3tc2 = new TableCell(new Paragraph(), new TableCellProperties());
    tr3.Append(r3tc1);
    tr3.Append(r3tc2);

    TableRow tr4 = new TableRow();
    TableCell r4tc1 = new TableCell(new Paragraph());
    r4tc1.TableCellProperties = new TableCellProperties(new TableCellWidth() { Width = "4261", Type = TableWidthUnitValues.Dxa });
    r4tc1.TableCellProperties.VerticalMerge = new VerticalMerge();


    TableCell r4tc2 = new TableCell(new Paragraph());
    tr4.Append(r4tc1);
    tr4.Append(r4tc2);

    t.Append(tr1);
    t.Append(tr2);
    t.Append(tr3);
    t.Append(tr4);

    body.Append(t);
}