4

ラベルを作成する最良の方法は、MicrosoftWordなどの既存の業界標準ツールを使用することです。

これをどのように実行し、出荷ラベルを設定しますか?

マージフィールドをデータグリッドビューの列にマッピングする方法がわかりません。これは私がこれまでに持っているコードです:

// Create a new empty document.
DocumentModel document = new DocumentModel();

// Add document content.
document.Sections.Add(
    new Section(document,
        new Paragraph(document,
            new Field(document, FieldType.MergeField, "FullName"))));

// Save the document to a file and open it with Microsoft Word.
document.Save("TemplateDocument.docx");
// If document appears empty in Microsoft Word, press Alt + F9.
Process.Start("TemplateDocument.docx");

// Initialize mail merge data source.
var dataSource = new { FullName = "John Doe" };

// Execute mail merge.
document.MailMerge.Execute(dataSource);

// Save the document to a file and open it with Microsoft Word.
document.Save("Document.docx");
Process.Start("Document.docx");
4

1 に答える 1

3

まず、ラベルテンプレートドキュメントの作成方法を学ぶ必要があります。たとえば、次のビデオチュートリアルに従ってください:www.youtube.com/watch?v = tIg70utT72Q

テンプレートドキュメントを保存する前に、メールマージデータソースとして.NETオブジェクトを使用するため、メールマージプロセスで作成されたメールマージデータソースを削除します。メールマージデータソースを削除するには、[メーリング]タブ->[メールマージの開始]->画像のように[通常のWord文書]を選択します。 メールマージデータソースを削除する

ドキュメントを「LabelTemplate.docx」などのファイルに保存します。Alt + F9を押すと、次の画像のようなフィールドコードが表示されます。 テンプレートコンテンツにラベルを付ける

これで、 GemBox.Documentコンポーネント(質問で使用するコード)を使用してプログラムによるメールマージを実行する準備が整いました。メールマージを実行するC#コードは次のとおりです。

// Set licensing info.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
ComponentInfo.FreeLimitReached += (sender, e) => e.FreeLimitReachedAction = FreeLimitReachedAction.ContinueAsTrial;

// Create mail merge data source. 
// You should use DataGridView.DataSource property - it will return DataView or DataTable that is data-bound to your DataGridView.
var dataTable = new DataTable()
{
    Columns =
    {
        new DataColumn("Name"),
        new DataColumn("Surname"),
        new DataColumn("Company")
    },
    Rows =
    {
        { "John", "Doe", "ACME" },
        { "Fred", "Nurk", "ACME" },
        { "Hans", "Meier", "ACME" }
    }
};

var document = DocumentModel.Load("LabelTemplate.docx", LoadOptions.DocxDefault);

// Use this if field names and data column names differ. If they are the same (case-insensitive), then you don't need to define mappings explicitly.
document.MailMerge.FieldMappings.Add("First_Name", "Name");
document.MailMerge.FieldMappings.Add("Last_Name", "Surname");
document.MailMerge.FieldMappings.Add("Company_Name", "Company");

// Execute mail merge. Each mail merge field will be replaced with the data from the data source's appropriate row and column.
document.MailMerge.Execute(dataTable);

// Remove any left mail merge field.
document.MailMerge.RemoveMergeFields();

// Save resulting document to a file.
document.Save("Labels.docx");

結果の「Labels.docx」ドキュメントは次のようになります。 ラベルメールのマージ結果

コンポーネントがトライアルモードで使用されるため、プロモーションヘッダーが自動的に追加されます。 GemBox.Documentメールマージは非常に柔軟性があり、階層的なメールマージをサポートし、 FieldMergingイベントを処理するか、マージフィールドのフォーマット文字列を指定することにより、各フィールドのマージをカスタマイズします。

于 2012-12-10T11:37:55.183 に答える