使用Javascript从Excel工作表中获取数据

时间:2015-02-02 06:29:45

标签: javascript

我使用以下代码使用javascript从excel表中提取数据。这很好用,在打开HTML页面后,获取我想要的数据并关闭页面,我无法打开和编辑excel表,因为它抛出了异常,"文件已经由用户持有并且无法编辑"。无论如何都要在代码末尾处理excel表的关闭?请帮帮我。

<!DOCTYPE html>
<html>
<body>

Enter Salomon account:<br>
<input type="text" id="myText" name="SalAccount">
<br>
<button onclick="myFunction()">Submit</button>

<p id="demo"></p>

<script language="javascript" >

function myFunction() 
{
alert("hello");
var excel = new ActiveXObject("Excel.Application");
var excel_file = excel.Workbooks.Open("C:/Users/bv15457/Desktop/test1.xlsx");
var excel_sheet = excel.Worksheets("Sheet1");

var x = document.getElementById("myText").value;

var lo = 1;
var hi = 682220;
var mid;
var element;
var Flag = 0;

while(lo <= hi && Flag != 1)
{
    mid = Math.floor((lo + hi) / 2, 10);
    element = excel_sheet.Cells(mid,1).Value;
    if (element < x)
    {
        lo = mid + 1;
    }
    else if (element > x) 
    { 
        hi = mid - 1;
    }
    else
    {
        document.getElementById("demo").innerHTML = excel_sheet.Cells(mid,2).Value;
        Flag = 1;
    }
}

if (Flag != 1)
{
    alert("Account is not found in XREF file");
}

}
</script>
</body>
</html>

0 个答案:

没有答案