gpt4 book ai didi

c# - 如何将数据添加到合并字段

转载 作者:太空宇宙 更新时间:2023-11-03 11:14:05 25 4
gpt4 key购买 nike

我想通过 Word 模板自动打印发票。我想我的逻辑是错误的,但我只是不知道正确的编码方式。这是我当前的代码

Selection wrdSelection;
MailMerge wrdMailMerge;
MailMergeFields wrdMergeFields;
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
Document wrdDoc = new Document();
wordApp.Visible = false;
wrdSelection = wordApp.Selection;

object oMissing = System.Reflection.Missing.Value;

// PUT MY EXISTING TEMPLATE FILE INTO WORD DOCUMENT
wrdDoc = wordApp.Documents.Add(Properties.Resources.invoiceTemp,oMissing,oMissing, oMissing);
// RETREIVE MAIL MERGE PROPERTIES FROM THE DOCUMENT IN HOPES OF UTILIZING IT

wrdMailMerge = wrdDoc.MailMerge;
wrdDoc.Select();
wrdMergeFields = wrdMailMerge.Fields;

如果有帮助,这里是我的模板中的合并字段:

date_issuedmonth_coveredinvoiceNotuitionlunchFeediscount, studentNo, studentName, amountDue, amountPaid, 余额 >, 惩罚, 状态

现在,如何将使用我的应用程序检索到的数据添加到从模板获取所有属性的文档中?

最佳答案

这里有一些关于使用模板和数据文件运行的注意事项。

Word.Application _wordApp = new Word.Application();
Word.Document oDoc = _wordApp.Documents.Add(@"z:\docs\mergetemplate.dotx");
_wordApp.Visible = true;
oDoc.MailMerge.MainDocumentType = Word.WdMailMergeMainDocType.wdFormLetters;
oDoc.MailMerge.OpenDataSource(@"Z:\Docs\new.csv", false, false, true);
oDoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
oDoc.MailMerge.Execute(false);

新创建的合并文件现在是事件文档,因此您可以保存它:

Word.Document oLetters = _wordApp.ActiveDocument;
oLetters.SaveAs2(@"z:\docs\letters.docx",
Word.WdSaveFormat.wdFormatDocumentDefault);

关于c# - 如何将数据添加到合并字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13188472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com