FlexSheet - SaveAsync (.359)

Import/Export excel file to FlexSheet using the loadAsync/saveAsync methods

by Troy Taylor

HTML

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="http://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<link rel="stylesheet" href="http://cdn.wijmo.com/5.20172.359/styles/wijmo.min.css">
<script src="http://cdn.wijmo.com/5.20172.359/controls/wijmo.min.js"></script>
<script src="http://cdn.wijmo.com/5.20172.359/controls/wijmo.input.min.js"></script>
<script src="http://cdn.wijmo.com/5.20172.359/controls/wijmo.grid.min.js"></script>
<script src="http://cdn.wijmo.com/5.20172.359/controls/wijmo.grid.filter.min.js"></script>
<script src="http://cdn.wijmo.com/5.20172.359/controls/wijmo.grid.sheet.min.js"></script>
<script src="http://cdn.wijmo.com/5.20172.359/controls/wijmo.xlsx.min.js"></script>
<script src="http://cdn.wijmo.com/5.20172.359/controls/wijmo.grid.xlsx.min.js"></script>
 <div class="container">
 
   <h1>
   FlexSheet Import/Export
   </h1>
   <p>This FlexSheet imports and exports a file <b>asynchronously</b></p>
   
 <div class="form-inline">
 <input type="file" 
    id="importFile"
    class="form-control"
    accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
    <!-- <button class="btn btn-default">Load</button> -->
    <button class="btn btn-default" id="saveBtn">Save</button>
 </div>
 <br>
 <div id="flexSheet" style="height:400px;"></div>
 
 </div>

JavaScript

var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(','),
    data = [];
for (var i = 0; i < countries.length; i++) {
    data.push({
        country: countries[i],
        downloads: Math.round(Math.random() * 20000),
        sales: Math.round(Math.random() * 10000),
        expenses: Math.round(Math.random() * 5000)
    });
}

// create the FlexSheet control
var flexSheet = new wijmo.grid.sheet.FlexSheet('#flexSheet');

var importFile = document.getElementById('importFile');
importFile.addEventListener('change', function() {
	loadWorkbook();
});
var saveButton = document.getElementById('saveBtn');
saveButton.addEventListener('click', function(s,e){
	save();
});

function loadWorkbook(){
	var reader;
  var file = importFile.files[0];
  if(file){
  	//
  	reader = new FileReader();
    reader.onload = function (e) {
    	flexSheet.loadAsync(reader.result);
    };
    reader.readAsDataURL(file);
  }
}

function save(){

	// this doesn't work
  flexSheet.saveAsync('', function (base64) {

     // user can access the base64 string in this callback.
     document.getElementById('importFile').href = 'data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;' + 'base64,' + base64;
     console.log(base64);
    }, function (reason) {

       // User can catch the failure reason in this callback.
       console.log('The reason of save failure is ' + reason);
  });
}


// create and add a new sheet the control
var sheet = new wijmo.grid.sheet.Sheet();
sheet.name = "New Sheet";
sheet.itemsSource = data;
flexSheet.sheets.push(sheet);

// push a new empty sheet onto the sheets array
var emptysheet = new wijmo.grid.sheet.Sheet(flexSheet, null, "Empty Sheet")
flexSheet.sheets.push(emptysheet);