您好,您在代码中先获取到了ActiveSheet,然后又执行了fromJSON方法,
这会导致获取到的sheet被销毁,如图:
应该把getActiveSheet的调用放到getValue前,如下代码所示:
- window.onload = function () {
- var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
- // spread.fromJSON(jsonData);
- var excelIo = new GC.Spread.Excel.IO();
- document.getElementById('loadExcel').onclick = function () {
- var excelFile = document.getElementById("fileDemo").files[0];
- var password = document.getElementById('password').value;
- // here is excel IO API
- excelIo.open(excelFile, function (json) {
- var workbookObj = json;
- spread.fromJSON(workbookObj);
- }, function (e) {
- // process error
- alert(e.errorMessage);
- if (e.errorCode === 2/*noPassword*/ || e.errorCode === 3 /*invalidPassword*/) {
- document.getElementById('password').onselect = null;
- }
- }, { password: password });
- };
- document.getElementById('saveExcel').onclick = function () {
- var fileName = document.getElementById('exportFileName').value;
- var password = document.getElementById('password').value;
- if (fileName.substr(-5, 5) !== '.xlsx') {
- fileName += '.xlsx';
- }
- var json = spread.toJSON();
- // here is excel IO API
- excelIo.save(json, function (blob) {
- saveAs(blob, fileName);
- }, function (e) {
- // process error
- console.log(e);
- }, { password: password });
- };
- //保存数据
- document.getElementById('saveData').onclick = function () {
- var sheet = spread.getActiveSheet();
- var cell = sheet.getValue(0, 0);
- console.log(cell);
- //获取数据
- // var cell = sheet.getText(0, 0);
- // console.log(cell)
- // 获取原数据
- // var data = spread.toJSON();
- // console.log(data.sheets['工作表1']);
- }
- };
复制代码 |