データベースからWord文書にいくつかの情報といくつかの写真を挿入しようとしています。
キーワードを見つけて実際のデータと画像に置き換えるテンプレートとして機能する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);
}
}
しかし、tnlCampersリストでforeachループを実行すると、本文部分だけでなくドキュメント構造全体をコピーしようとしているため、エラーがスローされます。私に何ができる?