javascript 如何使用javascript读取excel单元格值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9479832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read excel cell values using javascript
提问by user1237544
So, I need to be able to read an Excel sheet and access the values of cells. So far I have just created a small demo in an attempt to test code that I have seen elsewhere, but so far I've had no luck. Here's the demo:
因此,我需要能够读取 Excel 工作表并访问单元格的值。到目前为止,我刚刚创建了一个小演示,试图测试我在其他地方看到的代码,但到目前为止我还没有运气。这是演示:
function readFile(){
var excel=new ActiveXObject("Excel.Application");
var excel_file = excel.Workbooks.Open("C:\temp\mod9.csv");
var excel_sheet = excel_file.Worksheets("mod9");
var data = excel_sheet.Cells(1,1).Value;
alert(data);
return data
}
Really, with this demo I was just trying to see if I could pull a single value, but whenever I try to run this, it stops at the very first line of the function. It says "ActiveXObject is not defined." If you have another suggested way to read the file, then I would be happy to read that as well. Any help would be appreciated.
真的,在这个演示中,我只是想看看我是否可以提取单个值,但是每当我尝试运行它时,它就会停在函数的第一行。它说“ActiveXObject 未定义”。如果您有另一种阅读文件的建议方法,那么我也很乐意阅读。任何帮助,将不胜感激。
回答by Nitzan Tomer
As the comments to your question say, it's not recommended to use the ActiveXObjectin javascript since it will only work in IE, if you want to have a cross browser solution you'll have to take another approach. More than that, as @SimonWang commented, not only will the user need to open the page with IEhe will also need to have Excelinstalled on his machine, otherwise it won't work.
正如对您的问题的评论所说,不建议在 javascript 中使用ActiveXObject,因为它只能在IE 中工作,如果您想拥有跨浏览器解决方案,则必须采用另一种方法。更重要的是,正如@SimonWang 评论的那样,用户不仅需要用IE打开页面,还需要在他的机器上安装Excel,否则它将无法工作。
You can read the excel file on the server side, parse the data and return it to the javascript side with let's say an ajax request. I'm not sure how your server side is implemented, but there are solutions for that with almost every language out there. For example with php, you can check out this: http://php.net/manual/en/function.fgetcsv.php
您可以在服务器端读取 excel 文件,解析数据并将其返回到 javascript 端,假设是一个 ajax 请求。我不确定您的服务器端是如何实现的,但是几乎每种语言都有解决方案。例如使用 php,你可以看看这个:http: //php.net/manual/en/function.fgetcsv.php
回答by Philip Bevan
If you would like the excel spreadsheet so that any user can see it, you can publish it as a webpage using Google Docs.
如果您想要 Excel 电子表格以便任何用户都可以看到它,您可以使用 Google Docs 将其发布为网页。
Instructions are provided here:
此处提供说明:
http://support.google.com/docs/bin/answer.py?hl=en&answer=55244
http://support.google.com/docs/bin/answer.py?hl=zh-CN&answer=55244
Let me know if this helps.
如果这有帮助,请告诉我。
EDIT
编辑
Sorry I think I got confused by your question.
对不起,我想我对你的问题感到困惑。
Have you thought about using excel functionality to export the worksheet as CSV or XML and searching the files that way.
您是否考虑过使用 excel 功能将工作表导出为 CSV 或 XML 并以这种方式搜索文件。
File -> Save As -> Excel Workbook, CSV, XML, Web Page (HTML) ....
文件 -> 另存为 -> Excel 工作簿、CSV、XML、网页 (HTML) ....