垂直合并单元格后,OpenXml 无法打开 docx 文件,
OpenXml can't open docx file after vertically merged cells,
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打开它
Error:<p>
element must before element </tc>
我在 document.xml
中找不到 <p>
元素
困扰我好几天了,谁能帮帮我谢谢
您在文档中找不到 <p>
元素这一事实实际上就是问题所在。该模式期望每个 TableCell
中有一个 Paragraph
元素;没有 Paragraph
文档无效。这会导致相当神秘的错误,即您必须在 </tc>
(关闭 table 单元格元素)之前有一个 <p>
(开放段落元素)。
为您创建的每个 TableCell
添加一个 Paragraph
将解决您的问题。最简单的方法是将 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);
}
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打开它
Error:
<p>
element must before element</tc>
我在 document.xml
中找不到<p>
元素
困扰我好几天了,谁能帮帮我谢谢
您在文档中找不到 <p>
元素这一事实实际上就是问题所在。该模式期望每个 TableCell
中有一个 Paragraph
元素;没有 Paragraph
文档无效。这会导致相当神秘的错误,即您必须在 </tc>
(关闭 table 单元格元素)之前有一个 <p>
(开放段落元素)。
为您创建的每个 TableCell
添加一个 Paragraph
将解决您的问题。最简单的方法是将 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);
}