原創(chuàng)|行業(yè)資訊|編輯:吉煒煒|2025-01-03 11:38:44.477|閱讀 98 次
概述:本文介紹如何使用 C# 中的 TX Text Control MailMerge 合并自計(jì)算業(yè)務(wù)對(duì)象。示例展示了如何合并具有總和的產(chǎn)品列表。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
TX Text Control 是一款功能類似于 MS Word 的文字處理控件,包括文檔創(chuàng)建、編輯、打印、郵件合并、格式轉(zhuǎn)換、拆分合并、導(dǎo)入導(dǎo)出、批量生成等功能。廣泛應(yīng)用于企業(yè)文檔管理,網(wǎng)站內(nèi)容發(fā)布,電子病歷中病案模板創(chuàng)建、病歷書寫、修改歷史、連續(xù)打印、病案歸檔等功能的實(shí)現(xiàn)。TX Text Control
中的郵件合并類擁有創(chuàng)建動(dòng)態(tài)數(shù)據(jù)驅(qū)動(dòng)文檔的強(qiáng)大功能。它可自動(dòng)完成模板與數(shù)據(jù)合并的過程,以創(chuàng)建個(gè)性化或結(jié)構(gòu)化的文檔,例如信函、發(fā)票或報(bào)告。
合并塊是 MailMerge 類的高級(jí)功能,允許您在文檔中創(chuàng)建重復(fù)部分,例如:
MailMerge 類允許您使用各種數(shù)據(jù)源將內(nèi)容合并到模板中,包括 JSON、XML 或 IEnumerable 對(duì)象。在本文中,我們將重點(diǎn)介紹如何使用包含自計(jì)算屬性的對(duì)象和對(duì)象數(shù)組。
自我計(jì)算的業(yè)務(wù)對(duì)象
自計(jì)算業(yè)務(wù)對(duì)象是一種編程設(shè)計(jì)模式(在規(guī)范設(shè)計(jì)模式中未被正式認(rèn)可為命名設(shè)計(jì)模式),其中對(duì)象包含屬性和方法,可根據(jù)其內(nèi)部狀態(tài)或相關(guān)對(duì)象自動(dòng)計(jì)算某些派生值(例如總和)。這種方法可確保計(jì)算值保持準(zhǔn)確和一致,而無需外部邏輯來執(zhí)行計(jì)算。
自計(jì)算業(yè)務(wù)對(duì)象通常包括:
由于我們僅使用數(shù)據(jù)進(jìn)行合并和提供固定記錄,因此在這種情況下我們不需要事件處理程序。
示例:包含行項(xiàng)目的發(fā)票
考慮一個(gè)Invoice包含對(duì)象列表的類LineItem。該Invoice對(duì)象動(dòng)態(tài)計(jì)算項(xiàng)目的總成本,該LineItem對(duì)象根據(jù)數(shù)量和單價(jià)計(jì)算項(xiàng)目的總成本。
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; }
以下屏幕截圖顯示了示例發(fā)票模板,其中包含發(fā)票客戶的合并字段、行項(xiàng)目的合并塊和總金額:
以下代碼片段顯示如何將Invoice對(duì)象合并到模板中:
// 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); }
以下屏幕截圖顯示了包含合并數(shù)據(jù)的結(jié)果文檔。紅色圓圈突出顯示了已動(dòng)態(tài)替換為計(jì)算值的合并字段:
自計(jì)算業(yè)務(wù)對(duì)象在需要?jiǎng)討B(tài)和實(shí)時(shí)計(jì)算的場景(例如發(fā)票、購物車或財(cái)務(wù)報(bào)告)中具有顯著優(yōu)勢。通過將計(jì)算邏輯封裝在對(duì)象本身中,它們可確保派生值(例如總計(jì)、小計(jì)或稅額)始終準(zhǔn)確且最新,并即時(shí)反映基礎(chǔ)數(shù)據(jù)中的任何變化。這種方法不僅可以提高整個(gè)應(yīng)用程序的一致性,還可以減少對(duì)重復(fù)外部邏輯的需求,從而簡化代碼并提高可維護(hù)性。
上面的代碼展示了如何合并單個(gè)對(duì)象,但 MailMerge 也可用于合并對(duì)象列表,例如數(shù)組或列表。這樣您就可以使用不同的數(shù)據(jù)創(chuàng)建同一模板的多個(gè)實(shí)例,例如為多個(gè)客戶生成發(fā)票或?yàn)槎鄠€(gè)產(chǎn)品生成報(bào)告。
以下代碼片段顯示如何使用合并對(duì)象Invoice方法將對(duì)象列表合并到模板中:
// 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); }
生成的文檔將包含多張發(fā)票,每張發(fā)票都有自己的明細(xì)項(xiàng)目和總金額。
結(jié)論
自計(jì)算業(yè)務(wù)對(duì)象是一種強(qiáng)大的設(shè)計(jì)模式,它簡化了使用 TX Text Control 的 MailMerge 功能創(chuàng)建動(dòng)態(tài)數(shù)據(jù)驅(qū)動(dòng)文檔的過程。通過將計(jì)算邏輯封裝在對(duì)象本身中,您可以確保派生值始終準(zhǔn)確且最新,并立即反映基礎(chǔ)數(shù)據(jù)中的任何變化。這種方法不僅可以提高整個(gè)應(yīng)用程序的一致性,還可以減少對(duì)重復(fù)外部邏輯的需求,從而簡化代碼并提高可維護(hù)性。
產(chǎn)品試用下載、價(jià)格咨詢、優(yōu)惠獲取,或其他任何問題,請(qǐng)聯(lián)系。本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)