从 .net 应用程序创建 Word 文档

发布于 2024-10-11 08:55:52 字数 435 浏览 2 评论 0原文

我有一个用 C# 编写的 .net Web 应用程序,仅在 Intranet 上使用。

我有一个页面,需要在其上创建 Word 文档、动态修改一些文本、保存 Word 文档并将其作为电子邮件附件发送。

我尝试使用 Word Automation 来完成此操作,尽管它在我的开发计算机上运行,​​但当其他用户尝试使用它时,它在服务器上不起作用。

那么...如何即时创建 Word 文档、保存并将其用作电子邮件附件?我进行了搜索,人们说“使用 open xml”...但是(除了我不知道如何做到这一点之外)这是否允许我创建并保存一个 Word 文档,该文档将能够由人们使用打开Word 的旧副本(可以追溯到 Word 97)。

我一直在阅读“Sharepoint Server”这个词,但同样,我不知道它与 .net Web 应用程序的配合/方式如何。

有人能指出我正确的方向吗?

I have a .net web application written in C# that is only used on an Intranet.

I have a page on which I need to create a Word document, modify some of the text dynamically, save the Word document and send it as an email attachment.

I tried doing it using Word Automation and, although it worked on my development machine, it doesn't work on a server when other users try to use it.

So ... how to create a Word document on the fly, save it and use it as an email attachment? I have searched and people say 'use open xml' ... but (apart from the fact I have no idea how to do it) would this allow me to create and save a Word document that will be able to be opened by people using old copies of Word (say as far back as Word 97).

And I keep reading the words 'Sharepoint Server' but, again, have no idea what/how this might fit in with a .net web application.

Can anyone point me in the right direction please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

樱花坊 2024-10-18 08:55:52

OpenXML 是目前正确的选择。从头开始创建 Word 文档实际上相当容易,并且有很多教程。

仅仅因为您用 Sharepoint 标记了您的问题,我不确定您是否使用 Sharepoint - 如果是的话,甚至还有一些示例代码关于直接使用 Open XML 内容控件从 SharePoint 生成文档来自微软。

OpenXML is the right way to go right now. Creating a Word document from scratch is actually fairly easy and there are many tutorials.

Just because you tagged your question with Sharepoint I am not sure whether you are using Sharepoint - if so there is even some example code about Generating Documents from SharePoint Using Open XML Content Controls directly from Microsoft.

浮光之海 2024-10-18 08:55:52

代码在您的开发计算机上运行但在服务器上不起作用的最可能原因是您的开发计算机上可能安装了 Office/Word 主互操作程序集,但您没有说明它们是否安装在服务器上。我假设他们不是。

如果您的用户使用的是 Microsoft Office XP (2002) 或 Office 2003并且具有Microsoft Office 兼容包已安装,那么您最好的选择是使用 Open XML SDK 动态生成 .docx 文件,该文件您的用户将能够通过安装兼容包来打开。用户打开文档后,他们可以将其保存为正常的 .doc 格式。

如果您的用户群包括 Office 2000 或更早版本的用户,那么您就不走运了,因为这些版本的 Office 没有兼容包。

如果没有安装 Office 程序集,您将无法在 Web 服务器上生成 .doc 格式,正如其他人指出的那样,Microsoft 强烈建议不要这样做。

The most likely reason the code worked on your development machine but not on the server is because your development machine probably has the Office/Word Primary Interop Assemblies installed on it but you do not say whether these are installed on the server or not. I am assuming they are not.

If your users are on Microsoft Office XP (2002) or Office 2003 AND have the Microsoft Office Compatibility Pack installed, then your best option is to use the Open XML SDK to generate a .docx file on the fly, which your users will be able to open by having the compatibility pack installed. Once your users have opened the document, they can then save it in the normal .doc format.

If your user base includes users of Office 2000 or earlier, you are out of luck as there is no compatibility pack for these versions of Office.

You will not be able to generate the .doc format on the web server without having the Office assemblies installed and, as others have pointed out, this is strongly discouraged by Microsoft.

七秒鱼° 2024-10-18 08:55:52

您考虑过使用 RTF 吗? RTF 文件只是文本,因此您可以使用普通的 .Net 文件 I/O 读取文件,更改您感兴趣的位,然后再次将其写出。 RTF 可以被 97 及以上版本的 Word 读取(也可能是更旧的版本)。一个缺点是,如果您需要对文档进行任何复杂的更改等,如果您只是将 RTF 作为文本处理,这可能会很困难。

Have you considered using RTF? RTF files are just text, so you could use normal .Net file I/O to read in the file, change the bit you are interested in, and then write it out again. RTFs can be read by versions of Word from 97 onwards (and probably older versions too). One drawback is if you need to do any complicated etc changes to the document, which may be difficult if you are just processing the RTF as text.

ι不睡觉的鱼゛ 2024-10-18 08:55:52

其他人评论了为什么自动化这个词可能不起作用,以及为什么它无论如何都是不行的。

OpenXML 很好,但只支持较新的文件格式。

如果您想支持较旧的 .doc 文件格式(Word97 等),那么您将不得不查看另一个组件,并且可能是商业组件,例如(无连接)Aspose 单词

Others have commented on why the word automation probably isn't working and why its a no no anyway.

OpenXML is good but only supports the newer file formats.

If you want to support the older .doc file formats (Word97 etc) then you're going to have to look at another component and probably a commercial one such as (no connection) Aspose Words

私藏温柔 2024-10-18 08:55:52

切勿通过服务使用办公自动化。办公自动化旨在自动化办公桌面应用程序。它假定存在 UI。

它还假设单线程。服务器(尤其是任何类型的 Web 服务器)将在多个线程上运行,而 Office 自动化代码根本无法处理该问题。

此外,您必须为使用调用 Office 自动化的代码的每个最终用户拥有有效的 Office 许可证。

Don't ever use Office Automation from a service. Office Automation was designed to automate the Office desktop applications. It assumes the presence of a UI.

It also assumes single threading. A server (especially a web server of any kind) will be operating on multiple threads, and the Office Automation code just doesn't handle that at all.

Additionally, you must have a valid Office license for every end user who uses the code that calls Office Automation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文