在 C# 中导出到 Word 文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/282531/
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
Export to Word Document in C#
提问by Adam Haile
I'm looking for a .NET library that will allow creation of a Word document. I need to export HTML based content to a Word doc (97-2003 format, not docx).
我正在寻找一个允许创建 Word 文档的 .NET 库。我需要将基于 HTML 的内容导出到 Word 文档(97-2003 格式,而不是 docx)。
I know that there are the Microsoft Office Automation libraries and Office interop, but as far as I can tell, they require that you have office actually installed and they do the conversion by opening word itself. But I don't want to have the requirement of having office installed for the conversion to work.
我知道有 Microsoft Office 自动化库和 Office 互操作,但据我所知,它们要求您实际安装 Office,并且它们通过打开 word 本身来进行转换。但我不想有安装办公室的要求来转换工作。
Edit: Converting to RTF may even work, if possible.
编辑:如果可能,转换为 RTF 甚至可能有效。
采纳答案by Fionnuala
Would it work if I somehow converted the CSS to be embedded in the HTML??
如果我以某种方式将 CSS 转换为嵌入 HTML 中,它会起作用吗?
Yes. I use an internal style sheet, as I mentioned.
是的。正如我提到的,我使用内部样式表。
Document Example:
文档示例:
<html>
<head>
<STYLE type="text/css">
h1 {text-align:center; font-size:12.0pt; font-family:Arial; font-weight:bold;}
p {margin:0in; margin-bottom:0pt; font-size: 10.0pt;font-family: Arial;}
p.Address {text-align:center;font-family:Times; margin-bottom: 10px;}
</style></head>
<body>
<p class="Address">The Street</p>
<h1>Head</h1>
回答by Erik Hesselink
Since the doc format specification is not open, and the interop assemblies are the Microsoft solution, I fear that they are your primary (or even only) option.
由于 doc 格式规范未开放,并且互操作程序集是 Microsoft 解决方案,我担心它们是您的主要(甚至唯一)选项。
They do indeed require office to be installed, and they open Word (although showing a window is optional).
它们确实需要安装 office,并且它们会打开 Word(尽管显示窗口是可选的)。
I think Word can open HTML documents; is that an option for you?
我认为 Word 可以打开 HTML 文档;那是你的选择吗?
回答by Fionnuala
I have found that a document output to HTML but called .doc will open properly formated in Word. I tested with Word 2000 and a file with an internal style sheet.
我发现输出为 HTML 但名为 .doc 的文档将在 Word 中以正确格式打开。我使用 Word 2000 和带有内部样式表的文件进行了测试。
回答by Christopher Edwards
Using Word Automation from ASP.NET is not a good idea (see the MSKB - http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2)
从 ASP.NET 使用 Word Automation 不是一个好主意(请参阅 MSKB - http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2)
If you are not using WinForms your best option IMHO is to generate RTF, which ms word will happily open. (see the link in the already referenced article).
如果您不使用 WinForms,恕我直言,您最好的选择是生成 RTF,ms word 会很乐意打开。(请参阅已引用文章中的链接)。
Good Luck!
祝你好运!
回答by Adam Haile
I tried just opening the html directly in word, which technically works except for one thing... My html doc alsocontains CSS, and when opening in Word, it completely ignores the CSS so I no longer have any of the formatting. I realize that I wouldn't get everything out of the CSS but I would at least like to still have the specified fonts, font sizes, etc... Any way to get it to read the CSS? Would it work if I somehow converted the CSS to be embedded in the HTML??
我尝试直接在 word 中打开 html,这在技术上是有效的,除了一件事......我的 html 文档也包含 CSS,当在 Word 中打开时,它完全忽略了 CSS,所以我不再有任何格式。我意识到我不会从 CSS 中获取所有内容,但我至少希望仍然拥有指定的字体、字体大小等......有什么方法可以让它读取 CSS?如果我以某种方式将 CSS 转换为嵌入 HTML 中,它会起作用吗?
回答by Chris Canal
I use Aspose for working with Word, makes everything a breeze: http://www.aspose.com/
我使用 Aspose 来处理 Word,让一切变得轻而易举:http: //www.aspose.com/
回答by Andrew Hancox
There's a tool called JODConverter which hooks into open office to expose it's file format converters, there's versions available as a webapp (sits in tomcat) which you post to and a command line tool. I've been firing html at it and converting to .doc and pdf succesfully it's in a fairly big project, haven't gone live yet but I think I'm going to be using it. http://sourceforge.net/projects/jodconverter/
有一个名为 JODConverter 的工具,它连接到开放式办公室以公开它的文件格式转换器,有一些版本可用作您发布到的 web 应用程序(位于 tomcat 中)和一个命令行工具。我一直在使用 html 并成功地转换为 .doc 和 pdf 它在一个相当大的项目中,还没有上线,但我想我会使用它。 http://sourceforge.net/projects/jodconverter/