纯前端导入导出是V10中引人关注的功能之一。
使用前端excelio,可以将Excel文件(.xlsx)导入为JSON,将JSON对象导出为Excel。
-
导入
导入时使用excelio的open方法,在successCallBack回掉中我们可以获取到Spread.Sheets的JSON对象。
var excelFile = document.getElementById("fileDemo").files[0];excelIO.open(excelFile, function (json) {var workbookObj = json;
workbook.fromJSON(workbookObj);}, function (e) {
console.log(e);});
-
导出
导出时使用save方法,传递json对象,在successCallBack中会获得一个Excel文件的blob对象。您可以在前段通过使用FileSaver.js直接保存Excel,也可将blob提交服务器处理。
var fileName = document.getElementById("exportFileName").value;if (fileName.substr(-5, 5) !== '.xlsx') {
fileName += '.xlsx';}var json = JSON.stringify(workbook.toJSON());
excelIO.save(json, function (blob) {
saveAs(blob, fileName);}, function (e) {
console.log(e);});
需要注意的是前端ExcelIO仅支持07格式的Excel(.xlsx).
支持浏览器为:Microsoft IE10+、Mozilla FireFox、Safari和Google Chrome
导出时使用了开源组件FileSaver,github地址:https://github.com/eligrey/FileSaver.js/
更多资源
如果您对SpreadJS产品感兴趣,请到官方网站下载试用:/developer/spreadjs
产品咨询电话:400-657-6008
Spread.Sheets中文学习指南:http://demo.grapecity.com.cn/SpreadJS/TutorialSample/#/samples
Spread.Sheets在线英文产品文档:http://sphelp.grapecity.com/webhelp/SpreadSheets10/webframe.html