Html 如何在一个函数上使用javascript从excel文件中提取多个单元格。(如果可能的话)

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

How to extract multiple cells from an excel file using javascript on one function. (if possible)

javascripthtmlexcel

提问by user1739280

<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("I:test.xlsx");
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(2),row(1)" onClick="GetData(1,2);" />
<input type="button" value="cell(1),row(2)" onClick="GetData(2,1);" />
<input type="button" value="cell(2),row(2)" onClick="GetData(2,2);" />
</center>
</body>
</html>

I've tried this several times and I'm afraid I cannot explain myself very well so I found this generic code from this site that does almost exactly what I need it to do. The only thing is that I need it to get multiple cells from an excel file "onclick". As you can see, its only getting it from i.e. (1,1) but how would I write it if I wanted all of them (1,1)(1,2)(2,1)(2,2) to be gathered with only one button or a href? I know this must be easy to you guys but I'm new at this. Two weeks new. So please, any help would be appreciated. :) (And I've tried to play with it changing code here and there, but nada.)

我已经尝试了几次,恐怕我无法很好地解释自己,所以我从这个站点找到了这个通用代码,几乎完全符合我的需要。唯一的问题是我需要它来从 Excel 文件“onclick”中获取多个单元格。如您所见,它只能从 ie (1,1) 中获取,但是如果我希望所有 (1,1)(1,2)(2,1)(2,2) 都是只用一个按钮或一个href收集?我知道这对你们来说一定很容易,但我是新手。两周新。所以请,任何帮助将不胜感激。:)(而且我已经尝试使用它在这里和那里更改代码,但是 nada。)

回答by Tim Williams

Simplest example...

最简单的例子...

function gbid(s){return document.getElementById(s);}

function GetData(cell,row){
    var excel = new ActiveXObject("Excel.Application");
    var excel_file = excel.Workbooks.Open("I:\test.xlsx");
    var sht = excel.Worksheets("Sheet1");

    gbid('div1').innerText = sht.Cells(1,1).Value;
    gbid('div2').innerText = sht.Cells(1,2).Value;
    gbid('div3').innerText = sht.Cells(2,1).Value;
    gbid('div4').innerText = sht.Cells(2,2).Value;
}

To extract a range:

提取范围:

var v = sht.Range(sht.Cells(1,1), sht.Cells(10,10)).Value;
//or
var v = sht.Range("A1:J10").Value;