使用 javascript 将 HTML 转换为单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32222940/
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
HTML to word converting using javascript
提问by Vicky
I am trying to convert HTML to word (.docx) by using JavaScript. I am using this http://www.jqueryscript.net/other/Export-Html-To-Word-Document-With-Images-Using-jQuery-Word-Export-Plugin.htmlplug-in for conversion. But this one is just converting every thing inside the HTML file. i mean with head tag all elements even with some content inside. output file looks like this
我正在尝试使用 JavaScript 将 HTML 转换为 word (.docx)。我正在使用这个http://www.jqueryscript.net/other/Export-Html-To-Word-Document-With-Images-Using-jQuery-Word-Export-Plugin.html插件进行转换。但这只是转换 HTML 文件中的所有内容。我的意思是用 head 标记所有元素,即使里面有一些内容。输出文件看起来像这样
Mime-Version: 1.0 Content-Base: file:///home/userprofile/JsWs/sample.html Content-Type: Multipart/related; boundary="NEXT.ITEM-BOUNDARY";type="text/html"
--NEXT.ITEM-BOUNDARY Content-Type: text/html; charset="utf-8" Content-Location: file:///home/userprofile/JsWs/sample.html
<p>this is going to be paragraph </p> </body></html>
--NEXT.ITEM-BOUNDARY--
Mime-Version: 1.0 Content-Base: file:///home/userprofile/JsWs/sample.html Content-Type: Multipart/related; 边界="NEXT.ITEM-BOUNDARY";type="text/html"
--NEXT.ITEM-BOUNDARY 内容类型:text/html;charset="utf-8" 内容位置:file:///home/userprofile/JsWs/sample.html
<p>this is going to be paragraph </p> </body></html>
--NEXT.ITEM-边界--
and my html is
而我的HTML是
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="FileSaver.js"></script>
<script src="jquery.wordexport.js"></script>
</head>
<body>
<script type="text/javascript">
jQuery(document).ready(function($) {
$("a.word-export").click(function(event) {
$("#export-content").wordExport();
});
});
</script>
<div id="export-content">
<p>this is going to be paragraph </p>
</div>
<a class="word-export" href="javascript:void(0)"> Export as .doc </a>
</body>
</html>
Help me out how can i convert content of HTML in word.
帮我看看如何在 word 中转换 HTML 的内容。
回答by Aaron Adel
I don't believe this will open in Microsoft Word consistently on all devices. I've actually been working on a similar project googoose. It is meant to convert html to word documents as well. This does work for me on both my desktop and mobile phone version of Microsoft Word.
我不相信这会在所有设备上始终在 Microsoft Word 中打开。我实际上一直在研究一个类似的项目googoose。它也旨在将 html 转换为 word 文档。这在我的桌面版和手机版 Microsoft Word 上都对我有用。
From my testing, Word will have problems with MIME header, and also the fact that there are no html, body tags, etc.
根据我的测试,Word 会出现 MIME 标头问题,并且没有 html、body 标签等。
You should look into this project if you're still trying to do this. It should be supported for some time, as there's also a Wordpress Pluginthat uses googoose, which is linked to on the readme. The default action is to download the document on page load, but you can set it to be run onClick. For example,
如果你还在尝试这样做,你应该研究这个项目。它应该支持一段时间,因为还有一个使用 googoose的Wordpress 插件,它链接到自述文件。默认操作是在页面加载时下载文档,但您可以将其设置为在单击时运行。例如,
Just include jquery and this plugin.
只需包含 jquery 和这个插件。
<script type="text/javascript" src="http://github.com/aadel112/googoose/jquery.googoose.js"></script>
Then to invoke onClick
然后调用onClick
jQuery(document).ready(function($) {
$("a.word-export").click(function(event) {
$(document).googoose({
area: '#export-content'
});
});
});
With googoose you can include a table of contents, header, footer, automatic page numbering, etc. You can open the document up in Print view, set the margins, and page size. It's way more configurable than the referenced script, which is not configurable at all.
使用 googoose,您可以包含目录、页眉、页脚、自动页码等。您可以在打印视图中打开文档,设置边距和页面大小。它比引用的脚本更具可配置性,后者根本不可配置。
回答by Drashti
Try this,
试试这个,
function ConvertToHtml()
{
var div = document.createElement("div");
div.id = "export-content";
div.innerHTML = "<p>this is going to be paragraph </p>";
document.body.appendChild(div);
$("#export-content").wordExport();
}