{IF { MERGEFIELD Count } > “100” “谢谢” “最低订购量为 100 件”}
第一步:新建一个word文档。
Document document = new Document();
第 2 步:为文档添加一个新部分。
Section section = document.AddSection();
第 3 步:为该部分添加一个新段落。
Paragraph paragraph = section.AddParagraph();
第 4 步:定义创建 IF 字段的方法。
CreateIfField(document, paragraph);
第 5 步:定义合并数据。
string[] fieldName = {"Count"};string[] fieldValue = { "2" };
第 6 步:将数据合并到 IF 字段中。
document.MailMerge.Execute(fieldName, fieldValue);
第 7 步:更新文档中的所有字段。
document.IsUpdateFields = true;
第 8 步:将文档保存到文件中。
document.SaveToFile("sample.docx", FileFormat.Docx);
以下 CreateIfField() 方法显示了如何创建 IF 字段,如:
{IF { MERGEFIELD Count } > “100” “Thanks” ” 最低订购量为 100 个单位 “}
static void CreateIfField(Document document, Paragraph paragraph){IfField ifField = new IfField(document);ifField.Type = FieldType.FieldIf;ifField.Code = "IF ";paragraph.Items.Add(ifField);paragraph.AppendField("Count",FieldType.FieldMergeField);paragraph.AppendText(" > ");paragraph.AppendText(""100"" "");paragraph.AppendText(""""Thanks"" "");paragraph.AppendText(""""The minimum order is 100 units"""");IParagraphBase end = document.CreateParagraphItem(ParagraphItemType.FieldMark);(end as FieldMark).Type = FieldMarkType.FieldEnd;paragraph.Items.Add(end);ifField.End = end as FieldMark;}
检查有效截图如下:

完整代码:
using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;using Spire.Doc.Interface;namespace CreatIF{class Program{static void Main(string[] args){Document document = new Document();Section section = document.AddSection();Paragraph paragraph = section.AddParagraph();CreateIfField(document声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!