如何在 Word 2010 中从 SQL 数据库中检索数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9972024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 15:07:37  来源:igfitidea点击:

how to retrieve data from SQL database in Word 2010?

sqlsql-serverdatabasems-word

提问by jorrebor

I would like to populate a Word document with data from our MS SQL database.

我想用我们的 MS SQL 数据库中的数据填充 Word 文档。

Is this possible, and if yes how?

这是可能的,如果是,如何?

回答by Guido Leenders

I have done it various ways in the past. It depends whether the user initiates the action from OUTSIDE of Microsoft Word or from INSIDE Microsoft Word.

我过去曾以各种方式做到过。这取决于用户是从 Microsoft Word 的 OUTSIDE 还是从 Microsoft Word 的 INSIDE 启动操作。

From INSIDEMicrosoft Word, you can use one of the following techniques:

INSIDEMicrosoft Word 中,您可以使用以下技术之一:

  1. Open a template with placeholders and use VBA or VSTO to iterate over them using a data source using copy & paste. Please note that tables are also a crime here. This approach resembles approach 1 below with "OUTSIDE". Disadvantage that it is relatively slow (copy & paste) and that Microsoft Word autocorrect likes to kick in when least needed.
  2. Open a template with placeholders and use VBA or VSTO to parse the XML representation and then replace these. It is faster, but harder to write. Especially since the XML representation can contain XML fragments within the placeholders (such as '&amp;lt;&amp;lt;PUT_<xxx/>IT_HERE&amp;gt;&amp;gt;' and more complex cases). Also, you need to make sure you keep a valid XML document and well balanced.
  1. 打开带有占位符的模板,并使用 VBA 或 VSTO 使用复制和粘贴的数据源对其进行迭代。请注意,桌子在这里也是一种犯罪。这种方法类似于下面带有“OUTSIDE”的方法 1。缺点是它相对较慢(复制和粘贴)并且 Microsoft Word 自动更正喜欢在最不需要时启动。
  2. 打开带有占位符的模板并使用 VBA 或 VSTO 来解析 XML 表示,然后替换它们。它更快,但更难写。特别是因为 XML 表示可以在占位符中包含 XML 片段(例如“ &amp;lt;&amp;lt;PUT_<xxx/>IT_HERE&amp;gt;&amp;gt;”和更复杂的情况)。此外,您需要确保保持有效的 XML 文档并保持良好的平衡。

From OUTSIDEMicrosoft Word (such as web interface) you can use one of the following techniques:

OUTSIDEMicrosoft Word(例如 Web 界面)中,您可以使用以下技术之一:

  1. Store a template somewhere using RTF (which is far easier to process than Word's own structure). Put '&lt;&lt;PLACEHOLDER-FOR-NAME&gt;&gt;' or similar easily recognized texts where you like to replace it. When user requests the Word document, fetch the RTF, fetch the data, replace the placeholders, server RTF to user. RTF has some restrictions, but some advantages. Advantages are: ease of creating new templates and also works with Microsoft Wordpad and other Office packages. Disadvantages are that tables are a real mess to process and that not all Microsoft Word constructs are possible. Repeating rows in a table are even less recommended. High volume can be an issue.
  2. Use a reporting package that happens to also output docx, doc or RTF documents. Write a report. In general perfectly suited for high volume. Less suited if you want the end user to type a major amount of additional text, since reporting packages typically work based on pages instead on a flowing text, where sometimes an explicit or implicit page break is inserted. But if you only need the end user to enter one or two sentences in addition, it is sufficient.
  3. Fat-client. Put the SQL data somewhere. Open Word. Read the data and see further techniques for from INSIDE Microsoft Word.
  1. 使用 RTF 将模板存储在某处(这比 Word 自己的结构更容易处理)。将“ &lt;&lt;PLACEHOLDER-FOR-NAME&gt;&gt;”或类似的易于识别的文本放在您想要替换的位置。当用户请求Word文档时,获取RTF,获取数据,替换占位符,服务器RTF给用户。RTF 有一些限制,但也有一些优点。优点是:易于创建新模板,并且还适用于 Microsoft Wordpad 和其他 Office 包。缺点是表格处理起来非常混乱,而且并非所有的 Microsoft Word 结构都是可能的。更不推荐在表中重复行。高音量可能是一个问题。
  2. 使用恰好也输出 docx、doc 或 RTF 文档的报告包。写报告。一般来说,非常适合高音量。如果您希望最终用户键入大量附加文本,则不太适合,因为报告包通常基于页面而不是流动文本工作,有时会插入显式或隐式分页符。但如果只需要最终用户另外输入一两句话,就足够了。
  3. 胖客户端。将 SQL 数据放在某处。打开字。从 INSIDE Microsoft Word 读取数据并查看更多技术。

If you need to fill a Word document from SQL Server (or any other database or data platform) , I can suggest the free edition of Invantive Composition for filling Word documents from the database(please note that I've been involved with that product). It opens templates and merges them from within Word, but is more targeted at non-developers; just specify the template and datablocks (possibly nested) and publish. Developers can only add some C# in plugins. I think it is a good product when you have MANY templates (over 50) because it scales easier.

如果您需要从 SQL Server(或任何其他数据库或数据平台)填充 Word 文档,我可以建议使用免费版的Invantive Composition 从数据库中填充 Word 文档(请注意,我已经参与过该产品) . 它从 Word 中打开模板并合并它们,但更针对非开发人员;只需指定模板和数据块(可能嵌套)并发布。开发者只能在插件中添加一些C#。我认为当你有很多模板(超过 50 个)时它是一个很好的产品,因为它更容易扩展。

回答by savinson

You may use microsoft query to fetch data from SQL database to your document, this video may be usefull https://vimeo.com/83983247

您可以使用 microsoft query 从 SQL 数据库中获取数据到您的文档中,此视频可能很有用 https://vimeo.com/83983247

回答by anonymous

You could also try using MS-Excel as it binds to XML better than Word does. It's easy too to make Excel produce 'Word' styled output.

您也可以尝试使用 MS-Excel,因为它比 Word 更好地绑定到 XML。让 Excel 生成“Word”样式的输出也很容易。