use*_*227 5 javascript excel html5
我正在使用html5和javascript.我想读取用户从目录中选择的Excel文件(使用文件对话框),但我所知道的是按给定路径读取excel文件:
function readdata(y,x) {
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);
}
catch (ex) {
alert(ex);
}
return data;
}
Run Code Online (Sandbox Code Playgroud)
这是我必须阅读excel文件.任何人都可以帮助我按目录读取文件,或者不提供文件的路径.我也想知道如何显示输出.
Thanx提前
尝试下面的代码,它有点 hack,而且似乎可以在 IE7 上工作。但是,它不适用于其他浏览器,因为它们不会显示文件路径。其他浏览器也永远无法显示 ActiveXObject (Excel)。
<!DOCTYPE html>
<html>
<head>
<body>
<div id="divHidden" style="visibility: hidden; width: 0px; height: 0px">
<input type=file id="fileInput">
</div>
<input type=button value="click here to get a file" onclick="readdata(1,1);">
<script language=javascript>
function readdata(y,x) {
// Use the <input type='file'...> object to get a filename without showing the object.
document.all["fileInput"].click();
var fileName = document.all["fileInput"].value;
try {
var excel = new ActiveXObject("Excel.Application");
excel.Visible = false;
var excel_file = excel.Workbooks.Open(fileName);
var excel_sheet = excel_file.Worksheets("Sheet1");
// var data = excel_sheet.Cells(x, y).Value;
var data;
for (; excel_sheet.Cells(x, y).Value.length > 0; x++){
data[i][0] = excel_sheet.Cells(x, y).Value;
data[i][1] = excel_sheet.Cells(x, y+1).Value;
}
drawWithexcelValue(data);
}
catch (ex) {
alert(ex);
}
// This will show the data.
alert(data);
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2335 次 |
| 最近记录: |