[英]OpenXml, Word and C#
这是我的问题:我正在尝试使用OpenXml和C#执行邮件合并。 为此,我必须创建一个Word文档,该文档的页数与数据集中的行数相同(我的数据集是CSV文件 )。
我创建了一个新文档,并且试图将我的模板复制到该文档的每一页上。 不幸的是,只有第一页具有正确的格式(我的模板已复制在上面)。 要进行复制,我使用要从模板复制到新Word文档每一页的InnerXml
。
我知道我的问题来自于我的指示,但我不知道如何解决。
这是我的代码:
string fileName = @"MyTemplate";
using (WordprocessingDocument pkgDoc = WordprocessingDocument.Open(fileName, true))
{
pkgDoc.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
var test = (pkgDoc.MainDocumentPart.RootElement.InnerXml);
string filenamecible = @"@MyNewWordDocument";
using (WordprocessingDocument package = WordprocessingDocument.Create(filenamecible, WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = package.AddMainDocumentPart();
//Create DOM tree for simple document.
mainPart.Document = new Document();
for (int i = 0; i < csvline.Count; i++)
{
mainPart.RootElement.InnerXml = test;
var x = new Break() { Type = BreakValues.Page };;
mainPart.Document.Append(x);
mainPart.Document.Save();
}
package.MainDocumentPart.Document.Save();
}
}
好吧,如果有人遇到相同的问题,我会找到解决方案,代码是:
string fileName = @"MyTemplate";
using (WordprocessingDocument pkgDoc = WordprocessingDocument.Open(fileName, true))
{
pkgDoc.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
var test = (pkgDoc.MainDocumentPart.RootElement.InnerXml);
string filenamecible = @"@MyNewWordDocument";
using (WordprocessingDocument package = WordprocessingDocument.Create(filenamecible, WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = package.AddMainDocumentPart();
//Create DOM tree for simple document.
mainPart.Document = new Document();
for (int i = 0; i < csvline.Count; i++)
{
var x = new Break() { Type = BreakValues.Page };
Body b = new Body(test);
mainPart.Document.Append(b);
mainPart.Document.Append(x);
mainPart.Document.Save();
}
package.MainDocumentPart.Document.Save();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.