我正在通过重写 CustomXMLPart 文件来更新 word 文档。我基本上遵循了本教程:http: //blogs.msdn.com/b/brian_jones/archive/2009/01/05/taking-advantage-of-bound-content-controls.aspx
private bool _makeDoc()
{
var path = HttpContext.Current.Server.MapPath("~/Classes/Word/template.docx");
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(path, true))
{
//create new XML string
//these values will populate the template word doc
string newXML = "<root>";
newXML += "<name>";
newXML += "name goes here";
newXML += "</name>";
newXML += "<bio>";
newXML += "text" + "more text";
newXML += "</bio>";
newXML += "</root>";
MainDocumentPart mainPart = myDoc.MainDocumentPart;
//delete old xml part
mainPart.DeleteParts<CustomXmlPart>(mainPart.CustomXmlParts);
//add new xml part
CustomXmlPart customXml = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
using(StreamWriter ts = new StreamWriter(customXml.GetStream()))
{
ts.Write(newXML);
}
myDoc.Close();
}
return true;
}
问题是我不知道如何在“文本”和“更多文本”之间添加换行符。我试过 Environment.NewLine,我试过用<w:p><w:r><w:t>
标签包装它。我似乎无法让它生成有效的 docx 文件。
任何帮助,将不胜感激。