【实用技能】如何使用 C# 中的 TX Text Control合并自计算业务对象

原创|行业资讯|编辑:吉炜炜|2025-01-03 11:38:44.477|阅读 25 次

概述:本文介绍如何使用 C# 中的 TX Text Control MailMerge 合并自计算业务对象。示例展示了如何合并具有总和的产品列表。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

相关链接:

TX Text Control 是一款功能类似于 MS Word 的文字处理控件,包括文档创建、编辑、打印、邮件合并、格式转换、拆分合并、导入导出、批量生成等功能。广泛应用于企业文档管理,网站内容发布,电子病历中病案模板创建、病历书写、修改历史、连续打印、病案归档等功能的实现。TX Text Control 

的邮件合并类拥有创建动态数据驱动文档的强大功能。它可自动完成模板与数据合并的过程,以创建个性化或结构化的文档,例如信函、发票或报告。

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 .NET for WPF 分标准,专业,及企业三个版本,是一套专业的文字处理控件。

TX Text Control ActiveX

TX Text Control ActiveX是一个强大的文字处理组件,为开发者提供一个广泛的文字处理功能。它提供了全面的文本格式,邮件合并功能和文字处理关键性功能,如表格支持,图片,页眉和页脚、页面部分等。

TX Text Control .NET for Windows Forms

TX Text Control .NET for Windows Forms 是一套功能丰富的文字处理控件。

TX Text Control .NET Server for ASP.NET

一个将文档处理集成到 Web 应用程序中的文档管理控件。

TX Text Control ActiveX Server

TX Text Control ActiveX Server是一个完全可编程的,用于ASP.NET服务器环境与 Microsoft Internet Explorer的文字处理引擎。

扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP