Aspose.Word郵件合併之自定義數據源

来源:https://www.cnblogs.com/liszt/archive/2018/07/30/how-to-custom-data-source-in-aspose-word-for-mail-merge.html
-Advertisement-
Play Games

Aspose.Word在進行郵件合併時,預設的幾個重載方法對Database支持比較友好,但是也可以通過自定義數據源來實現從集合或者對象中返回數據進行郵件合併。 自定義數據源主要是通過實現IMailMergeDataSource介面來實現的。官方的例子如下: 參考文檔: https://apiref ...


Aspose.Word在進行郵件合併時,預設的幾個重載方法對Database支持比較友好,但是也可以通過自定義數據源來實現從集合或者對象中返回數據進行郵件合併。

 

自定義數據源主要是通過實現IMailMergeDataSource介面來實現的。官方的例子如下:

 

[C#]

public void MailMergeCustomDataSource()
{
    // Create some data that we will use in the mail merge.
    CustomerList customers = new CustomerList();
    customers.Add(new Customer("Thomas Hardy", "120 Hanover Sq., London"));
    customers.Add(new Customer("Paolo Accorti", "Via Monte Bianco 34, Torino"));

    // Open the template document.
    Document doc = new Document(MyDir + "MailMerge.CustomDataSource.doc");

    // To be able to mail merge from your own data source, it must be wrapped
    // into an object that implements the IMailMergeDataSource interface.
    CustomerMailMergeDataSource customersDataSource = new CustomerMailMergeDataSource(customers);

    // Now you can pass your data source into Aspose.Words.
    doc.MailMerge.Execute(customersDataSource);

    doc.Save(MyDir + "MailMerge.CustomDataSource Out.doc");
}

/// <summary>
/// An example of a "data entity" class in your application.
/// </summary>
public class Customer
{
    public Customer(string aFullName, string anAddress)
    {
        mFullName = aFullName;
        mAddress = anAddress;
    }

    public string FullName
    {
        get { return mFullName; }
        set { mFullName = value; }
    }

    public string Address
    {
        get { return mAddress; }
        set { mAddress = value; }
    }

    private string mFullName;
    private string mAddress;
}

/// <summary>
/// An example of a typed collection that contains your "data" objects.
/// </summary>
public class CustomerList : ArrayList
{
    public new Customer this[int index]
    {
        get { return (Customer)base[index]; }
        set { base[index] = value; }
    }
}

/// <summary>
/// A custom mail merge data source that you implement to allow Aspose.Words 
/// to mail merge data from your Customer objects into Microsoft Word documents.
/// </summary>
public class CustomerMailMergeDataSource : IMailMergeDataSource
{
    public CustomerMailMergeDataSource(CustomerList customers)
    {
        mCustomers = customers;

        // When the data source is initialized, it must be positioned before the first record.
        mRecordIndex= -1;
    }

    /// <summary>
    /// The name of the data source. Used by Aspose.Words only when executing mail merge with repeatable regions.
    /// </summary>
    public string TableName
    {
        get { return "Customer"; }
    }

    /// <summary>
    /// Aspose.Words calls this method to get a value for every data field.
    /// </summary>
    public bool GetValue(string fieldName, out object fieldValue)
    {
        switch (fieldName)
        {
            case "FullName":
                fieldValue = mCustomers[mRecordIndex].FullName;
                return true;
            case "Address":
                fieldValue = mCustomers[mRecordIndex].Address;
                return true;
            default:
                // A field with this name was not found, 
                // return false to the Aspose.Words mail merge engine.
                fieldValue = null;
                return false;
        }
    }

    /// <summary>
    /// A standard implementation for moving to a next record in a collection.
    /// </summary>
    public bool MoveNext()
    {
        if (!IsEof)
            mRecordIndex++;

        return (!IsEof);
    }

    public IMailMergeDataSource GetChildDataSource(string tableName)
    {
        return null;
    }

    private bool IsEof
    {
        get { return (mRecordIndex >= mCustomers.Count); }
    }

    private readonly CustomerList mCustomers;
    private int mRecordIndex;
}

 

參考文檔:

https://apireference.aspose.com/net/words/aspose.words.mailmerging/imailmergedatasource

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 今天去了回客科技 筆試了一波。很遺憾啊,腦袋有思路 但是還沒到手寫代碼很熟練的程度,基本功不到位。 第一道:線程的題:三個線程 +1 一個線程 -1 運算 。 看到網上還有四個線程的,兩個加法計算,兩個減法運算。基本的思路都是一樣的 ,註意看同步處理。 下麵貼出代碼實現: 這裡引申出來了其他問題的思 ...
  • 對象:java是面向對象的編程,對象就相當於我們生活中的一個具體的事物,比如一個手機,它擁有尺寸,顏色,cpu,記憶體等屬性,還擁有上網,打電話等功能。在java中,對象也擁有數據和方法。 類:類就是一組擁有相同屬性和相同方法的對象的集合。 成員變數和局部變數 成員變數:在類的方法外面,能被類中所有的 ...
  • package demo01; import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date; public class demo27 { public static void main ...
  • 轉載請註明出處:http://www.cnblogs.com/zhiyong-ITNote/ 一直不習慣linq的擴展方法,每次用的時候,賊不順手,尤其是查數據的時候,這不更新個資料庫這麼簡單地需求都搞了一個小時(好吧,也有心不在焉的因素)。總結了一下,代碼如下: private readonly ...
  • 以下為將被序列化的類Entity: [XmlRoot("Root")] public class Entity { [XmlAttribute(AttributeName = "Attr1")] public string Attribute1; [XmlElement("SecondLevel") ...
  • 在進行 WPF 程式打包發佈的時候如果對程式打包沒有特別高的要求,InnoSetup 足以勝任普通的程式打包發佈需求,它支持安裝包加密,安裝包升級安裝,註冊表操作等常規功能,以下腳本示例中有對常見操作進行相關說明。 [TOC] 簡介 Inno Setup用Delphi寫成,其官方網站同時也提供源程式 ...
  • 前一段時間由於項目需要 .net core 在docker下的部署,途中也遇到很多坑,看了各同行的博客覺得多多少少還是有些問題,原本不想寫此篇文章,由於好友最近公司也需要部署,硬是要求,於是花了些時間寫了下來,並實現docker製作鏡像的過程中先編譯.net core 項目包然後形成鏡像。 (一) ...
  • 字元串的不可變性,從字面的意思上理解,這個“不可變”視乎是不成立的。 通過賦值操作我們發現我們可以更改字元串變數的值,這種改變並不能推翻“字元串不可變性”中的不可變。 也就是說字元串變化並不指的是賦值這種變化。 通過字元串類型和值類型在記憶體中的存儲方式對比看看,字元串中的不可變到底指的是什麼? 值類 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...