使用 IE、FF 和 Chrome 通过 JavaScript 从 Excel 中检索数据

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

Retrieving data from Excel by JavaScript using IE, FF and Chrome

javascriptexcelinternet-explorerfirefoxgoogle-chrome

提问by Ripon Al Wasim

I have an excel file named test.xls. The following JS code retrieve data from Excel in Internet Explorer well. But I want to use Firefox as well as Chrome. What's the code for FF and Chrome?

我有一个名为 test.xls 的 excel 文件。下面的 JS 代码很好地从 Internet Explorer 中的 Excel 中检索数据。但我想同时使用 Firefox 和 Chrome。FF 和 Chrome 的代码是什么?

<html>
<head>
<title>
Style Get data from excel sheet
</title>
<script language="javascript" >
  function GetData(cell,row){
  var excel = new ActiveXObject("Excel.Application");
  var excel_file = excel.Workbooks.Open("F:\test.xls");
  var excel_sheet = excel.Worksheets("Sheet1");
  var data = excel_sheet.Cells(cell,row).Value;
  document.getElementById('div1').innerText =data;
  }
  </script>
</head>
<body>
<p>&nbsp;</p>
<div style="background: #009955; width:'100%';" align="center">
  <font color="#000080" size="12pt">
<b>Get data from excel sheets</b>
  </font>
</div>
<center>
<p>&nbsp;</p>
<div id="div1" style="background: #DFDFFF; width:'100%';" align="center">
Click buttons to fetch data from F:\test.xls
</div>
<input type="button" value="cell(1),row(1)" onClick="GetData(1,1);" />
<input type="button" value="cell(1),row(2)" onClick="GetData(1,2);" />
<input type="button" value="cell(2),row(1)" onClick="GetData(2,1);" />
<input type="button" value="cell(2),row(2)" onClick="GetData(2,2);" />
</center>
</body>
</html>

回答by Christian Sauer

I am fairly sure that this only works for Internet Explorer. Additionally, this solution depends on an installed excel on the client machine. A better solution would be using epplus and reading the file on the server (only works for xlsx files, there are other solutions for the old xlsx files).

我相当确定这仅适用于 Internet Explorer。此外,此解决方案取决于客户端计算机上已安装的 excel。更好的解决方案是使用 epplus 并读取服务器上的文件(仅适用于 xlsx 文件,旧的 xlsx 文件还有其他解决方案)。