翻譯|使用教程|編輯:吉煒煒|2024-12-10 13:51:52.650|閱讀 108 次
概述:本文介紹如何在 .NET C# 中的郵件合并過程中操作表格單元格。可以使用 FieldMerged 事件在合并后操作表格單元格。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
TX Text Control 中的郵件合并 類是一個(gè)強(qiáng)大的庫,旨在通過將數(shù)據(jù)合并到模板中來自動(dòng)創(chuàng)建文檔。它充當(dāng)結(jié)構(gòu)化數(shù)據(jù)(例如來自數(shù)據(jù)庫、JSON 或 XML)和動(dòng)態(tài)文檔生成之間的橋梁,對(duì)于需要自動(dòng)化文檔工作流程的應(yīng)用程序來說非常有用。
從本質(zhì)上講,MailMerge 類簡化了創(chuàng)建專業(yè)、數(shù)據(jù)驅(qū)動(dòng)文檔的復(fù)雜任務(wù),允許開發(fā)人員輕松地將模板中的字段與數(shù)據(jù)源合并。模板是使用 TX Text Control 文字處理界面設(shè)計(jì)的,合并字段代表動(dòng)態(tài)內(nèi)容。
合并區(qū)塊
合并塊是 TX Text Control 的 MailMerge 類中的一個(gè)關(guān)鍵概念,它允許在文檔中動(dòng)態(tài)生成結(jié)構(gòu)化、可重復(fù)的內(nèi)容。合并塊允許開發(fā)人員有效地處理模板中的重復(fù)數(shù)據(jù)結(jié)構(gòu),例如列表、表格或嵌套區(qū)域。
從高層次上講,合并塊是模板中定義的部分,對(duì)應(yīng)于數(shù)據(jù)集合或數(shù)據(jù)表。在合并過程中,MailMerge 會(huì)遍歷數(shù)據(jù)源,動(dòng)態(tài)創(chuàng)建每條記錄的內(nèi)容。這些合并塊可以使用 MailMerge 類的現(xiàn)成功能進(jìn)行有條件的呈現(xiàn)、過濾和排序。
代碼級(jí)操作
但是 MailMerge 類還允許在合并過程中進(jìn)行非常詳細(xì)的代碼級(jí)操作。這包括處理合并事件、自定義合并過程以及以編程方式控制合并操作的輸出。這種級(jí)別的控制對(duì)于需要對(duì)合并過程進(jìn)行細(xì)粒度控制的復(fù)雜文檔生成場(chǎng)景至關(guān)重要。
本文介紹如何使用“字段合并” 事件來操作合并字段位于表格單元格內(nèi)時(shí)由事件返回的表格單元格。在本例中,我們將使用一個(gè)非常簡單的模板,該模板由兩個(gè)合并字段和一個(gè)用紅色突出顯示的簡單重復(fù)塊組成。
TX 文本控件中的郵件合并模板
為了合并模板,我們將使用以下簡化的 JSON 數(shù)據(jù)。
[ { "invoice-id": "1", "invoice-date": "2019-01-01", "line-items": [ { "product-id": "1", "quantity": "1", "unit-price": "8" }, { "product-id": "2", "quantity": "2", "unit-price": "200" }, { "product-id": "3", "quantity": "1", "unit-price": "100" }, { "product-id": "4", "quantity": "1", "unit-price": "3" } ] } ]
為了合并此模板,將使用以下代碼:
textControl1.Load("template.tx", TXTextControl.StreamType.InternalUnicodeFormat); MailMerge mailMerge = new MailMerge(); mailMerge.TextComponent = textControl1; string jsonData = File.ReadAllText("data.json"); mailMerge.MergeJsonData(jsonData);
合并后的文檔結(jié)果如下:
TX 文本控件中的合并文檔
FieldMerged 事件
現(xiàn)在讓我們附加 FieldMerged 事件來操作合并過程。
textControl1.Load("template.tx", TXTextControl.StreamType.InternalUnicodeFormat); MailMerge mailMerge = new MailMerge(); mailMerge.TextComponent = textControl1; mailMerge.FieldMerged += MailMerge_FieldMerged; string jsonData = File.ReadAllText("data.json"); mailMerge.MergeJsonData(jsonData);
每次合并字段時(shí)(成功或失敗),都會(huì)調(diào)用此事件。如果字段位于表格單元格內(nèi),則返回表格單元格。
在此事件中,我們檢查TableCell屬性是否不為空。如果不為空,我們將設(shè)置單元格的背景顏色。
private void MailMerge_FieldMerged(object sender, MailMerge.FieldMergedEventArgs e) { if (e.TableCell != null) { e.TableCell.CellFormat.BackColor = Color.Red; e.TableCell.CellFormat.BottomBorderWidth = 60; e.TableCell.CellFormat.BottomBorderColor = Color.Blue; } }
因此,合并過程中所有合并的單元格都會(huì)被著色。
TX 文本控件中帶有彩色單元格的合并文檔
動(dòng)態(tài)單元格顏色
讓我們通過添加一些邏輯使這個(gè)過程動(dòng)態(tài)化。CellFilterInstructions類用于將一個(gè)值與給定值進(jìn)行比較以返回特定的顏色。
using System; using System.Drawing; using System.Linq; public class CellFilterInstructions { public double? CompareValue { get; set; } = null; public RelationalOperator? Operator { get; set; } = null; public Color TrueColor { get; set; } = Color.White; public Color FalseColor { get; set; } = Color.White; public enum RelationalOperator { Equals = 0, NotEqual, LessThan, GreaterThan, } // evaluates the instruction and returns the proper color public Color? GetColor(string value) { if (Double.TryParse(ParseToNumber(value), out double dValue) == true) { switch (Operator) { case RelationalOperator.Equals: return (dValue == CompareValue ? TrueColor : FalseColor); case RelationalOperator.NotEqual: return (dValue != CompareValue ? TrueColor : FalseColor); case RelationalOperator.GreaterThan: return (dValue > CompareValue ? TrueColor : FalseColor); case RelationalOperator.LessThan: return (dValue < CompareValue ? TrueColor : FalseColor); default: return null; } } else return null; } private string ParseToNumber(string text) { var numericChars = "0123456789,.".ToCharArray(); return new String(text.Where(c => numericChars.Any(n => n == c)).ToArray()); } }
以下代碼創(chuàng)建了一條新規(guī)則,如果值大于 10,則返回綠色,如果值小于 10,則返回紅色。此規(guī)則被序列化并存儲(chǔ)在表單元格的Name屬性中。
textControl1.Load("template.tx", TXTextControl.StreamType.InternalUnicodeFormat); CellFilterInstructions cellFilterInstructions = new CellFilterInstructions() { CompareValue = 10, Operator = CellFilterInstructions.RelationalOperator.GreaterThan, TrueColor = Color.Green, FalseColor = Color.Red }; textControl1.Tables[1].Cells[2,2].Name = JsonConvert.SerializeObject(cellFilterInstructions); MailMerge mailMerge = new MailMerge(); mailMerge.TextComponent = textControl1; mailMerge.FieldMerged += MailMerge_FieldMerged; string jsonData = File.ReadAllText("data.json"); mailMerge.MergeJsonData(jsonData);
在FieldMerged事件中,此規(guī)則被放棄并評(píng)估。然后,返回的顏色將應(yīng)用于表格單元格的表格單元格格式。
private void MailMerge_FieldMerged(object sender, MailMerge.FieldMergedEventArgs e) { // custom field handling if (e.TableCell == null) return; if (e.TableCell.Name != "") { CellFilterInstructions instructions = (CellFilterInstructions)JsonConvert.DeserializeObject( e.TableCell.Name, typeof(CellFilterInstructions)); // retrieve the color Color? color = instructions.GetColor(e.MailMergeFieldAdapter.ApplicationField.Text); // apply the color if (color != null) e.TableCell.CellFormat.BackColor = (Color)color; } }
以下屏幕截圖顯示了此合并過程的結(jié)果:
TX 文本控件中帶有條件單元格顏色的合并文檔
結(jié)論
TX Text Control 中的 MailMerge 類為自動(dòng)化文檔生成過程提供了強(qiáng)大而靈活的解決方案。使用合并塊和代碼級(jí)操作,開發(fā)人員可以輕松創(chuàng)建動(dòng)態(tài)、數(shù)據(jù)驅(qū)動(dòng)的文檔。FieldMerged 事件對(duì)合并過程提供了細(xì)粒度的控制,允許開發(fā)人員根據(jù)特定條件自定義輸出。這種級(jí)別的控制對(duì)于需要精確處理數(shù)據(jù)和內(nèi)容的復(fù)雜文檔生成場(chǎng)景至關(guān)重要。
產(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@ke049m.cn
文章轉(zhuǎn)載自:慧都網(wǎng)