Javascript Excel OpenFile

时间:2013-03-06 12:01:20

标签: javascript excel

我有这段代码:

<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 个答案:

答案 0 :(得分:5)

首先,尝试将脚本移动到正文的底部。您还应将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>

答案 1 :(得分: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>

相关问题