1

我正在使用 html5 和 javascript。我正在从 java 脚本中读取 excel 文件并显示输出。请先分析我的代码

 <input type="button" id="btnSubmit" onclick="readdata(1, 2)" value="Submit" />

 

var xVal = 1;
var yVal = 2

    function readdata(x,y) {
        x = xVal;
        y = yVal;
        try {
            var excel = new ActiveXObject("Excel.Application");
            excel.Visible = false;
            var excel_file = excel.Workbooks.Open("D:\\Test.xls");// alert(excel_file.worksheets.count);
            var excel_sheet = excel_file.Worksheets("Sheet1");
            var data = excel_sheet.Cells(x, y).Value;
            //alert(data);
            drawWithexcelValue(data);
            xVal = xVal + 1;
        }
        catch (ex) {
            alert(ex);
        }

现在我正在从这段代码中读取文件并用这段代码显示输出:

function drawWithexcelValue(val) {

    var txtSpeed = val; //alert(txtSpeed.value);
    if (txtSpeed !== null) {

        iTargetSpeed = txtSpeed;

        // Sanity checks
        if (isNaN(iTargetSpeed)) {
            iTargetSpeed = 0;
        } else if (iTargetSpeed < 0) {
            iTargetSpeed = 0;
        } else if (iTargetSpeed > 80) {
            iTargetSpeed = 80;
        }

        job = setTimeout("draw()", 5);
    }
}

Q .1 每次我单击提交按钮时,它都会显示来自 excel 文件的值,我希望我不必每次都单击提交按钮..它会以某个时间间隔自动显示值,例如 4 秒.

问:-2 我不想要提交按钮,这意味着当我运行此代码时,它会自动开始运行脚本说 onload =“readdata(1, 2)”,但它只显示一个值...如何以一定的时间间隔显示所有值..请帮助!!!!!!

伙计们,如果你能给我编辑过的代码,那对我来说真的会很有帮助

4

1 回答 1

2

在这里,这段代码肯定对你有用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Speedometer HTML5 Canvas</title>

    <script src="script copy.js">

</script>

</head>
<body onload='draw(0);'>
    <canvas id="tutorial" width="440" height="220">
            Canvas not available.
        </canvas>
    <div id="divHidden" style="visibility: hidden; width: 0px; height: 0px">
        <form id="drawTemp">
        <input type="text" id="txtSpeed" name="txtSpeed" value="20" maxlength="2" />
        <input type="button" value="Draw" onclick="drawWithInputValue();">
        <input type="file" id="file" onchange="checkfile(this);" />
       <input type="button" id="btnSubmit" onclick="readdata(1, 2)" value="Submit" /> 


    <button onclick="myStopFunction()">Stop Meter</button>


        </form>
    </div>
</body>
</html>

<script type="text/javascript" language="javascript">


    var myVar=setInterval(function(){readdata(1,2)},2000);
    function myStopFunction()
    {
        clearInterval(myVar);
    }


    function checkfile(sender) {
        var validExts = new Array(".xlsx", ".xls", ".csv");
        var fileExt = sender.value;
        fileExt = fileExt.substring(fileExt.lastIndexOf('.'));
        if (validExts.indexOf(fileExt) < 0) {
            alert("Invalid file selected, valid files are of " +
               validExts.toString() + " types.");
            return false;
        }
        else return true;

    }
var xVal = 1;
var yVal = 2

    function readdata(x,y) {
        x = xVal;
        y = yVal;
        try {
            var excel = new ActiveXObject("Excel.Application");
            excel.Visible = false;
            var excel_file = excel.Workbooks.Open("D:\\Test.xls");// alert(excel_file.worksheets.count);
            var excel_sheet = excel_file.Worksheets("Sheet1");
            var data = excel_sheet.Cells(x, y).Value;
            //alert(data);
            drawWithexcelValue(data);
            xVal = xVal + 1;
            if(data==null || data=="")
            { 
            myStopFunction(); 
            }
            excel.Application.Quit();
        }
        catch (ex) {
            alert(ex);
        }
    }
</script>
于 2013-07-29T20:41:44.990 回答