Javascript Excel OpenFile

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15246837/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:02:05  来源:igfitidea点击:

Javascript Excel OpenFile

javascriptexcel

提问by PythonNewbie

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>

So the main problem is I keep getting this error:

所以主要问题是我不断收到此错误:

On line 7 , Unable to get value of property Open
URL file:///C:/users/admin/desktop/test.hta

回答by Simon Adcock

Firstly, try moving your script to the bottom of the body. You should also set your Excelvariable to be visible. And there's a typo with the line Excel.Workbook.Open("teste.xlsx");(should be Workbooks). The following is working for me in IE. I don't think it will work in other browsers:

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

回答by Jebasingh

This work with IE 11 and you have to enable all the ActiveX controls in the Internet options. This will open the excel and open the exact sheet what you mentioned in the sheet name.

这适用于 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>