JavaScript Excel 打开文件

4

I have this code:

<html>
  <script type="text/javascript">
    function test() {
      var Excel = new ActiveXObject("Excel.Application");
      Excel.Workbook.Open("teste.xlsx");
    }
  </script>

  <body>

    <form name="form1">
      <input type=button onClick="test();" value="Open File">
      <br><br>
    </form>

  </body>
</html>

所以主要问题是我一直收到这个错误提示:
On line 7 , Unable to get value of property Open
URL file:///C:/users/admin/desktop/test.hta
2个回答

7
首先,尝试将您的脚本移动到body的底部。您还应该将Excel变量设置为可见。代码行Excel.Workbook.Open("teste.xlsx");中有一个错别字(应该是Workbooks)。以下内容可以在IE中正常工作,但我不认为它会在其他浏览器中工作:
<html>

  <body>

    <form name="form1">
      <input type=button onClick="test()" value="Open File">
      <br><br>
    </form>

    <script type="text/javascript">
      function test() {
        var Excel = new ActiveXObject("Excel.Application");
        Excel.Visible = true;
        Excel.Workbooks.Open("teste.xlsx");
      }
    </script>
  </body>
</html>

2
肯定不会在其他浏览器中起作用。但是,自从我制作 HTA 以来已经很长时间了。 - Svend
1
对我来说,这并不像看起来的那么容易,我更改了代码为:code Excel.Workbooks.Open("C:\Users\Admin\Desktop\teste.xlsx"); code否则它只会打开一个空白的Excel文件。 - PythonNewbie

1
这适用于IE 11,您需要在Internet选项中启用所有ActiveX控件。 这将打开Excel并打开您在工作表名称中提到的确切工作表。

<form name="form1">
  <input type=button onClick="test()" value="Open File">
  <br><br>
</form>

<script type="text/javascript">
  function test() 
  {  
    var Excel = new ActiveXObject("Excel.Application");  
    Excel.Visible = true; Excel.Workbooks.open("c:\\jeba\\sample.xlsx");  
    var excel_sheet = Excel.Worksheets("sheetname_1");  
    excel_sheet.activate();  
  }   
</script>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接