没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
原创|行业资讯|编辑:吉炜炜|2025-01-03 11:38:44.477|阅读 87 次
概述:本文介绍如何使用 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 是一套功能丰富的文字处理控件。
近日,Stimulsoft 正式发布了2025.3 版本的报表工具,为 .NET 和 .NET Framework 平台带来了重要更新 —— 支持在报表计算过程中以解释模式运行 C# 脚本。这一功能将进一步提升报表的灵活性和扩展性,方便开发者在报表设计与运行中实现更多自定义逻辑。
在 Python 开发中处理 Microsoft Excel 文件,选择一个功能完善、易于使用的 Excel 库都能显著提升效率。Aspose.Cells for Python 兼顾了性能、灵活性与功能丰富度,能够满足不同业务场景下的表格处理需求。它不仅仅是一个读取 Excel 文件的工具,更是一套完整的电子表格处理引擎,已在金融、数据分析、自动化、报表生成等领域广泛应用。
传统的建模工具通常依赖于基于文件的存储方式,这种方式在处理小型项目时可能足够,但在面对大型团队和复杂模型时,往往会暴露出诸多问题,如文件版本冲突、数据一致性难以保证、扩展性受限等。企业级建模工具Sparx EA的DBMS存储库为企业提供了一种基于服务器的存储解决方案,通过将模型数据存储在企业级数据库管理系统(如Oracle和SQL Server)中,有效解决了这些问题。
TestComplete与BitBar的整合方案,成功破解了跨平台自动化测试的核心痛点。通过高效覆盖多技术栈、利用云端真机实现大规模并行测试、无缝融入CI/CD流程,团队得以将测试效率提升,显著加快反馈闭环,最终确保应用在多样化终端上的一致性与可靠性,驱动业务高效发展。
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号