C# 有没有机器上没有word的动态生成word文档的方法

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

Is there a way to generate word documents dynamically without having word on the machine

c#ms-worddocument

提问by minty

I am planning on generating a Word document on the webserver dynamically. Is there good way of doing this in c#? I know I could script Word to do this but I would prefer another option.

我计划在网络服务器上动态生成 W​​ord 文档。在 C# 中有没有这样做的好方法?我知道我可以编写 Word 来执行此操作,但我更喜欢另一种选择。

回答by Brian

You'd be better off generating an rtf file, which word will know how to open.

你最好生成一个 rtf 文件,哪个词会知道如何打开。

回答by Chris Roberts

There are third party libraries about that will do the job.

有第三方库可以完成这项工作。

Doing a quick google came up with this one, for example.

例如,做一个快速的谷歌想出了这个

I haven't tried any, so I can't give you specific advice, I'm afraid!

我没有试过,所以我不能给你具体的建议,恐怕!

Let us know how you get on...

让我们知道您的身体情况如何...

回答by CMS

If want to generate Office 2007 documents check the Open XML File Formats, they're simple zipped XML files, check this links:

如果要生成 Office 2007 文档,请检查 Open XML 文件格式,它们是简单的压缩 XML 文件,请检查此链接:

Edit:Check this project, can serve you as a good starting point:

编辑:检查这个项目,可以作为一个很好的起点:

Seems very simple and customizable, look this code snippet:

看起来非常简单和可定制,看看这个代码片段:

Paragraph p = new Paragraph();
p.Runs.Add(new Run("Text can have multiple format styles, they can be "));
p.Runs.Add(new Run("bold and italic", 
        TextFormats.Format.Bold | TextFormats.Format.Italic));
doc.Paragraphs.Add(p);

回答by Fionnuala

Word will quite happily open a HTML with a .doc extension. If you include an internal style sheet, you can have it fully formatted. There was previous post on this subject:

Word 会很高兴地打开带有 .doc 扩展名的 HTML。如果包含内部样式表,则可以将其完全格式化。之前有一篇关于这个主题的帖子:

Export to Word Document in C#

在 C# 中导出到 Word 文档

回答by Jobi Joy

In Office 2007 Microsoft introduced a new file format called the Microsoft Open Office XML Format(.docx). This format is not compatible with older versions of Microsoft Word. Since this is XML you can create or read with out having a Word installed.

在 Office 2007 中,Microsoft 引入了一种称为Microsoft Open Office XML 格式(.docx)的新文件格式。此格式与旧版本的 Microsoft Word 不兼容。由于这是 XML,您无需安装 Word 即可创建或阅读。

回答by Vilx-

Creating the old .DOC files (pre-Word 2007) is nigh-impossible without Word itself. The format is just too complex. Microsoft has released the format description, but it's enough to reduce a grown programmer to tears. There is a reason for that too (historical), but that doesn't make things better.

如果没有 Word 本身,几乎不可能创建旧的 .DOC 文件(Word 2007 之前的版本)。格式太复杂了。微软已经发布了格式说明,但这足以让一个成熟的程序员流泪。这也是有原因的(历史上的),但这并没有让事情变得更好。

The new .DOCX would be easier, although quite a bit of hassle still. However depending on which Word versions you are targeting, there are some other options too.

新的 .DOCX 会更容易,但仍然有点麻烦。但是,根据您的目标 Word 版本,还有一些其他选项。

For one, there is the classic .RTF. The format is pretty complex still, yet well documented and has strong support across many applications and platforms. And you might use some string-replacing into template files to make things easier (it's non-binary).

一方面,有经典的 .RTF。该格式仍然相当复杂,但有据可查,并且在许多应用程序和平台上都有强大的支持。您可能会使用一些字符串替换到模板文件中来使事情变得更容易(它是非二进制的)。

Then there are the "old" Word XML files. I think they worked starting with Word XP. Kinda the predecessors of .DOCX. I've used them, not bad. And the documentation is pretty OK.

然后是“旧的”Word XML 文件。我认为他们从 Word XP 开始工作。有点像 .DOCX 的前身。我用过,还不错。而且文档还不错。

Finally, the easy way that I would choose, is to make a simple HTML. Word can load HTML files just fine starting with version 2000. In the simplest way just change the extension of a HTML file to .DOC and you have it. You can also add a few word-specific tags and comments to make it look even better in Word. Use the Word's Save As...HTML option to see what they are.

最后,我选择的简单方法是制作一个简单的 HTML。从 2000 版开始,Word 可以很好地加载 HTML 文件。最简单的方法是将 HTML 文件的扩展名更改为 .DOC 即可。您还可以添加一些特定于单词的标签和注释,使其在 Word 中看起来更好。使用 Word 的另存为...HTML 选项查看它们是什么。

回答by JB Brown

I've worked at a company in the past that really wanted generated word documents, in the end they were perfectly satisfied with RTF docs that had a ".doc" extension. Word has no problem recognizing and opening them.

我过去曾在一家真正想要生成 Word 文档的公司工作,最终他们对具有“.doc”扩展名的 RTF 文档非常满意。Word 识别和打开它们没有问题。

The RTF docs were generated with iText.net(free .net library), the API is pretty easy to use, performs extremely well, you don't need word on the machine, also, you could extend to generating PDF, HTML, and Text docs in the future with very little effort. After four years the solution I created is still in place, so that's a little testimony in iText.net's favor.

RTF 文档是用iText.net(免费的 .net 库)生成的,API 非常易于使用,性能非常好,你不需要在机器上写字,而且,你可以扩展到生成 PDF、HTML 和将来只需很少的努力即可编写文本文档。四年后,我创建的解决方案仍然存在,所以这是 iText.net 的一个小见证。

It looks like the official iText pagesuggests that iText Sharpis the best .Net choice right now, so that's another option

看起来官方iText 页面表明iText Sharp是目前最好的 .Net 选择,所以这是另一种选择

回答by JB Brown

Here is the component that generates document based on the custom template. The documents are generated from the sharepoint list ... so the data is pulled from the list item into the document on the fly: http://store.sharemuch.com/products/generate-word-documents-from-sharepoint-list

这是基于自定义模板生成文档的组件。文档是从共享点列表中生成的……所以数据会从列表项中动态提取到文档中:http: //store.sharemuch.com/products/generate-word-documents-from-sharepoint-list

Hope that helps,

希望有所帮助,

Yaroslav Pentsarskyy Blog: www.sharemuch.com

Yaroslav Pentsarskyy 博客:www.sharemuch.com