TX Text Control X10独家揭秘之使用对象作为数据源
文档处理控件TX Text Control即将发布的X10版本,将升级重点还是放到了其比较优势的流式布局报表设计和生成上。慧都获得了来自其开发商Text Control GmbH公司的一手资料,迫不及待的为大家带来还在测试阶段的TX Text Control X10的独家揭秘。
直接使用枚举业务对象作为数据源,来创建合并模板是TX Text Control最常用的需求之一。即将发布的TX Text Control X10使用了一个新方法来实现这一需求,即使用新增的DocumentServer.MailMerge方法和IEnumerable对象。
public void MergeObjects(System.Collections.IEnumerable mergeData);
邮件合并需要将集合中所有对象的公共属性作为表列和子表,才能正常进行处理。IEnumerable的属性作为关系能够自动实例化,并被用于合并块和嵌套合并块。
例如一下数据源的类结构:
以下代码就是上图UML类设计:
public class Invoice { public List<Product> Products { get; set; } public Customer Customer { get; set; } } public class Product { public Product(string Name, Decimal Price) { this.Name = Name; this.Price = Price; } public string Name { get; set; } public Decimal Price { get; set; } }
现在需要一个报表模板,需要合并块Products,合并字段是块中的Name和Price。操作方式如下。
使用以下代码就能实现创建数据源对象,并使用MergeObjects来开始合并。
Invoice invoice = new Invoice(); invoice.Products = new List<Product>(); invoice.Products.Add(new Product("Apple", 3.55m)); invoice.Products.Add(new Product("Banana", 2.4m)); invoice.Products.Add(new Product("Pineapple", 2.99m)); var invoices = new List<Invoice>(); invoices.Add(invoice); mailMerge1.MergeObjects(invoices);