如何将 HTML 文件转换为 Word?

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

How to convert HTML file to word?

htmlms-word

提问by Mask

I need to save HTML documents in memory as Word .DOC files.

我需要将 HTML 文档作为 Word .DOC 文件保存在内存中。

Can anybody give me some links to both closed and open source libraries that I can use to do this?

任何人都可以给我一些封闭和开源库的链接,我可以用它来做这件事吗?

Also, I should edit this question to add the language I'm using in order to narrow down the choices.

此外,我应该编辑此问题以添加我正在使用的语言以缩小选择范围。

回答by Jan

Try using pandoc

尝试使用 pandoc

pandoc -f html -t docx -o output.docx input.html

If the input or output format is not specified explicitly, pandoc will attempt to guess it from the extensions of the input and output filenames.
— pandoc manual

如果未明确指定输入或输出格式,pandoc 将尝试从输入和输出文件名的扩展名中猜测它。
— pandoc 手册

So you can even use

所以你甚至可以使用

pandoc -o output.docx input.html

回答by SAR

just past this on head of your php page. before any code on this should be the top code.

刚刚在你的 php 页面的头部过去。在此之前的任何代码都应该是最上面的代码。

<?php
header("Content-Type: application/vnd.ms-word"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("content-disposition: attachment;filename=Hawala.doc");

?>

this will convert all html to MSWORD, now you can customize it according to your client requirement.

这会将所有 html 转换为 MSWORD,现在您可以根据您的客户要求对其进行自定义。

回答by user1980965

A good option is to use an API like Docverter. Docverter will allow you to convert HTML to PDF or DOCX using an API.

一个不错的选择是使用像Docverter这样的 API 。Docverter 将允许您使用 API 将 HTML 转换为 PDF 或 DOCX。

回答by SnapShot

When doing this I found it easiest to:

这样做时,我发现最容易:

  1. Visit the page in a web browser
  2. Save the page using the web browser with .htm extension (and maybe a folder with support files)
  3. Start Word and open the saved htmfile (Word will open it correctly)
  4. Make any edits if needed
  5. Select Save As and then choose the extension you would like doc, docx, etc.
  1. 在网络浏览器中访问页面
  2. 使用带有 .htm 扩展名的 Web 浏览器保存页面(可能还有一个包含支持文件的文件夹)
  3. 启动Word并打开保存的htmfile(Word会正确打开)
  4. 如果需要,进行任何编辑
  5. 选择另存为,然后选择您想要的扩展名 doc、docx 等。

回答by sleath

Other Alternatives from just renaming the file to .doc.....

其他替代方法只是将文件重命名为 .doc .....

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word(office.11).aspx

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word(office.11​​).aspx

Here is a good place to start. You can also try using this Office Open XML.

这是一个很好的起点。您也可以尝试使用此 Office Open XML。

http://www.ecma-international.org/publications/standards/Ecma-376.htm

http://www.ecma-international.org/publications/standards/Ecma-376.htm