原創|行業資訊|編輯:吉煒煒|2025-01-03 11:38:44.477|閱讀 98 次
概述:本文介紹如何使用 C# 中的 TX Text Control MailMerge 合并自計算業務對象。示例展示了如何合并具有總和的產品列表。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
TX Text Control 是一款功能類似于 MS Word 的文字處理控件,包括文檔創建、編輯、打印、郵件合并、格式轉換、拆分合并、導入導出、批量生成等功能。廣泛應用于企業文檔管理,網站內容發布,電子病歷中病案模板創建、病歷書寫、修改歷史、連續打印、病案歸檔等功能的實現。TX Text Control
中的郵件合并類擁有創建動態數據驅動文檔的強大功能。它可自動完成模板與數據合并的過程,以創建個性化或結構化的文檔,例如信函、發票或報告。
合并塊是 MailMerge 類的高級功能,允許您在文檔中創建重復部分,例如:
MailMerge 類允許您使用各種數據源將內容合并到模板中,包括 JSON、XML 或 IEnumerable 對象。在本文中,我們將重點介紹如何使用包含自計算屬性的對象和對象數組。
自我計算的業務對象
自計算業務對象是一種編程設計模式(在規范設計模式中未被正式認可為命名設計模式),其中對象包含屬性和方法,可根據其內部狀態或相關對象自動計算某些派生值(例如總和)。這種方法可確保計算值保持準確和一致,而無需外部邏輯來執行計算。
自計算業務對象通常包括:
由于我們僅使用數據進行合并和提供固定記錄,因此在這種情況下我們不需要事件處理程序。
示例:包含行項目的發票
考慮一個Invoice包含對象列表的類LineItem。該Invoice對象動態計算項目的總成本,該LineItem對象根據數量和單價計算項目的總成本。
public class Invoice { public string Customer { get; set; } public List<LineItem> LineItems { get; set; } public decimal Total => LineItems.Sum(x => x.Total); } public class LineItem { public string Name { get; set; } public int Quantity { get; set; } public decimal Price { get; set; } public decimal Total => Quantity * Price; }
以下屏幕截圖顯示了示例發票模板,其中包含發票客戶的合并字段、行項目的合并塊和總金額:
以下代碼片段顯示如何將Invoice對象合并到模板中:
// Create an instance of the Invoice class and populate it with customer data and line items. Invoice invoice = new Invoice() { Customer = "John Doe", // Set the customer's name. LineItems = new List<LineItem>() // Initialize the list of line items for the invoice. { new LineItem() { Name = "Item 1", Quantity = 2, Price = 10 }, // Add the first line item. new LineItem() { Name = "Item 2", Quantity = 3, Price = 20 }, // Add the second line item. new LineItem() { Name = "Item 3", Quantity = 1, Price = 30 } // Add the third line item. } }; // Create a new instance of ServerTextControl, which will handle the document processing. using (ServerTextControl tx = new ServerTextControl()) { tx.Create(); // Initialize the TextControl instance. // Load the template file into the TextControl instance. tx.Load("template.tx", StreamType.InternalUnicodeFormat); // Create a MailMerge instance and associate it with the TextControl instance. MailMerge mailMerge = new MailMerge() { TextComponent = tx // Set the TextControl instance as the target for the MailMerge. }; // Perform the mail merge using the invoice object to populate the template placeholders. mailMerge.MergeObject(invoice); }
以下屏幕截圖顯示了包含合并數據的結果文檔。紅色圓圈突出顯示了已動態替換為計算值的合并字段:
自計算業務對象在需要動態和實時計算的場景(例如發票、購物車或財務報告)中具有顯著優勢。通過將計算邏輯封裝在對象本身中,它們可確保派生值(例如總計、小計或稅額)始終準確且最新,并即時反映基礎數據中的任何變化。這種方法不僅可以提高整個應用程序的一致性,還可以減少對重復外部邏輯的需求,從而簡化代碼并提高可維護性。
上面的代碼展示了如何合并單個對象,但 MailMerge 也可用于合并對象列表,例如數組或列表。這樣您就可以使用不同的數據創建同一模板的多個實例,例如為多個客戶生成發票或為多個產品生成報告。
以下代碼片段顯示如何使用合并對象Invoice方法將對象列表合并到模板中:
// Create a list of invoices, each containing a customer and a list of line items. List<Invoice> invoices = new List<Invoice>() { // First invoice for customer "ACME Inc." new Invoice() { Customer = "ACME Inc.", // Set the customer name. LineItems = new List<LineItem>() // Define the line items for this invoice. { new LineItem() { Name = "Product 1", Quantity = 2, Price = 100 }, // First line item. new LineItem() { Name = "Product 2", Quantity = 3, Price = 200 } // Second line item. } }, // Second invoice for customer "Sample, LLC" new Invoice() { Customer = "Sample, LLC", // Set the customer name. LineItems = new List<LineItem>() // Define the line items for this invoice. { new LineItem() { Name = "Product 3", Quantity = 1, Price = 300 }, // First line item. new LineItem() { Name = "Product 4", Quantity = 4, Price = 400 } // Second line item. } }, // Third invoice for customer "Test GmbH" new Invoice() { Customer = "Test GmbH", // Set the customer name. LineItems = new List<LineItem>() // Define the line items for this invoice. { new LineItem() { Name = "Product 5", Quantity = 2, Price = 500 }, // First line item. new LineItem() { Name = "Product 6", Quantity = 3, Price = 600 } // Second line item. } } }; // Create a new instance of ServerTextControl for document processing. using (ServerTextControl tx = new ServerTextControl()) { tx.Create(); // Initialize the ServerTextControl instance. // Load the template document that contains placeholders for the mail merge. tx.Load("template.tx", StreamType.InternalUnicodeFormat); // Create a MailMerge instance and associate it with the ServerTextControl instance. MailMerge mailMerge = new MailMerge() { TextComponent = tx // Attach the ServerTextControl as the target for the merge operation. }; // Merge the list of invoice objects into the template document. // This processes all invoices, creating a document for each one based on the template. mailMerge.MergeObjects(invoices); }
生成的文檔將包含多張發票,每張發票都有自己的明細項目和總金額。
結論
自計算業務對象是一種強大的設計模式,它簡化了使用 TX Text Control 的 MailMerge 功能創建動態數據驅動文檔的過程。通過將計算邏輯封裝在對象本身中,您可以確保派生值始終準確且最新,并立即反映基礎數據中的任何變化。這種方法不僅可以提高整個應用程序的一致性,還可以減少對重復外部邏輯的需求,從而簡化代碼并提高可維護性。
產品試用下載、價格咨詢、優惠獲取,或其他任何問題,請聯系。本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網