いくつかの情報とデータベースからの画像を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);
}
}
しかし、私は私のtnlCampersリスト上のforeachループを実行するとき、完全な文書構造だけではなく身体の部分をコピーしようとしているので、それはエラーがスローされます。私に何ができる?
@TravisJこれまでにこのようなことは一度もしていません。だから、どんな助けもありがとう。私は必ずしも解決策を求めているとは限らない。正しい方向にちょうど1点。 –
Visual Studioをお持ちの場合は、まず2007テンプレートプロジェクトの単語を確認してください。 (単語2003のテンプレートプロジェクトもあります)。私はこれが正しい方向のポイントであるべきだと思う –