1

我有一个用 c# 编写的 winForm 应用程序,并且我有一个包含目录中的文件的树视图。它还包含有关每个文件的数据(完整路径、创建时间、大小),如下所示:

这是我的树视图

我正在尝试将此数据导出到 MS-Word 模板,如下所示: 在此处输入图像描述

我的问题是复制每个文件的合并字段并将每个文件属性(文件数更改)插入到位,如下所示:

在此处输入图像描述

这是我的代码:

private void btnExportWord_Click_1(object sender, EventArgs e)
    {
        object oMissing = Missing.Value;
        Word.Application oWord = new Word.Application();
        Word.Document oWordDoc = new Word.Document();
        oWord.Visible = false;
        oWordDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);           
        Object oTemplatePath = @"C:\test\MyXMLTemplate.dotx";         
        oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

        for (int i = 0; i < treeViewXMLFiles.Nodes[0].Nodes.Count; i++)
        {
            string strFilename = treeViewXMLFiles.Nodes[0].Nodes[i].Text;
            string strFull_path = treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[0].Text;
            string strCreationTime = treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[1].Text;
            string strSize = treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[2].Text;

            foreach (Word.Field myMergeField in oWordDoc.Fields)
            {
                Word.Range rngFieldCode = myMergeField.Code;
                String fieldText = rngFieldCode.Text;
                if (fieldText.StartsWith(" MERGEFIELD"))
                {
                    Int32 endMerge = fieldText.IndexOf("\\");
                    Int32 fieldNameLength = fieldText.Length - endMerge;
                    String fieldName = fieldText.Substring(11, endMerge - 11);
                    fieldName = fieldName.Trim();

                    if (fieldName == "File_Name")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText(strFilename);
                    }
                    if (fieldName == "Full_Path")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText(strFull_path);
                    }
                    if (fieldName == "CreationTime")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText(strCreationTime);
                    }
                    if (fieldName == "Size")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText(strSize);
                    }
                }
            }                
        }           
        Object oSaveAsFile = (Object)@"C:\test\FINISHED_XML_Template.doc";            
        oWordDoc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

        oWordDoc.Close(false, ref oMissing, ref oMissing);
        oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
    }

我想找很久没有成功的答案。
我希望这里的任何人都可以帮助我。

4

1 回答 1

1

如果在单个文档中复制合并字段,您应该执行以下操作:

  1. 创建单个目标文档。

  2. 将带有合并字段的模板作为另一个文档加载。

  3. for (int i = 0; i < treeViewXMLFiles.Nodes[0].Nodes.Count; i++)循环中,执行以下操作:

    3.1 选择模板的内容并将其附加到目标文档中。

    3.2 用当前树视图节点中的数据替换(!)目标文档(!)中的合并字段。请注意,您需要替换目标文档中的字段,以便字段消失而只保留“纯”文本。如果您不这样做,for 循环会在后续迭代期间再次绊倒相同的字段,并弄乱先前复制的内容。(您的代码似乎已经正确地进行了字段替换。)


任何应该只在带有合并字段的序列之前或之后出现一次的文本(例如“Hello all”介绍性短语)需要在 for 循环执行之前/之后附加到目标文档。

将这些文本片段保存在单独的模板文件中可以使您的代码相对简单,但您将不得不处理多达三个模板文件,而不仅仅是一个模板文件。但是,在您获得丰富的 C# 和 Word 编程经验之后,您可能会考虑一种方法,允许您只维护一个模板。(有不同的方法可以做到这一点;例如,您可以在模板中使用特定的格式样式来标记要为每个树视图节点复制的内容以及在目标文档中应该只出现一次的内容。)

于 2014-04-17T14:20:47.673 回答