天天看點

TX Text Control文字處理教程(7)郵件合并

郵件合并功能與之前的提到的将TX文檔全部存入資料庫有所不同,郵件合并功能是将資料庫中特定字段插入到模闆文檔的特定位置。與此同時,本章節還會示範在郵件合并功能的基礎上添加列印功能、以及如何建立郵件合并所需的模闆文檔。 

本章節相應的源代碼可以在TX Text Control.NET的安裝目錄中找到: 

        Samples\WinForms\VB.NET\MailMerge 

        Samples\WinForms\CSharp\MailMerge 

<b>第一步:合并資料庫資料和文本</b> 

        啟動程式并選擇Template菜單下的Load指令,通過Load指令加載模闆檔案,模闆檔案中包含一些文本域,這些文本域的資料将被資料庫中對應的資料替換。 

        選擇Database菜單下的Browse Database指令打開【Address Database】窗體,通過該窗體可以從資料庫中選擇資料并合并到文檔中。 

        當點選Merge按鈕時,資料庫中的資料将被拷貝到文檔中相應的文本域中。        你還可以通過重複點選Next和Merge按鈕來合并不同的資料到文檔中。 

        該示例中使用的資料源來自一個XML檔案,檔案中包含一些位址資訊。打開【Address Database】窗體以及每次點選【Previous】和【Next】按鈕都會通過GetRecord()方法來取得資料:

[C#]  

private void GetRecord()  

{  

    DataRow Row = dsAddress.Tables[0].Rows[CurrentRow];  

    lblCompany.Text = Row["company"].ToString();  

    lblRecipient.Text = Row["recipient"].ToString();  

    lblStreet.Text = Row["street"].ToString();  

    lblCity.Text = Row["city"].ToString();  

    lblCountry.Text = Row["country"].ToString();  

    lblSalutation.Text = Row["salutation"].ToString();  

    SetButtonState();  

點選【Merge】按鈕時,資料源中的資料将被拷貝到文檔的相應的文本域中,文本域與資料源中的字段有着相同名字,是以通過For Each操作可以完成這個拷貝工作:

private void cmdMerge_Click(object sender, System.EventArgs e)  

    foreach (TXTextControl.TextField Field in tx.TextFields)  

    {  

        Field.Text = dsAddress.Tables[0].Rows[CurrentRow][Field.Name].ToString();  

    }  

<b>第二步:列印操作</b> 

        在【Address Database】窗體中添加一個【Print】按鈕,當點選【Print】按鈕時,程式會将資料源中的記錄合并到文檔中并進行列印操作:

private void cmdPrint_Click(object sender, System.EventArgs e)  

    PrintDocument PrintDoc = new PrintDocument();  

    foreach (DataRow CurrentRow in dsAddress.Tables[0].Rows)  

        // Merge data from current record  

        foreach (TXTextControl.TextField Field in tx.TextFields)  

            Field.Text = CurrentRow[Field.Name].ToString();  

        // Print  

        PrintDoc.PrinterSettings.FromPage = 0;  

        PrintDoc.PrinterSettings.ToPage = tx.Pages;  

        tx.Print(PrintDoc);  

由于列印操作會自動将資料進行合并,是以不再需要步驟一中的【Merge】按鈕,同時使用Grid來顯示資料源中的資料,這樣可以更好的浏覽資料源中的資料 

<b>第三步:建立模闆文檔</b> 

<b>加載、儲存和導入檔案</b> 

在第一和第二步中程式會自動加載template.tx模闆檔案,是以不能加載其它模闆文檔,也不能儲存文檔。是以需要修改加載檔案相關的代碼,通過一個檔案對話框來選擇需要加載的模闆文檔,同時添加【Saveing】和【Importing】菜單項,需要注意的是隻有将文檔儲存為TX Text Control的格式才能保留文檔中的文本域。而使用者可以通過【SaveFile】對話框來講文檔儲存了其它格式的檔案:

private void mnuFile_SaveTemplateAs_Click(object sender, System.EventArgs e)  

    dlgSaveFile.Filter = "Text Control Files (*.tx)|*.tx";  

    dlgSaveFile.ShowDialog();  

    if (dlgSaveFile.FileName != "")  

        textControl1.Save(dlgSaveFile.FileName,  

        TXTextControl.StreamType.InternalFormat);  

<b>添加資料庫字段</b> 

第一和第二步中包含了必須的文本域,為了建立更靈活的檔案,應用程式應該提供給使用者選擇資料庫字段的功能,由使用者來決定将哪些字段添加到模闆文檔中。在程式中添加一個【Insert】菜單,菜單包含資料源中的所有列: 

然而,每個資料源可能包含不同的列資訊,【Insert】菜單也需要動态建立。資料源中的每列就對應【Insert】菜單的一個菜單項,當點選這些菜單項時,會在文檔中穿件相應的文本域:

private void CreateTextFieldMenu()  

    mnuInsert.MenuItems.Clear();  

    foreach (DataColumn DataField in dsAddress.Tables[0].Columns)  

        mnuInsert.MenuItems.Add(DataField.ColumnName,  

            new EventHandler(InsertMenuItems_Click));  

private void InsertMenuItems_Click(object sender, System.EventArgs e)  

    TXTextControl.TextField textField = new TXTextControl.TextField();  

    textField.Text = "(" + ((MenuItem)sender).Text + ")";  

    textField.Name = ((MenuItem)sender).Text;  

    textField.ShowActivated = true;  

    textField.DoubledInputPosition = true;  

    textControl1.TextFields.Add(textField);  

本文轉自 powertoolsteam 51CTO部落格,原文連結:http://blog.51cto.com/powertoolsteam/773933,如需轉載請自行聯系原作者