你的位置:首页 > Java教程

[Java教程]JS导入Excel


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<head runat="server">
<title></title>
</head>
<script type="text/javascript">
function ReadExcel() {
var tempStr = "";
//得到文件路径的值
var filePath = document.getElementById("upfile").value;
//创建操作EXCEL应用程序的实例
var oXL = new ActiveXObject("Excel.application");
//打开指定路径的excel文件
var oWB = oXL.Workbooks.open(filePath);
//操作第一个sheet(从一开始,而非零)
oWB.worksheets(1).select();
var oSheet = oWB.ActiveSheet;
//使用的行数
var rows = oSheet.usedrange.rows.count;
try {
for (var i = 2; i <= rows; i++) {
if (oSheet.Cells(i, 2).value == "null" || oSheet.Cells(i, 3).value == "null") break;
var a = oSheet.Cells(i, 2).value.toString() == "undefined" ? "" : oSheet.Cells(i, 2).value;
tempStr += (" " + oSheet.Cells(i, 2).value + " " + oSheet.Cells(i, 3).value + " " + oSheet.Cells(i, 4).value + " " + oSheet.Cells(i, 5).value + " " + oSheet.Cells(i, 6).value + "\n");
}
} catch (e) {
document.getElementById("txtArea").value = tempStr;
}
document.getElementById("txtArea").value = tempStr;
//退出操作excel的实例对象
oXL.Application.Quit();
//手动调用垃圾收集器
CollectGarbage();
}


</script>
<body>
<input type="file" id="upfile" /><input type="button" onclick="ReadExcel();" value="read"/>

<br/>
<textarea id="txtArea" cols="50" rows="10"></textarea>

</body>
</html>