2012-04-18 9 views
1

いくつかの情報とデータベースからの画像を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ループを実行するとき、完全な文書構造だけではなく身体の部分をコピーしようとしているので、それはエラーがスローされます。私に何ができる?

+0

@TravisJこれまでにこのようなことは一度もしていません。だから、どんな助けもありがとう。私は必ずしも解決策を求めているとは限らない。正しい方向にちょうど1点。 –

+0

Visual Studioをお持ちの場合は、まず2007テンプレートプロジェクトの単語を確認してください。 (単語2003のテンプレートプロジェクトもあります)。私はこれが正しい方向のポイントであるべきだと思う –

答えて

0

また、GemBox.Documentコンポーネントをお試しください。

非常にユーザーフレンドリーで効率的な差し込み印刷のサポートがあります。 C# Word mail mergeの資料とcustomize mail mergeのサンプルを参照してください。

1

Visual Studio 2010とOpen XML 2.0 SDKを使用してテンプレートからWord文書を生成するユーティリティWord Doc Generatorを試すことができます。

関連する問題