使用JavaScript来“创建” Microsoft Word文档 [英] Using JavaScript to "Create" a Microsoft Word Document

查看:90
本文介绍了使用JavaScript来“创建” Microsoft Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JavaScript动态创建文档,然后在Microsoft Word中打开该文档。这可能吗?这是我目前的代码:

 < html> 
< head>
< title>< / title>

< script src =js / jquery-1.4.4.jstype =text / javascript>< / script>
< / head>
< body>

< div id =myDiv>快速的棕色狐狸懒洋洋地跳过死亡的日志。< / 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字'>中);
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>


解决方案

我不确定你到底想要做什么在你的代码中做,但这里有一些关于访问word文档和doc中的表格的信息:


  1. Microsoft Word对象模型



    此对象模型是Microsoft Word(不是Javascript)的一部分,它允许您从其他程序(不仅仅是网页)远程自动化单词任何计算机程序。)



    它主要是为Visual Basic设计的,但可以通过网页上的Javascript访问 - 见下文第2段。



    然而,通过Javascript使用它有点棘手,特别是因为你不能使用visual basic常量 - 你需要通过值来引用它们。如果你进一步研究这个,你很快就会知道我的意思。



    那你在哪里可以找到这个对象模型?



    如果您查找Word帮助文件,它就在那里。



    如果你查看Word帮助,在编程信息下,你将找到Microsoft Word Visual Basic编程参考。



    Word对象模型,它允许您执行解决问题所需的操作,如:




    • 打开Word

    • 在Word中打开文档

    • 访问表的集合在那个ActiveDocument中。

    • 访问给定表的行和单元格。


  2. 你如何从Javascript访问它?



    这可能只能通过Internet Explorer(也许是Opera)来实现。



    在这里你需要了解ActiveXObjects。



    ActiveXObjects(如果你不知道)是单独的计算机程序启用其他功能。互联网上有很多ActiveX对象。



    当你安装Word时,这也会安装一个ActiveX对象来自动化word,让你可以访问Word对象模型。 / p>

    所以在javascript中,让我们打开一个新的单词实例:

      var oApplication = new ActiveXObject(Word.Application); 
    oApplication.Visible = true; //可见在Word对象模型中`

    你有它。



    然后,如果你想打开你的文件并获得表格:

      oApplication。 Documents.Open( mYfILEname的名称); 
    var oDocument = oApplication.ActiveDocument;
    var oTable = oDocument.Tables(1);`


现在我留给你继续其余的事情。


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:

<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>

解决方案

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:

  1. 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.
  2. 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);`
    

And now I leave it to you to keep going with the rest.

这篇关于使用JavaScript来“创建” Microsoft Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆