【发布时间】: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