使用 JavaScript “创建” Microsoft Word 文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6779926/
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
Using JavaScript to "Create" a Microsoft Word Document
提问by user535056
I would like to dynamically create a document using JavaScript and then open that document in Microsoft word. Is this possible? Here is my current code:
我想使用 JavaScript 动态创建一个文档,然后在 Microsoft Word 中打开该文档。这可能吗?这是我当前的代码:
<html>
<head>
<title></title>
<script src="js/jquery-1.4.4.js" type="text/javascript"></script>
</head>
<body>
<div id="myDiv">The quick brown fox jumped lazly over the dead log.</div>
<script type="text/jscript">
var printWindow = window.open("", "Print", "width=800,height=400,scrollbar=0");
var printAreaHtml = $("#myDiv").attr("outerHTML");
printWindow.document.open("text/html", "replace");
printWindow.document.writeln("<html><head>")
printWindow.document.writeln("<meta HTTP-EQUIV='Content-Type' content='application/vnd.ms-word'>");
printWindow.document.writeln("<meta HTTP-EQUIV='Content-Disposition' content='attachment;filename=print.doc'>");
printWindow.document.writeln("</head>");
printWindow.document.writeln("<body>");
printWindow.document.write(printAreaHtml);
printWindow.document.writeln("</body>");
printWindow.document.writeln("</html>");
printWindow.document.close();
// printWindow.print();
</script>
</body>
</html>
回答by Evan
I'm not sure exactly what you are trying to do in your code up there but here is some information i found about accessing a word document and a table within the doc:
我不确定您在上面的代码中到底要做什么,但这里是我发现的有关访问 Word 文档和文档中的表格的一些信息:
Microsoft Word Object Model
This object model is part of Microsoft Word (not Javascript) and it lets you "automate" word remotely from other programs (not just web pages, but any computer program).
It is primarily designed for Visual Basic, but can be accessed by Javascript from a web page - see para 2 below.
However it is a bit more tricky to use through Javascript, particularly because you cannot use visual basic constants - you need to refer to them by value. If you research this further, you will soon know what I mean by this.
So where can you find out about this Object Model?
It is all there in the Word help files if you look for it.
If you look in the Word help, under programming information, you will find the Microsoft Word Visual Basic Programming Reference.
The Word object model, which lets you do things you will need to solve your problem like:
- Open Word
- Open a Document in Word
- Access the collection of Tables in that ActiveDocument.
- Access the Rows and Cells of a given Table.
How do you access this from Javascript?
This might only be done I think through Internet Explorer (and perhaps Opera).
Here you need to learn about ActiveXObjects.
ActiveXObjects (if you do not know) are separate computer programs which enable additional functionality. There are lots of ActiveX objects on the internet.
When you install Word, this also installs an ActiveX object for automating word, giving you access to the Word Object Model.
So in javascript, lets open up a new instance of word:
var oApplication=new ActiveXObject("Word.Application"); oApplication.Visible=true; // "Visible" is in the Word Object Model`
There you have it.
Then if you want to open you file and get the table:
oApplication.Documents.Open("myfilename"); var oDocument=oApplication.ActiveDocument; var oTable=oDocument.Tables(1);`
Microsoft Word 对象模型
这个对象模型是 Microsoft Word(不是 Javascript)的一部分,它可以让您从其他程序(不仅仅是网页,而是任何计算机程序)远程“自动化”单词。
它主要是为 Visual Basic 设计的,但可以通过网页上的 Javascript 访问 - 请参阅下面的第 2 段。
然而,通过 Javascript 使用它有点棘手,特别是因为您不能使用可视化基本常量 - 您需要按值引用它们。如果你进一步研究这个,你很快就会明白我的意思。
那么从哪里可以找到有关此对象模型的信息?
如果您要查找,Word 帮助文件中都有。
如果您查看 Word 帮助,在编程信息下,您将找到 Microsoft Word Visual Basic 编程参考。
Word 对象模型,可让您执行解决问题所需的操作,例如:
- 打开字
- 在 Word 中打开文档
- 访问该 ActiveDocument 中的表集合。
- 访问给定表的行和单元格。
你如何从 Javascript 访问它?
我认为这可能只能通过 Internet Explorer(也许还有 Opera)来完成。
在这里,您需要了解 ActiveXObjects。
ActiveXObjects(如果您不知道)是启用附加功能的独立计算机程序。Internet 上有很多 ActiveX 对象。
当您安装 Word 时,这也会安装一个 ActiveX 对象来自动处理 word,让您可以访问 Word 对象模型。
所以在 javascript 中,让我们打开一个新的 word 实例:
var oApplication=new ActiveXObject("Word.Application"); oApplication.Visible=true; // "Visible" is in the Word Object Model`
你有它。
然后,如果您想打开文件并获取表格:
oApplication.Documents.Open("myfilename"); var oDocument=oApplication.ActiveDocument; var oTable=oDocument.Tables(1);`
And now I leave it to you to keep going with the rest.
现在我把它留给你继续做其余的事情。
回答by binarious
I don't believe that this idea will work. You need to create the Word file with a serverside language. For example PHP: http://www.webcheatsheet.com/php/create_word_excel_csv_files_with_php.php
我不相信这个想法会奏效。您需要使用服务器端语言创建 Word 文件。例如 PHP:http: //www.webcheatsheet.com/php/create_word_excel_csv_files_with_php.php
回答by Colin Pickard
EDIT: this wasn't possible when the question was asked but in 2017 it is. See link from comment by jrm - http://www.effectiveui.com/blog/2015/02/23/generating-a-downloadable-word-document-in-the-browser/
编辑:当提出问题时这是不可能的,但在 2017 年是。查看 jrm 评论中的链接 - http://www.effectiveui.com/blog/2015/02/23/generating-a-downloadable-word-document-in-the-browser/
Browser place some serious restrictions on Javascript which will prevent you creating a downloadable file. See this related question:
浏览器对 Javascript 设置了一些严格的限制,这将阻止您创建可下载的文件。看到这个相关的问题:
Create a file in memory for user to download, not through server
回答by Leonardo
if you need server side document generation and server is running Java, take a look at this: https://github.com/leonardoanalista/java2word/
如果您需要生成服务器端文档并且服务器正在运行 Java,请查看:https: //github.com/leonardoanalista/java2word/
回答by Parag Arora
You can not get this working using client side. Main thing is you need to send headers not as html. So I would suggest you to use server side scripting as Max suggested and preferably use .htaccess file if you are using Apache server to also name these files as .doc.
您无法使用客户端进行此工作。最主要的是您需要发送标题而不是 html。所以我建议你使用 Max 建议的服务器端脚本,如果你使用 Apache 服务器也将这些文件命名为 .doc,最好使用 .htaccess 文件。
Lets assume your php file needs to create a .doc file with some passed argument lets say id. So you want file_.doc to point to file.php?id=, try using following rewrite rule so that browser understands by extension too RewriteRule file_(.*).doc file.php?id=$1
让我们假设您的 php 文件需要创建一个带有一些传递参数的 .doc 文件,比如 id。所以你想让 file_.doc 指向 file.php?id=,请尝试使用以下重写规则,以便浏览器也能通过扩展名理解 RewriteRule file_(.*).doc file.php?id=$1
回答by Aaron Adel
This is absolutely possible. Googooseis a jQuery plugin that I wrote to handle a lot of the more complicated conversions. It's still fairly new, but there appear to be a few other attempts at this, so you could check those out. Here is the best documentation I've found so far that actually explains this process http://sebsauvage.net/wiki/doku.php?id=word_document_generation. If you're interested check out the examples in Googoose.
这是绝对可能的。Googoose是我编写的一个 jQuery 插件,用于处理许多更复杂的转换。它仍然是相当新的,但似乎还有其他一些尝试,所以你可以检查一下。这是迄今为止我找到的最好的文档,它实际上解释了这个过程http://sebsauvage.net/wiki/doku.php?id=word_document_generation。如果您有兴趣,请查看 Googoose 中的示例。
回答by Max
sometimes we can not use server side app or activeX to create office document because of phonegap mobile app that uses only client-side javascipt to operate. the only way i found for now is uding word binary file format or OOXML
有时我们无法使用服务器端应用程序或activeX创建办公文档,因为phonegap移动应用程序仅使用客户端javascipt来操作。我现在找到的唯一方法是使用 word 二进制文件格式或 OOXML
http://msdn.microsoft.com/en-us/library/hh643138(v=office.12)
http://msdn.microsoft.com/en-us/library/hh643138(v=office.12)
some say that its much easier to create RTF file and i agree with them.
有人说创建 RTF 文件要容易得多,我同意他们的看法。