Javascript 使用 JQuery 读取 Excel 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5494387/
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
Read Excel data with JQuery
提问by Robert Fleming
I would like to know how to read data from a multi-worksheet MS Excel 2003 file using nothing, but jquery. I have read several solutions for PHP/JQuery, ActiveX, etc..., but I would like to do it with JQuery ONLY. Any idea of how this could work?
我想知道如何使用 jquery 从多工作表 MS Excel 2003 文件中读取数据。我已经阅读了几个针对 PHP/JQuery、ActiveX 等的解决方案......,但我想只用JQuery来做。知道这是如何工作的吗?
I have found http://plugins.jquery.com/project/csv2tableand this does the job almost perfectly, except for the fact that I have to break each sheet out into a CSV file. I would like to drop that step and read it directly from the Excel file. Thank you in advance for your help!
我找到了http://plugins.jquery.com/project/csv2table,这几乎完美地完成了这项工作,除了我必须将每个工作表分解成一个 CSV 文件的事实。我想放弃该步骤并直接从 Excel 文件中读取它。预先感谢您的帮助!
BTW - I am working in FireFox 4 and have no need for cross browser support.
顺便说一句 - 我在 FireFox 4 中工作,不需要跨浏览器支持。
回答by jason saldo
Office Web Components provide an api to excel documents via javascript (or vb). They are poorly documented but for intranet type applications they can get the job done. I have used them for the pivot table functionality in IE6 and do not know if it will work with Firefox.
Office Web Components 提供了一个 api,用于通过 javascript(或 vb)处理文档。它们的文档很差,但对于 Intranet 类型的应用程序,它们可以完成工作。我已经将它们用于 IE6 中的数据透视表功能,不知道它是否适用于 Firefox。
http://en.wikipedia.org/wiki/Office_Web_Components
回答by OzrenTkalcecKrznaric
Actually it's possible without OWC and such exotic addins, but manual work will be required. Another thing - it's doable on local machine only - don't expect your worksheet to act like a web server. Another possibility is to set it up on a network share, but I'm not sure how will it all work in the sandbox.
实际上,没有 OWC 和这种奇特的插件是可能的,但需要手动工作。另一件事 - 它仅在本地机器上可行 - 不要期望您的工作表像 Web 服务器一样运行。另一种可能性是将其设置在网络共享上,但我不确定它在沙箱中将如何工作。
EDIT:I know the question was about Excel 2003 format. However, there are still Google queries about the same functionality and today people are using MS Office versions 2010/2013. So, I believe the answer can be beneficial to the reader.
编辑:我知道问题是关于 Excel 2003 格式的。但是,仍然有关于相同功能的 Google 查询,如今人们使用的是 MS Office 版本 2010/2013。所以,我相信答案对读者是有益的。
So, here it is:
所以,这里是:
- Using zip.jsyou can open zipped files. This means you can open MS Office files starting from Office 2007 (.docx, .xlsx. etc). Older office files have custom format and you can't read them as zipped files.
- After you open the file, hierarchical folder structure with various files in it is available. The data itself is in
/xl/worksheets/[worksheet name].xml
and/xl/sharedStrings.xml
, which means you have to dig it out using XML parser and correlate afterwards. - Luckily enough, XML parser is available in jQuery:
$.parseXML('...')
- 使用zip.js您可以打开压缩文件。这意味着您可以从 Office 2007(.docx、.xlsx 等)开始打开 MS Office 文件。较旧的办公文件具有自定义格式,您无法将它们作为压缩文件阅读。
- 打开文件后,可以使用包含各种文件的分层文件夹结构。数据本身在
/xl/worksheets/[worksheet name].xml
and 中/xl/sharedStrings.xml
,这意味着您必须使用 XML 解析器将其挖掘出来并在之后进行关联。 - 幸运的是,XML 解析器在 jQuery 中可用:
$.parseXML('...')
Have fun ;)
玩得开心 ;)
回答by SheetJS
I realize this question is old, but I have a basic parser that supports most of what you are asking for: http://oss.sheetjs.com/js-xls/
我意识到这个问题很老,但我有一个基本的解析器,支持你所要求的大部分内容:http: //oss.sheetjs.com/js-xls/
回答by Naman Goel
There are a few tools (libraries) for nodeJS that you can find on the npm website. There's one called excel that reads xlsx files and there's one called office for most ms office files. Now since it's all Javascript anyway, you should be able to download, and check out the source to find a way to integrate it on the client-side. Hope that helps
您可以在 npm 网站上找到一些用于 nodeJS 的工具(库)。有一种叫做 excel 的可以读取 xlsx 文件,而对于大多数 ms office 文件,有一种叫做 office。现在,无论如何它都是 Javascript,您应该可以下载并查看源代码以找到将其集成到客户端的方法。希望有帮助
回答by Gaurav Agrawal
Convert the excel in a CSV file to get read from Jquery. make use of the Jquery plugin CSV2Table
将 excel 转换为 CSV 文件以从 Jquery 读取。使用 Jquery 插件 CSV2Table
<div id="view1"></div>
<script type="text/javascript">
$(function(){
$('#view1').csv2table('Path/Anil.csv');
});
</script>