从目录中读取 excel 文件,而不是从您提供的地址中读取 [英] read the excel file from the directory not from the address that you given

查看:28
本文介绍了从目录中读取 excel 文件,而不是从您提供的地址中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 html5 和 javascript.我想读取用户从目录中选择的 Excel 文件(使用文件对话框),但我所知道的是按给定路径读取 excel 文件:

function readdata(y,x) {尝试 {var excel = new ActiveXObject("Excel.Application");excel.Visible = false;var excel_file = excel.Workbooks.Open("D:\\Test.xls");//警报(excel_file.worksheets.count);var excel_sheet = excel_file.Worksheets("Sheet1");var data = excel_sheet.Cells(x, y).Value;//警报(数据);drawWithexcelValue(data);}抓住(前){警报(例如);}返回数据;}

这就是我要读取的excel文件.任何人都可以帮助我按目录读取文件,或者不提供文件路径.我也想知道如何显示输出.

提前谢谢

解决方案

试试下面的代码,它有点hack,它似乎适用于 IE7.但是,它不适用于其他浏览器,因为它们不会显示文件路径.其他浏览器也永远无法显示 ActiveXObject (Excel).

<头><身体><div id="divHidden" style="visibility: hidden; width: 0px; height: 0px"><input type=file id="fileInput">

<input type=button value="点击这里获取文件" onclick="readdata(1,1);"><脚本语言=javascript>函数读取数据(y,x){//使用 <input type='file'...>object 获取文件名而不显示对象.document.all["fileInput"].click();var fileName = document.all["fileInput"].value;尝试 {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;无功数据;for (; excel_sheet.Cells(x, y).Value.length > 0; x++){数据[i][0] = excel_sheet.Cells(x, y).Value;数据[i][1] = excel_sheet.Cells(x, y+1).Value;}drawWithexcelValue(data);}抓住(前){警报(例如);}//这将显示数据.警报(数据);}</html>

I am using html5 and javascript. I want to read the Excel file which is chosen by the user from the directory (using a file dialog) but all I know is to read an excel file by the given path:

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;
}

This is what I have to read the excel file. Can any one help me to read a file by directory, or without giving the path of the file. I would also like to know how to show output from that.

Thanx in advance

解决方案

Try the following code, it is a bit of a hack and it seems to work with IE7. However, it will not work with other browsers because they will not show the file path. Other browsers will also never be able to show the 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>

这篇关于从目录中读取 excel 文件,而不是从您提供的地址中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆