1

データベースから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ループを実行すると、本文部分だけでなくドキュメント構造全体をコピーしようとしているため、エラーがスローされます。私に何ができる?

4

2 に答える 2

1

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

于 2012-04-19T08:42:17.227 に答える
0

GemBox.Documentコンポーネントを試すこともできます。

差し込み印刷の非常にユーザー フレンドリーで効率的なサポートを備えています。C# Word 差し込み印刷の記事を参照して、差し込み印刷のサンプルをカスタマイズてください。

于 2012-04-19T08:15:23.357 に答える