【问题标题】:Word Template with data from Windows Forms - C#带有来自 Windows 窗体的数据的 Word 模板 - C#
【发布时间】:2014-04-26 14:01:24
【问题描述】:

我使用 VS 2012 在 C# 中创建了一个 Word 模板项目。在运行期间,它直接生成一个包含所需详细信息的 Word 文档模板。我只需要先打开一个 Windows 表单并获取用户数据,然后使用这些数据填充相应的模板。

如果可以的话,我需要你关于这个和示例示例的所有想法。

【问题讨论】:

  • 这个问题要么太宽泛,要么要求站外资源。

标签: c# visual-studio-2012 word-template


【解决方案1】:

我得到的印象是您在谈论 Word 邮件合并,使用您的 c# 代码提供合并字段。

使用 interops(MS 提供的特殊 dll)这个词,您可以按照以下方式执行此操作:

Application word = new Word.Application();

word.Documents.Open ( "C:\sometemplate.doc" );
word.ActiveDocument.Select ( );

Range range = word.ActiveDocument.Range ( 0, 0 );


word.ActiveDocument.Fields.Add ( range, -1, "SET field 'value'", true );


word.ActiveDocument.Fields.Update ( );
word.ActiveDocument.Fields.Unlink ( );
word.ActiveDocument.Save ( );
word.ActiveDocument.Close ( );

我建议您关闭文档,然后使用 Process.Start() 再次打开它,因为 Word 互操作在内存处理方面可能是一个大错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 2011-03-15
    • 2020-05-17
    • 1970-01-01
    • 2015-01-02
    相关资源
    最近更新 更多