1

如何创建现有 MS Word 2010 模板的实例(模板只有 1 个文本框),获取可填充字段并在其中插入字符串?

所有这些都在 c# 中,到目前为止我什至无法打开 .docx 文档。

4

2 回答 2

0

它不是免费的,但我已经使用 Syncfusion(在 WinRT 上一次)完成了这项工作。您可以设置一个书签,然后在 c# 中导航到该书签并替换其内容(字符串、图像、表格等)。

http://www.syncfusion.com/

文档:http ://help.syncfusion.com/winrt (DocIO是您正在搜索的内容)

他们有免费试用版,但也许您可以免费找到类似的东西(更换书签,如果您无法获得可填写的内容)(我没有,但只搜索了 WinRT)

于 2013-08-28T11:12:45.340 回答
0

我能够用这些库做到这一点:

using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.IO;
using Vladimir.Classes_Auxiliares;

这些字段:

public object Template;
public Object oFalse = false;
public Object oTrue = true;
public Object oMissing = System.Reflection.Missing.Value;

还有这些方法。第一个是打开 Word 应用程序(Visible = false),并根据保存的模板创建一个新文档。

public virtual Word.Document CriarDocumento(ETipoDeDocumento tipoDeDocumento, Word.Document doc, Word.Application word)
        {
            Template = tipoDeDocumento.ToString() + ".dotm";
            doc = word.Documents.Add(Template, ref oMissing, ref oMissing, ref oMissing);


            return doc;
        }

下一个方法将查找内容或单词与 Dictionary 键匹配的文本和对象,以使用我希望由 Dictionary 表示的值填充活动文档,其中键与模板中的字符串相同,值是新值我想传递给新文件。

    public virtual void PreencherDocumentoPorObjetoETexto(Dictionary<string, string> listaDeParametros, Word.Document doc)
    {
        foreach (Shape shape in doc.Shapes)
        {
            foreach (string key in listaDeParametros.Keys)
            {
                if (shape.TextFrame.TextRange.Text.Contains(key))
                    shape.TextFrame.TextRange.Text = listaDeParametros[key];
            }
        }

        foreach (string key in listaDeParametros.Keys)
        {
            Word.Find findObject = doc.Application.Selection.Find;
            findObject.ClearFormatting();
            findObject.Text = key;
            findObject.Replacement.ClearFormatting();
            findObject.Replacement.Text = listaDeParametros[key];

            object replaceAll = Word.WdReplace.wdReplaceAll;
            findObject.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
        }

    }

此方法最终将在打印对话框打开的情况下以打印预览状态显示 Word 应用程序。只有选项是打印或取消。

    public virtual void ImprimirDocumento(Word.Application word)
    {
        word.PrintPreview = true;
        word.Visible = true;

        Word.Dialog wordPrintDialog = word.Application.Dialogs[Word.WdWordDialog.wdDialogFilePrint];
        wordPrintDialog.Show(ref oMissing);
    }

每次使用这些方法打开文档时都应调用此方法,它将关闭文档和 Word 应用程序。

    public virtual void FecharDocumento(Word.Document doc, Word.Application word)
    {
        System.Threading.Thread.Sleep(1000);

        doc.Close(oFalse, oMissing, oMissing);
        word.Quit(oFalse, oMissing, oMissing);
        Marshal.FinalReleaseComObject(doc);
        Marshal.FinalReleaseComObject(word);
    }

如果您使用上述方法没有完成Word应用程序,我建议您调用任务管理器并手动完成实例。

于 2013-10-09T15:22:59.477 回答