合并文档我试图插入一些信息和一些图片从数据库到Word文档。与OpenXML的2.0
我创建了一个Word文档作为,我想找到并与实际数据和图像替换关键字的模板。
如何打开这个模板,更换关键词,并把它变成新的文档。然后,我需要重新打开该模板并重新执行此操作,直到列表中的每个人都在新的Word文档中。
//The template file exists, so open it and use it to set up the main Word document
using (WordprocessingDocument templatePackage = WordprocessingDocument.Open(templateDocPath, false))
{
//Read the template file
string docText = null;
using (StreamReader sr = new StreamReader(templatePackage.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
string tempString = docText;
Regex regexText = new Regex("<name>");
docText = regexText.Replace(docText, tnlCampers[0].FirstName + " " + tnlCampers[0].LastName);
regexText = new Regex("<address>");
docText = regexText.Replace(docText, tnlCampers[0].Address1 + " " + tnlCampers[0].Address2 + " " + tnlCampers[0].City + ", " + tnlCampers[0].State + " " + tnlCampers[0].ZipCode);
regexText = new Regex("<phone>");
docText = regexText.Replace(docText, tnlCampers[0].CellPhone);
regexText = new Regex("<email>");
docText = regexText.Replace(docText, tnlCampers[0].Email);
//Write to the newly created Word Document
using (StreamWriter sw = new StreamWriter(package.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(docText);
}
}
但是当我做一个foreach循环我tnlCampers名单上,因为它试图复制,而不是仅仅的身体部位的完整的文档结构抛出一个错误。我能做什么?
@TravisJ我从来没有真正做过这样的事。所以,任何帮助表示赞赏。我不一定要求解决方案。只是在正确的方向点。 – 2012-04-18 19:00:53
如果你有视觉工作室,首先看看他们的单词2007模板项目。 (还有一个单词2003模板项目)。我认为这应该是一个'正确的方向' – 2012-04-18 19:21:43