如何用C#创建Word文档?

发布于 2024-07-05 03:01:09 字数 1454 浏览 8 评论 0原文

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

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

发布评论

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

评论(17

兔小萌 2024-07-12 03:01:09

答案将稍微取决于应用程序是在服务器上运行还是在客户端计算机上运行。 如果您在服务器上运行,那么您将需要使用一种基于 XML 的 Office 生成格式,因为当 在服务器上使用 Office Automation

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

处理事情的最佳方式完全取决于您在开发上投入的时间。 如果您选择办公自动化,可以通过 Google 找到很多很好的教程,而且学习起来相当简单。 然而,Open Office XML 格式相当新,因此您可能会发现学习曲线有点高。

Office Open XML I信息

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.

However, if you are working on the client machine then you have a choice of either using Office Automation or 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.

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.

Office Open XML Iinformation

你怎么这么可爱啊 2024-07-12 03:01:09

DocX 免费库,用于创建 DocX 文档,积极开发,使用起来非常简单直观。 由于 CodePlex 即将消亡,项目已转移到 github

DocX free library for creating DocX documents, actively developed and very easy and intuitive to use. Since CodePlex is dying, project has moved to github.

零度° 2024-07-12 03:01:09

我花了大约一周的时间来快速了解 Office Open XML。 我们有一个数据库应用程序,用于存储我们想要在 Microsoft Word 中报告的调查数据。 实际上,您可以使用 C# 从头开始​​创建 Word 2007 (docx) 文件。 Open XML SDK 版本 2 包括一个名为 Document Reflector 的很酷的应用程序,它实际上提供 C# 代码来完全重新创建 Word 文档。 您可以使用部分或全部代码,并替换您想要即时更改的位。 SDK 附带的帮助文件也有一些很好的代码示例。

服务器上不需要 Office Interop 或任何其他 Office 软件 - 新格式是 100% XML。

I have spent the last week or so getting up to speed on Office Open XML. We have a database application that stores survey data that we want to report in Microsoft Word. You can actually create Word 2007 (docx) files from scratch in C#. The Open XML SDK version 2 includes a cool application called the Document Reflector that will actually provide the C# code to fully recreate a Word document. You can use parts or all of the code, and substitute the bits you want to change on the fly. The help file included with the SDK has some good code samples as well.

There is no need for the Office Interop or any other Office software on the server - the new formats are 100% XML.

情何以堪。 2024-07-12 03:01:09

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

它支持嵌入图像、表格以及文本,默认使用 Microsoft Word 打开,虽然它的功能集更加有限(不包括任何高级格式),但对于看起来、感觉和打开像 Word 文档的东西来说,它并不遥远。

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

Have you considered using .RTF as an alternative?

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.

Your end users probably won't notice.

稀香 2024-07-12 03:01:09

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

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

I have found Aspose Words to 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.

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

我为君王 2024-07-12 03:01:09

要在 .NET 中(特别是在 C# 或 VB.NET 中)使用 Office Automation 生成 Word 文档:

  1. Microsoft.Office.Interop.Word 程序集引用添加到您的项目中。 路径为 \Visual Studio Tools for Office\PIA\Office11\Microsoft.Office.Interop.Word.dll

  2. 遵循 Microsoft 代码示例
    您可以在这里找到:http://support.microsoft.com/kb/316384/en-我们

To generate Word documents with Office Automation within .NET, specifically in C# or VB.NET:

  1. Add the Microsoft.Office.Interop.Word assembly reference to your project. The path is \Visual Studio Tools for Office\PIA\Office11\Microsoft.Office.Interop.Word.dll.

  2. Follow the Microsoft code example
    you can find here: http://support.microsoft.com/kb/316384/en-us.

⒈起吃苦の倖褔 2024-07-12 03:01:09

Schmidty,如果您想在 Web 服务器上生成 Word 文档,您将需要每个客户端(而不仅仅是 Web 服务器)的许可证。 请参阅第一个链接中的本节 Rob 发布的内容:

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

如果您满足许可要求,我认为您将需要使用 COM Interop - 具体来说, Office XP 主互操作程序集

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 link Rob posted:

"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)."

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

黯淡〆 2024-07-12 03:01:09

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

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.

岛歌少女 2024-07-12 03:01:09

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

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

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

块引用
Essential DocIO 支持以几乎所有格式将标量和矢量图像插入到文档中。 位图、gif、png 和 tiff 是支持的一些常见图像类型。

所以值得考虑。

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

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

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.

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

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.

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

雪若未夕 2024-07-12 03:01:09

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

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.

毁梦 2024-07-12 03:01:09

我在 Open 上写了一个博客文章系列XML WordprocessingML 文档生成。 我的方法是创建一个包含内容控件的模板文档,并在每个内容控件中编写一个 XPath 表达式,该表达式定义如何从包含驱动文档生成过程的数据的 XML 文档中检索内容。 该代码是免费的,并根据 Microsoft 互惠许可证 (Ms-RL) 获得许可。 在同一博客文章系列中,我还探索了一种在内容控件中编写 C# 代码的方法。 然后,文档生成过程处理模板文档并生成可生成所需文档的 C# 程序。 这种方法的优点之一是您可以使用任何数据源作为文档生成过程的数据源。 该代码也根据 Microsoft 互惠许可证获得许可。

I've written a blog post series on Open XML WordprocessingML document generation. My approach is that you create a template document that contains content controls, and in each content control you write an XPath expression that defines how to retrieve the content from an XML document that contains the data that drives the document generation process. The code is free, and is licensed under the the Microsoft Reciprocal License (Ms-RL). In that same blog post series, I also explore an approach where you write C# code in content controls. The document generation process then processes the template document and generates a C# program that generates the desired documents. One advantage of this approach is that you can use any data source as the source of data for the document generation process. That code is also licenced under the Microsoft Reciprocal License.

╄→承喏 2024-07-12 03:01:09

我目前正在做这件事。

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

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

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

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

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.

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.

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.

顾挽 2024-07-12 03:01:09

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

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

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

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

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

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

夏有森光若流苏 2024-07-12 03:01:09

@Danny Smurf:实际上这篇文章描述了 Rob 回答的 Office Open XML 格式。 我会更加关注我现在发布的链接,以确保它们不会过时。 我实际上在 WordML 上进行了搜索,当时它是这么叫的。

我相信 Office Open XML 格式是最好的选择。

@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.

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

空‖城人不在 2024-07-12 03:01:09

LibreOffice 还支持通过 API 进行无头交互。 不幸的是,目前还没有太多关于此功能的信息..:(

LibreOffice also supports headless interaction via API. Unfortunately there's currently not much information about this feature yet.. :(

生来就爱笑 2024-07-12 03:01:09

您还可以使用 Word 文档生成器。 它可用于客户端或服务器端部署。 从项目描述来看:

WordDocumentGenerator 是一个用于生成 Word 文档的实用程序
使用 Visual Studio 2010 和 Open XML 2.0 SDK 的模板。
WordDocumentGenerator 有助于生成 Word 文档
不可刷新以及基于预定义模板的可刷新
使用最少的代码更改。 内容控件用作占位符
用于文档生成。 它支持Word 2007和Word 2010。

获取它:http://worddocgenerator.codeplex.com/

下载 SDK: http://www.microsoft.com/en-us/download /details.aspx?id=5124

You could also use Word document generator. It can be used for client-side or server-side deployment. From the project description:

WordDocumentGenerator is an utility to generate Word documents from
templates using Visual Studio 2010 and Open XML 2.0 SDK.
WordDocumentGenerator helps generate Word documents both
non-refresh-able as well as refresh-able based on predefined templates
using minimum code changes. Content controls are used as placeholders
for document generation. It supports Word 2007 and Word 2010.

Grab it: http://worddocgenerator.codeplex.com/

Download SDK: http://www.microsoft.com/en-us/download/details.aspx?id=5124

梦纸 2024-07-12 03:01:09

另一种选择是 Windward Docgen (免责声明 - 我是创始人)。 使用 Windward,您可以在 Word 中设计模板,包括图像、表格、图表、仪表以及您想要的任何其他内容。 您可以设置插入来自 XML 或 SQL 数据源的数据的标记(包括 forEach 循环、导入等功能)。 然后生成 DOCX、PDF、HTML 等格式的报告。

Another alternative is Windward Docgen (disclaimer - I'm the founder). With Windward you design the template in Word, including images, tables, graphs, gauges, and anything else you want. You can set tags where data from an XML or SQL datasource is inserted (including functionality like forEach loops, import, etc). And then generate the report to DOCX, PDF, HTML, etc.

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