如何在 C# 中创建 Word 文档?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10412/
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-07-31 17:35:30  来源:igfitidea点击:

How can a Word document be created in C#?

提问by Schmidty

I have a project where I would like to generate a report export in MS Word format. The report will include images/graphs, tables, and text. What is the best way to do this? Third party tools? What are your experiences?

我有一个项目,我想在其中生成 MS Word 格式的报告导出。该报告将包括图像/图表、表格和文本。做这个的最好方式是什么?第三方工具?你有什么经验?

采纳答案by rjzii

The answer is going to depend slightly upon if the application is running on a server or if it is running on the client machine. If you are running on a server then you are going to want to use one of the XML based office generation formats as there are know issues when using Office Automation on a server.

答案将略微取决于应用程序是在服务器上运行还是在客户端计算机上运行。如果您在服务器上运行,那么您将希望使用一种基于 XML 的办公生成格式,因为在服务器上使用办公自动化时存在已知问题。

However, if you are working on the client machine then you have a choice of either using Office Automationor using the Office Open XML format (see links below), which is supported by Microsoft Office 2000 and up either natively or through service packs. One draw back to this though is that you might not be able to embed some kinds of graphs or images that you wish to show.

但是,如果您在客户端计算机上工作,那么您可以选择使用 Office 自动化或使用 Office Open XML 格式(请参阅下面的链接),Microsoft Office 2000 及更高版本支持本机或通过服务包。但是,这样做的一个缺点是您可能无法嵌入您希望显示的某些类型的图形或图像。

The best way to go about things will all depend sightly upon how much time you have to invest in development. If you go the route of Office Automation there are quite a few good tutorials out there that can be found via Google and is fairly simple to learn. However, the Open Office XML format is fairly new so you might find the learning curve to be a bit higher.

处理事情的最佳方式完全取决于您必须在开发上投入多少时间。如果你走办公自动化的路线,有很多很好的教程可以通过谷歌找到,而且很容易学习。但是,Open Office XML 格式相当新,因此您可能会发现学习曲线要​​高一些。

Office Open XML Iinformation

Office Open XML Iinformation

回答by TheSmurf

@Dale Ragan: That will work for the Office 2003 XML format, but that's not portable (as, say, .doc or .docx files would be).

@Dale Ragan:这适用于 Office 2003 XML 格式,但不可移植(例如 .doc 或 .docx 文件)。

To read/write those, you'll need to use the Word Object Library ActiveX control:

要读/写这些,您需要使用 Word 对象库 ActiveX 控件:

http://www.codeproject.com/KB/aspnet/wordapplication.aspx

http://www.codeproject.com/KB/aspnet/wordapplication.aspx

回答by Dale Ragan

@Danny Smurf: Actually this article describes what will become the Office Open XML format which Rob answered with. I will pay more attention to the links I post for now on to make sure there not obsolete. I actually did a search on WordML, which is what it was called at the time.

@Danny Smurf:实际上,本文描述了 Rob 回答的将成为 Office Open XML 格式的内容。我会更加注意我现在发布的链接,以确保没有过时。其实我在WordML上搜了一下,当时是这么叫的。

I believe that the Office Open XML format is the best way to go.

我相信 Office Open XML 格式是最好的方法。

回答by Luke Girvin

Schmidty, if you want to generate Word documents on a web server you will need a licence for each client(not just the web server). See this section in the first linkRob posted:

Schmidty,如果您想在 Web 服务器上生成 Word 文档,您需要为每个客户端(不仅仅是 Web 服务器)提供许可证。在Rob 发布的第一个链接中查看此部分:

"Besides the technical problems, you must also consider licensing issues. Current licensing guidelines prevent Office applications from being used on a server to service client requests, unless those clients themselves have licensed copies of Office. Using server-side Automation to provide Office functionality to unlicensed workstations is not covered by the End User License Agreement (EULA)."

“除了技术问题,您还必须考虑许可问题。当前的许可指南禁止在服务器上使用 Office 应用程序来为客户端请求提供服务,除非这些客户端本身拥有 Office 的许可副本。使用服务器端自动化提供 Office 功能以未经许可的工作站不受最终用户许可协议 (EULA) 的保护。”

If you meet the licensing requirements, I think you will need to use COM Interop - to be specific, the Office XP Primary Interop Assemblies.

如果您满足许可要求,我认为您将需要使用 COM Interop - 具体而言,Office XP Primary Interop Assemblies

回答by DavidWhitney

Have you considered using .RTF as an alternative?

您是否考虑过使用 .RTF 作为替代方案?

It supports embedding images and tables as well as text, opens by default using Microsoft Word and whilst it's featureset is more limited (count out any advanced formatting) for something that looks and feels and opens like a Word document it's not far off.

它支持嵌入图像和表格以及文本,默认情况下使用 Microsoft Word 打开,虽然它的功能集更加有限(包括任何高级格式),但它的外观和感觉和打开方式与 Word 文档相似,但它并不遥远。

Your end users probably won't notice.

您的最终用户可能不会注意到。

回答by John

I have found Aspose Wordsto be the best as not everybody can open Office Open XML/*.docx format files and the Word interop and Word automation can be buggy. Aspose Words supports most document file types from Word 97 upwards.

我发现Aspose Words是最好的,因为不是每个人都可以打开 Office Open XML/*.docx 格式的文件,而且 Word 互操作和 Word 自动化可能有问题。Aspose Words 支持 Word 97 以上的大多数文档文件类型。

It is a pay-for component but has great support. The other alternative as already suggested is RTF.

这是一个付费组件,但有很大的支持。已经建议的另一种选择是 RTF。

回答by DancesWithBamboo

Check out VSTO (Visual Studio Tools for Office). It is fairly simple to create a Word template, inject an xml data island into it, then send it to the client. When the user opens the doc in Word, Word reads the xml and transforms it into WordML and renders it. You will want to look at the ServerDocument class of the VSTO library. No extra licensing is required from my experience.

查看 VSTO(Office 的 Visual Studio 工具)。创建一个 Word 模板,在其中注入一个 xml 数据岛,然后将其发送给客户端是相当简单的。当用户在 Word 中打开文档时,Word 会读取 xml 并将其转换为 WordML 并呈现它。您将需要查看 VSTO 库的 ServerDocument 类。根据我的经验,不需要额外的许可。

回答by littlecharva

I currently do this exact thing.

我目前正在做这件事。

If the document isn't very big, doesn't contain images and such, then I store it as an RTF with #MergeFields# in it and simply replace them with content, sending the result down to the user as an RTF.

如果文档不是很大,不包含图像等,那么我将它存储为带有 #MergeFields# 的 RTF,并简单地用内容替换它们,将结果作为 RTF 发送给用户。

For larger documents, including images and dynamically inserted images, I save the initial Word document as a Single Webpage *.mht file containing the #MergeFields# again. I then do the same as above. Using this, I can easily render a DataTable with some basic Html table tags and replace one of the #MergeFields# with a whole table.

对于较大的文档,包括图像和动态插入的图像,我再次将初始 Word 文档保存为包含 #MergeFields# 的单个网页 *.mht 文件。然后我做和上面一样的。使用它,我可以轻松地使用一些基本的 Html 表标签呈现一个 DataTable,并用整个表替换 #MergeFields# 之一。

Images can be stored on your server and the url embedded into the document too.

图像可以存储在您的服务器上,网址也可以嵌入到文档中。

Interestingly, the new Office 2007 file formats are actually zip files - if you rename the extension to .zip you can open them up and see their contents. This means you should be able to switch content such as images in and out using a simple C# zip library.

有趣的是,新的 Office 2007 文件格式实际上是 zip 文件 - 如果将扩展名重命名为 .zip,则可以打开它们并查看其内容。这意味着您应该能够使用简单的 C# zip 库来切入和切出图像等内容。

回答by littlecharva

I faced this problem and created a small library for this. It was used in several projects and then I decided to publish it. It is free and very very simple but I'm sure it will help with you with the task. Invoke the Office Open XML Library, http://invoke.co.nz/products/docx.aspx.

我遇到了这个问题并为此创建了一个小型库。它被用于几个项目,然后我决定发布它。它是免费的,而且非常简单,但我相信它会帮助您完成任务。调用 Office Open XML 库,http://invoke.co.nz/products/docx.aspx

回答by Jafin

I have had good success using the Syncfusion Backoffice DocIO which supports doc and docx formats.

我使用支持 doc 和 docx 格式的 Syncfusion Backoffice DocIO 取得了很好的成功。

In prior releases it did not support everything in word, but accoriding to your list we tested it with tables and text as a mail merge approach and it worked fine.

在之前的版本中,它不支持 Word 中的所有内容,但根据您的列表,我们使用表格和文本作为邮件合并方法对其进行了测试,并且运行良好。

Not sure about the import of images though. On their blurb page http://www.syncfusion.com/products/DocIO/Backoffice/features/default.aspxit says

虽然不确定图像的导入。在他们的简介页面http://www.syncfusion.com/products/DocIO/Backoffice/features/default.aspx它说

Blockquote Essential DocIO has support for inserting both Scalar and Vector images into the document, in almost all formats. Bitmap, gif, png and tiff are some of the common image types supported.

Blockquote Essential DocIO 支持在文档中插入标量和矢量图像,几乎所有格式。位图、gif、png 和 tiff 是一些支持的常见图像类型。

So its worth considering.

所以值得考虑。

As others have mentioned you can build up a RTF document, there are some good RTF libraries around for .net like http://www.codeproject.com/KB/string/nrtftree.aspx

正如其他人提到的,你可以建立一个 RTF 文档,有一些很好的 .net 的 RTF 库,比如http://www.codeproject.com/KB/string/nrtftree.aspx