【问题标题】:OpenXml can't open docx file after vertically merged cells,垂直合并单元格后,OpenXml 无法打开 docx 文件,
【发布时间】:2015-06-02 14:38:03
【问题描述】:
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> 元素

困扰了我好几天,谁能帮忙谢谢

【问题讨论】:

    标签: openxml-sdk


    【解决方案1】:

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

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多