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
答案 0 :(得分:0)
您在文档中找不到<p>
元素的事实实际上是问题所在。架构期望每Paragraph
个TableCell
个元素;没有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);
}