【问题标题】:How to fill a fillable word 2010 document with c#如何用c#填充可填写的word 2010文档
【发布时间】:2013-10-09 23:22:59
【问题描述】:

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

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

【问题讨论】:

    标签: c# ms-word


    【解决方案1】:

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

    http://www.syncfusion.com/

    docu:http://help.syncfusion.com/winrtDocIO 是您要搜索的内容)

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

    【讨论】:

    • 谢谢,但我正在寻找免费的解决方案。
    • 或者您可以使用 .net 中内置的东西。适用于 Office 的 Visual Studio 工具,应该可以帮助您入门
    • 哦,是的,我完全忘记了。内置工具不适用于 RT,这就是为什么我不得不搜索其他东西。
    【解决方案2】:

    我可以通过这些库做到这一点:

    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;
            }
    

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

        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应用程序,我建议您调用任务管理器并手动完成实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 1970-01-01
      • 2022-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多