How can I import data from excel (xlsx, csv) to a

2019-06-11 06:26发布

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!

1条回答
SAY GOODBYE
2楼-- · 2019-06-11 07:08

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

Disclaimer: I am the author of 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.

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>

查看更多
登录 后发表回答