如何使用 JavaScript 将数据从 excel (xlsx, csv) 导入 HTML 页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27883900/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:07:37  来源:igfitidea点击:

How can I import data from excel (xlsx, csv) to a HTML page using JavaScript?

javascripthtmlexcelcsvxlsx

提问by Modj

H! I have a .xlsx and .html files. How can I import the xlsx file to my html file using JavaScript, I already watched Lynda: JavaScript Essential Training by Simon Allardice and didn't get any clue how to do this! Googled a lot and still here. Tried the similar questions... Just need a script to copy it into my html file, and then my html file can read the xlsx and display it in the browser!

H!我有一个 .xlsx 和 .html 文件。如何使用 JavaScript 将 xlsx 文件导入到我的 html 文件中,我已经观看了 Simon Allardice 的 Lynda:JavaScript Essential Training 并且不知道如何执行此操作!谷歌搜索了很多,仍然在这里。试过类似的问题...只需要一个脚本将其复制到我的html文件中,然后我的html文件就可以读取xlsx并在浏览器中显示它!

回答by agershun

You can use AlasqlJavaScript SQL library, which has special functionality to read XLSX files and put in HTML page. It uses js-xlsxlibrary to read XLSX files.

您可以使用AlasqlJavaScript SQL 库,它具有读取 XLSX 文件和放入 HTML 页面的特殊功能。它使用js-xlsx库来读取 XLSX 文件。

Disclaimer: I am the author of Alasql.

免责声明:我是 Alasql 的作者。

In this example:

在这个例子中:

  • Alasql takes Excel file from cdn.rawgit.com (you can replace it with your address
  • Alasql put result table to DIV with id="res" - you can change the name or add CSS to prettify result table
  • {headers:true} flag means that Alasql reads and writes headers
  • You can read CSV files as well - just replace XLSX() to CSV() in the example
  • For production purpose download alasql.min.js and xlsx.core.min.js to your server or use CDN.
  • Alasql 从 cdn.rawgit.com 获取 Excel 文件(您可以将其替换为您的地址
  • Alasql 将结果表放入带有 id="res" 的 DIV - 您可以更改名称或添加 CSS 来美化结果表
  • {headers:true} 标志表示 Alasql 读写头
  • 您也可以读取 CSV 文件 - 只需将示例中的 XLSX() 替换为 CSV()
  • 出于生产目的,将 alasql.min.js 和 xlsx.core.min.js 下载到您的服务器或使用 CDN。

See the working code snippet below:

请参阅下面的工作代码片段:

alasql('select * into html("#res",{headers:true}) \
  from xlsx("https://cdn.rawgit.com/agershun/alasql/version-0.0.36/test/test168.xlsx",\
            {headers:true})')
<script src="https://cdn.jsdelivr.net/alasql/0.3/alasql.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.7.12/xlsx.core.min.js"></script>

<div id="res"></div>