![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/adamschen9921/1389938913-656726699.jpg)
![]() |
![]() |
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function test1() //讀取c:\test.xls
{
var excel;
excel = new ActiveXObject("Excel.Application");
excel.Visible = false;
form1.TextBox1.value = excel.Workbooks.Open("C:/test.xls").ActiveSheet.Cells(1,1).Value;
excel.Quit();
}
function test2() //讀取input的檔案
{
if (document.getElementById("file1").value != "")
{
var MyFilename = (document.getElementById("file1").value).replace(/\\/ig, "/"); //將路徑反斜線轉為正斜線
var excel;
excel = new ActiveXObject("Excel.Application");
excel.Visible = false;
var DisplayValue = excel.Workbooks.Open(MyFilename).ActiveSheet.Cells(1,1).Value; //也可讀文字檔,Cells(1,1)為第1列,Cells(2,1)為第2列
if (DisplayValue != null)
{
form1.TextBox2.value = DisplayValue;
}
else
{
form1.TextBox2.value = "";
}
alert(excel.Workbooks.Open(MyFilename).ActiveSheet.Cells(2,1).Value);
excel.Quit();
}
}
</script>
</head>
<body bgcolor="#ffffff">
<form id="form1" enctype="multipart/form-data" method="post">
<br>
<input type="button" value="讀取c:\test.xls" onclick="test1();"></input>
<br>
<input type="text" ID="TextBox1"></input>
<br>
<hr>
<br>
<input type="file" id="file1" onchange="test2();"></input>
<br>
<input type="text" ID="TextBox2"></input>
</form>
</body>
</html>