简体   繁体   English

如何使用 javascript 读取 xlsx 文件

[英]How to read an xlsx file with javascript

So I have been googling and googling and googling this for the past few hours and every thing I have tried dose not work.所以在过去的几个小时里,我一直在谷歌搜索和谷歌搜索,而我尝试过的每件事都不起作用。

all I want to do is read the values in the cells (using the field name and index number) of an excel spreadsheet using javascript and then display the data that is in the cells as strings on an webpage.我要做的就是使用javascript读取excel电子表格的单元格中的值(使用字段名称和索引号),然后将单元格中的数据显示为网页上的字符串。

evey time i google this it eyther sends me to herehttp://codetheory.in/parse-read-excel-files-xls-xlsx-javascript which I can't get to work and I do not think it is what I want anyway as it looks like its just creating other files to use.每次我用谷歌搜索它,它都会把我送到这里http://codetheory.in/parse-read-excel-files-xls-xlsx-javascript我无法开始工作,我认为这不是我想要的无论如何,因为它看起来只是创建其他要使用的文件。

or it takes me to other posts here on stack overflow that either do not answer my question (for example they have converters where you drag and drop an xlsx file in to an converter to convert it to CSV or JSON which will not work because it has to be automated) or they have links that link to other posts that lead me to something that was irreverent.或者它将我带到这里关于堆栈溢出的其他帖子,这些帖子要么不回答我的问题(例如,他们有转换器,您可以在其中将 xlsx 文件拖放到转换器中以将其转换为 CSV 或 JSON,因为它有自动化)或者他们有链接到其他帖子的链接,这些帖子让我看到了一些不敬的东西。

EDIT----- To make it clear I am only to build a webpage that displays the content that is in the xslx.编辑-----为了清楚起见,我只是构建一个显示 xslx 中内容的网页。 The xslx file will be replaced every day. xslx 文件将每天更换。

I have no control over what goes on the server other then what is in the folder that holds the webpage and xslx file.除了保存网页和 xslx 文件的文件夹中的内容之外,我无法控制服务器上的内容。

the process has to be compleatley automated (with the exception of uploading the xslx file that just a copy and paste thing)该过程必须完全自动化(除了上传 xslx 文件,它只是一个复制和粘贴的东西)

Excel has no API that JavaScript can access client-side. Excel 没有 JavaScript 可以访问客户端的 API。 To the contrary, using VBA you can extract data from the spreadsheet and then open a web browser and write JavaScript into the document that the browser is showing.相反,使用 VBA,您可以从电子表格中提取数据,然后打开 Web 浏览器并将 JavaScript 写入浏览器显示的文档中。

To do what you want, you'd need to access the Excel data on the server, via some server-side API (probably .NET) and then deliver that data to the client as HTML, CSS and JavaScript.要做你想做的事,你需要通过一些服务器端 API(可能是 .NET)访问服务器上的 Excel 数据,然后将该数据作为 HTML、CSS 和 JavaScript 交付给客户端。

FYI: That example you referenced is a server-side solution using node.仅供参考:您引用的示例是使用节点的服务器端解决方案。

You should look at the answers there: How to read an excel file contents on client side?您应该在那里查看答案: 如何在客户端读取 excel 文件内容?

There is a working solution making use of the SheetJS lib and the FileReader API from javascript.有一个使用SheetJS lib 和来自 javascript 的FileReader API 的工作解决方案。

To read an Excel File this way first you need to read that file as a binary string using the FileReader API then you'll read that binary string using the SheetJS functionnalities.要以这种方式读取 Excel 文件,首先您需要使用FileReader API 将该文件作为binary string读取,然后您将使用SheetJS函数读取该binary string

This can be done 100% client side doesn't involve writing another file or uploading the file on a server, and as it is standard javascript it should works everywhere as long as the web navigator supports the FileReader API.这可以做到 100% 客户端不涉及写入另一个文件或将文件上传到服务器上,并且由于它是标准的 javascript,只要 Web 导航器支持FileReader API,它就可以在任何地方工作。

Related links:相关链接:
https://github.com/SheetJS/sheetjs https://github.com/SheetJS/sheetjs
https://developer.mozilla.org/en-US/docs/Web/API/FileReader https://developer.mozilla.org/en-US/docs/Web/API/FileReader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM