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

时间:2022-10-30 15:45:21

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格式生成一个报表导出。报告将包括图像/图表、表格和文本。最好的方法是什么?第三方工具吗?你的经验是什么?

17 个解决方案

#1


49  

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的office生成格式,因为在服务器上使用office自动化时存在一些问题。

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.

但是,如果您正在处理客户端机器,那么您可以选择使用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

#2


17  

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.

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

#3


14  

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.

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

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

不需要在服务器上使用Office互操作或任何其他Office软件——新的格式是100% XML。

#4


10  

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.

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

#5


10  

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.

我发现,如果不是每个人都能打开Office open XML/*,那么这是最好的。docx格式文件、词的互操作和词的自动操作可能存在错误。Aspose Word支持从Word 97向上的大多数文档文件类型。

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

这是一个付费的部分,但有很大的支持。另一个已经建议的替代方案是RTF。

#6


7  

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

在。net中生成具有办公自动化的Word文档,特别是在c#或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.

    添加Microsoft.Office.Interop。对项目的Word汇编引用。路径是为Office\PIA\ \ \Office11\ microsoft.coop . interop . word.dll准备的Visual Studio工具。

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

    您可以在这里找到Microsoft代码示例:http://support.microsoft.com/kb/316384/en-us。

#7


4  

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:

Schmidty,如果你想在web服务器上生成Word文档,你需要为每个客户端(不仅仅是web服务器)提供许可证。请参阅第一个链接中的这一节:

"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的许可副本。使用服务器端自动化为无许可证的工作站提供办公功能不受最终用户许可协议(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互操作—具体地说,就是Office XP主互操作程序集。

#8


3  

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中打开doc时,Word读取xml并将其转换为WordML并呈现它。您将希望查看VSTO库的ServerDocument类。我的经验不需要额外的授权。

#9


3  

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

我已经成功地使用了支持doc和docx格式的Syncfusion后台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.aspx it 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文档,有一些好的RTF库围绕。net,如http://www.codeproject.com/KB/string/nrtftree.aspx。

#10


3  

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。

#11


3  

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.

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

#12


2  

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.

如果文档不是很大,不包含图像等等,那么我将它存储为一个RTF,其中包含#MergeFields#,然后用内容替换它们,将结果作为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文档保存为一个单独的页面*。mht文件再次包含#MergeFields#。然后我做了和上面一样的事情。使用它,我可以轻松地用一些基本的Html表标记呈现一个DataTable,并将#MergeFields#中的一个替换为整个表。

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

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

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库来切换内容,例如图像。

#13


1  

@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

#14


1  

@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:实际上,这篇文章描述了罗布回答的Office Open XML格式。我会更加关注我现在发布的链接,以确保不会过时。实际上,我搜索了WordML,这是当时它的名字。

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

我认为Office Open XML格式是最好的方法。

#15


1  

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

LibreOffice还支持通过API进行无头交互。不幸的是,目前关于这个特性的信息还不多。:(

#16


1  

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

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

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.

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

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

抓住它:http://worddocgenerator.codeplex.com/

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

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

#17


0  

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.

另一种选择是Windward Docgen(声明:我是创始人)。通过向风设计Word格式的模板,包括图像、表格、图形、量规和其他任何你想要的东西。您可以在插入XML或SQL数据源数据的地方设置标记(包括forEach循环、import等功能)。然后生成报告给DOCX、PDF、HTML等。

#1


49  

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的office生成格式,因为在服务器上使用office自动化时存在一些问题。

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.

但是,如果您正在处理客户端机器,那么您可以选择使用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

#2


17  

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.

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

#3


14  

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.

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

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

不需要在服务器上使用Office互操作或任何其他Office软件——新的格式是100% XML。

#4


10  

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.

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

#5


10  

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.

我发现,如果不是每个人都能打开Office open XML/*,那么这是最好的。docx格式文件、词的互操作和词的自动操作可能存在错误。Aspose Word支持从Word 97向上的大多数文档文件类型。

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

这是一个付费的部分,但有很大的支持。另一个已经建议的替代方案是RTF。

#6


7  

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

在。net中生成具有办公自动化的Word文档,特别是在c#或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.

    添加Microsoft.Office.Interop。对项目的Word汇编引用。路径是为Office\PIA\ \ \Office11\ microsoft.coop . interop . word.dll准备的Visual Studio工具。

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

    您可以在这里找到Microsoft代码示例:http://support.microsoft.com/kb/316384/en-us。

#7


4  

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:

Schmidty,如果你想在web服务器上生成Word文档,你需要为每个客户端(不仅仅是web服务器)提供许可证。请参阅第一个链接中的这一节:

"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的许可副本。使用服务器端自动化为无许可证的工作站提供办公功能不受最终用户许可协议(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互操作—具体地说,就是Office XP主互操作程序集。

#8


3  

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中打开doc时,Word读取xml并将其转换为WordML并呈现它。您将希望查看VSTO库的ServerDocument类。我的经验不需要额外的授权。

#9


3  

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

我已经成功地使用了支持doc和docx格式的Syncfusion后台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.aspx it 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文档,有一些好的RTF库围绕。net,如http://www.codeproject.com/KB/string/nrtftree.aspx。

#10


3  

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。

#11


3  

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.

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

#12


2  

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.

如果文档不是很大,不包含图像等等,那么我将它存储为一个RTF,其中包含#MergeFields#,然后用内容替换它们,将结果作为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文档保存为一个单独的页面*。mht文件再次包含#MergeFields#。然后我做了和上面一样的事情。使用它,我可以轻松地用一些基本的Html表标记呈现一个DataTable,并将#MergeFields#中的一个替换为整个表。

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

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

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库来切换内容,例如图像。

#13


1  

@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

#14


1  

@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:实际上,这篇文章描述了罗布回答的Office Open XML格式。我会更加关注我现在发布的链接,以确保不会过时。实际上,我搜索了WordML,这是当时它的名字。

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

我认为Office Open XML格式是最好的方法。

#15


1  

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

LibreOffice还支持通过API进行无头交互。不幸的是,目前关于这个特性的信息还不多。:(

#16


1  

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

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

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.

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

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

抓住它:http://worddocgenerator.codeplex.com/

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

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

#17


0  

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.

另一种选择是Windward Docgen(声明:我是创始人)。通过向风设计Word格式的模板,包括图像、表格、图形、量规和其他任何你想要的东西。您可以在插入XML或SQL数据源数据的地方设置标记(包括forEach循环、import等功能)。然后生成报告给DOCX、PDF、HTML等。