问题
Is there a way to generate an .xslx file from javascript and allow the user to download it by javascript? This page is geared towards being run offline in html 5 with no internet connectivity.
回答1:
You could generate a data URI, and let the user save the link. However, IE8 has very limited support for data URIs. There is a 32 KB limit, and it's not allowed to be used with a href
.
Also, you still need to find a actual XLSX JS library... But it is possible.
回答2:
It has been done successfully by Ed Spencer. This project is using an EXT DataGrid as the source of the data, but I'm sure you could adapt it pretty easily.
回答3:
As Javascript has no file I/O, it's not going to be easy for your users to download. However, this kind of work is perfectly suited for a simple PHP script, which could generate your XSLX and save to your server dynamically.
回答4:
You can generate any Office document with OpenXML SDK for Javascript http://openxmlsdkjs.codeplex.com/
As for allowing the user to save a file from JS I recommend FileSaver.js https://github.com/eligrey/FileSaver.js/
来源:https://stackoverflow.com/questions/2742422/generate-office-open-xml-excel-file-from-javascript