没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
原创|行业资讯|编辑:吉炜炜|2025-01-03 11:38:44.477|阅读 25 次
概述:本文介绍如何使用 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@evget.com
文章转载自:慧都网TX Text Control .NET for WPF 分标准,专业,及企业三个版本,是一套专业的文字处理控件。
TX Text Control ActiveX是一个强大的文字处理组件,为开发者提供一个广泛的文字处理功能。它提供了全面的文本格式,邮件合并功能和文字处理关键性功能,如表格支持,图片,页眉和页脚、页面部分等。
TX Text Control .NET for Windows Forms 是一套功能丰富的文字处理控件。
那么,BarTender 支持哪些数据库类型?又是如何助力企业实现数据驱动的智能打印?今天,我们来全面拆解!
企业级软件测试方案Parasoft SOAtest推出的AI助手采用代理式AI,自动生成API测试场景,使不同水准的测试团队都能轻松实现API测试自动化。此次更新后,测试人员可以借助自然语言指令,结合服务定义文件,高效生成API测试场景。除此之外,AI助手还利用AI代理生成测试数据,并针对数据循环对测试场景进行参数化。
Parasoft Virtualize通过环境可视化、实时监控和智能虚拟化三大核心能力,帮开发团队提前扫清环境障碍,让测试效率大幅提升,从此告别“测试五分钟,排查两小时”的尴尬。
很多客户非常关心MES系统的价格问题。这里我们大概聊一下系统的大概价格是怎么定下来的。
TX Text Control .NET for WPF 分标准,专业,及企业三个版本,是一套专业的文字处理控件。
TX Text Control ActiveXTX Text Control ActiveX是一个强大的文字处理组件,为开发者提供一个广泛的文字处理功能。它提供了全面的文本格式,邮件合并功能和文字处理关键性功能,如表格支持,图片,页眉和页脚、页面部分等。
TX Text Control .NET for Windows FormsTX Text Control .NET for Windows Forms 是一套功能丰富的文字处理控件。
TX Text Control .NET Server for ASP.NET一个将文档处理集成到 Web 应用程序中的文档管理控件。
TX Text Control ActiveX ServerTX Text Control ActiveX Server是一个完全可编程的,用于ASP.NET服务器环境与 Microsoft Internet Explorer的文字处理引擎。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@evget.com
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
慧都科技 版权所有 Copyright 2003-
2025 渝ICP备12000582号-13 渝公网安备
50010702500608号