如何使用javascript查找excel工作表的范围(最后一行和最后一列)

时间:2015-08-21 13:28:32

标签: javascript html excel

我需要一个html文件中的javascript程序,它可以提醒我最后一行和最后一个cloumn在excel中有数据。目前我在下面的程序中硬编码i(行)和j(列)值。

该程序可以读取excel文件(自动检测excel文件和工作表)并将数据写入HTML。

我已经尝试了注释掉的代码。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>excel</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script type="text/javascript">
   function readFromExcel(x,y)
   {
var a =location.pathname;
var b= location.pathname.match(/[^\/]+$/)[0];
var c=a.replace(b,"myData.xlsx");
var file = c.substring(1)
      var excel = new ActiveXObject("Excel.Application");
      var excel_file = excel.Workbooks.Open(file);
    excel.Workbooks(1).Activate ;
    var  excel_sheet =excel_file.ActiveSheet;
        alert(excel_sheet.Name);

//  var LastRow = excel_sheet.Cells("A", excel_sheet.Rows.Count).End(xlUp).Row;
//  var LastRow =excel_sheet.Range("a10000").End(xlUp).Row;
//  var LastRow = excel_sheet.Range("A1").Value;
//  var LastRow = excel_sheet.Range("A1").Select;
//  var LastRow = wsTarget.Columns("A").Find("*", wsTarget.Range("A1"), xlValues, SearchDirection:=xlPrevious).Row;
//  var LastRow = excel_sheet.Range("A65536").End(xlUp).Row;
    alert(LastRow);


      var data = "<table>";  
for(i=1;i<10;i++){
    data= data + "<tr>";

    for(j=1;j<6;j++){

    data = data+"<td> " ;

    data= data + excel_sheet.Cells(i,j).Value ;

    data = data+ "</td>";
    }

data= data + "</tr>";
}
data = data + "</table>";
alert(data);
document.write(data);
return data;
    //Clean up

    excel.ActiveWorkbook.Close(true);
    excel.Application.Quit();
    excel = null;

    setTimeout("CollectGarbage()",1);
   }
</script>

</head>
<body onload="readFromExcel()">

<div id="dataTab">

</div>

</body>
</html>

0 个答案:

没有答案